values.test.js
16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
'use strict';
const chai = require('chai'),
Sequelize = require('../../../index'),
expect = chai.expect,
Support = require('../support'),
dialect = Support.getTestDialect(),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('DAO'), () => {
describe('Values', () => {
describe('set', () => {
it('doesn\'t overwrite generated primary keys', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
const user = User.build({ id: 1, name: 'Mick' });
expect(user.get('id')).to.equal(1);
expect(user.get('name')).to.equal('Mick');
user.set({
id: 2,
name: 'Jan'
});
expect(user.get('id')).to.equal(1);
expect(user.get('name')).to.equal('Jan');
});
it('doesn\'t overwrite defined primary keys', function() {
const User = this.sequelize.define('User', {
identifier: { type: DataTypes.STRING, primaryKey: true }
});
const user = User.build({ identifier: 'identifier' });
expect(user.get('identifier')).to.equal('identifier');
user.set('identifier', 'another identifier');
expect(user.get('identifier')).to.equal('identifier');
});
it('doesn\'t set timestamps', function() {
const User = this.sequelize.define('User', {
identifier: { type: DataTypes.STRING, primaryKey: true }
});
const user = User.build({}, {
isNewRecord: false
});
user.set({
createdAt: new Date(2000, 1, 1),
updatedAt: new Date(2000, 1, 1)
});
expect(user.get('createdAt')).not.to.be.ok;
expect(user.get('updatedAt')).not.to.be.ok;
});
it('doesn\'t set underscored timestamps', function() {
const User = this.sequelize.define('User', {
identifier: { type: DataTypes.STRING, primaryKey: true }
}, {
underscored: true
});
const user = User.build({}, {
isNewRecord: false
});
user.set({
created_at: new Date(2000, 1, 1),
updated_at: new Date(2000, 1, 1)
});
expect(user.get('created_at')).not.to.be.ok;
expect(user.get('updated_at')).not.to.be.ok;
});
it('doesn\'t set value if not a dynamic setter or a model attribute', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING },
email_hidden: { type: DataTypes.STRING }
}, {
setterMethods: {
email_secret(value) {
this.set('email_hidden', value);
}
}
});
const user = User.build();
user.set({
name: 'antonio banderaz',
email: 'antonio@banderaz.com',
email_secret: 'foo@bar.com'
});
user.set('email', 'antonio@banderaz.com');
expect(user.get('name')).to.equal('antonio banderaz');
expect(user.get('email_hidden')).to.equal('foo@bar.com');
expect(user.get('email')).not.to.be.ok;
expect(user.dataValues.email).not.to.be.ok;
});
it('allows use of sequelize.fn and sequelize.col in date and bool fields', function() {
const User = this.sequelize.define('User', {
d: DataTypes.DATE,
b: DataTypes.BOOLEAN,
always_false: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
}, { timestamps: false });
return User.sync({ force: true }).then(() => {
return User.create({}).then(user => {
// Create the user first to set the proper default values. PG does not support column references in insert,
// so we must create a record with the right value for always_false, then reference it in an update
let now = dialect === 'sqlite' ? this.sequelize.fn('', this.sequelize.fn('datetime', 'now')) : this.sequelize.fn('NOW');
if (dialect === 'mssql') {
now = this.sequelize.fn('', this.sequelize.fn('getdate'));
}
user.set({
d: now,
b: this.sequelize.col('always_false')
});
expect(user.get('d')).to.be.instanceof(Sequelize.Utils.Fn);
expect(user.get('b')).to.be.instanceof(Sequelize.Utils.Col);
return user.save().then(() => {
return user.reload().then(() => {
expect(user.d).to.equalDate(new Date());
expect(user.b).to.equal(false);
});
});
});
});
});
describe('includes', () => {
it('should support basic includes', function() {
const Product = this.sequelize.define('product', {
title: Sequelize.STRING
});
const Tag = this.sequelize.define('tag', {
name: Sequelize.STRING
});
const User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
Product.hasMany(Tag);
Product.belongsTo(User);
const product = Product.build({}, {
include: [
User,
Tag
]
});
product.set({
id: 1,
title: 'Chair',
tags: [
{ id: 1, name: 'Alpha' },
{ id: 2, name: 'Beta' }
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
});
expect(product.tags).to.be.ok;
expect(product.tags.length).to.equal(2);
expect(product.tags[0]).to.be.instanceof(Tag);
expect(product.user).to.be.ok;
expect(product.user).to.be.instanceof(User);
});
it('should support basic includes (with raw: true)', function() {
const Product = this.sequelize.define('Product', {
title: Sequelize.STRING
});
const Tag = this.sequelize.define('tag', {
name: Sequelize.STRING
});
const User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
Product.hasMany(Tag);
Product.belongsTo(User);
const product = Product.build({}, {
include: [
User,
Tag
]
});
product.set({
id: 1,
title: 'Chair',
tags: [
{ id: 1, name: 'Alpha' },
{ id: 2, name: 'Beta' }
],
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
}, { raw: true });
expect(product.tags).to.be.ok;
expect(product.tags.length).to.equal(2);
expect(product.tags[0]).to.be.instanceof(Tag);
expect(product.user).to.be.ok;
expect(product.user).to.be.instanceof(User);
});
});
});
describe('get', () => {
it('should use custom attribute getters in get(key)', function() {
const Product = this.sequelize.define('Product', {
price: {
type: Sequelize.FLOAT,
get() {
return this.dataValues.price * 100;
}
}
});
const product = Product.build({
price: 10
});
expect(product.get('price')).to.equal(1000);
});
it('should custom virtual getters in get(key)', function() {
const Product = this.sequelize.define('Product', {
priceInCents: {
type: Sequelize.FLOAT
}
}, {
getterMethods: {
price() {
return this.dataValues.priceInCents / 100;
}
}
});
const product = Product.build({
priceInCents: 1000
});
expect(product.get('price')).to.equal(10);
});
it('should use custom getters in toJSON', function() {
const Product = this.sequelize.define('Product', {
price: {
type: Sequelize.STRING,
get() {
return this.dataValues.price * 100;
}
}
}, {
getterMethods: {
withTaxes() {
return this.get('price') * 1.25;
}
}
});
const product = Product.build({
price: 10
});
expect(product.toJSON()).to.deep.equal({ withTaxes: 1250, price: 1000, id: null });
});
it('should work with save', function() {
const Contact = this.sequelize.define('Contact', {
first: { type: Sequelize.STRING },
last: { type: Sequelize.STRING },
tags: {
type: Sequelize.STRING,
get(field) {
const val = this.getDataValue(field);
return JSON.parse(val);
},
set(val, field) {
this.setDataValue(field, JSON.stringify(val));
}
}
});
return this.sequelize.sync().then(() => {
const contact = Contact.build({
first: 'My',
last: 'Name',
tags: ['yes', 'no']
});
expect(contact.get('tags')).to.deep.equal(['yes', 'no']);
return contact.save().then(me => {
expect(me.get('tags')).to.deep.equal(['yes', 'no']);
});
});
});
describe('plain', () => {
it('should return plain values when true', function() {
const Product = this.sequelize.define('product', {
title: Sequelize.STRING
});
const User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
Product.belongsTo(User);
const product = Product.build({}, {
include: [
User
]
});
product.set({
id: 1,
title: 'Chair',
user: {
id: 1,
first_name: 'Mick',
last_name: 'Hansen'
}
}, { raw: true });
expect(product.get('user', { plain: true })).not.to.be.instanceof(User);
expect(product.get({ plain: true }).user).not.to.be.instanceof(User);
});
});
describe('clone', () => {
it('should copy the values', function() {
const Product = this.sequelize.define('product', {
title: Sequelize.STRING
});
const product = Product.build({
id: 1,
title: 'Chair'
}, { raw: true });
const values = product.get({ clone: true });
delete values.title;
expect(product.get({ clone: true }).title).to.be.ok;
});
});
it('can pass parameters to getters', function() {
const Product = this.sequelize.define('product', {
title: Sequelize.STRING
}, {
getterMethods: {
rating(key, options) {
if (options.apiVersion > 1) {
return 100;
}
return 5;
}
}
});
const User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
}, {
getterMethods: {
height(key, options) {
if (options.apiVersion > 1) {
return 185; // cm
}
return 6.06; // ft
}
}
});
Product.belongsTo(User);
const product = Product.build({}, {
include: [
User
]
});
product.set({
id: 1,
title: 'Chair',
user: {
id: 1,
first_name: 'Jozef',
last_name: 'Hartinger'
}
});
expect(product.get('rating')).to.equal(5);
expect(product.get('rating', { apiVersion: 2 })).to.equal(100);
expect(product.get({ plain: true })).to.have.property('rating', 5);
expect(product.get({ plain: true }).user).to.have.property('height', 6.06);
expect(product.get({ plain: true, apiVersion: 1 })).to.have.property('rating', 5);
expect(product.get({ plain: true, apiVersion: 1 }).user).to.have.property('height', 6.06);
expect(product.get({ plain: true, apiVersion: 2 })).to.have.property('rating', 100);
expect(product.get({ plain: true, apiVersion: 2 }).user).to.have.property('height', 185);
expect(product.get('user').get('height', { apiVersion: 2 })).to.equal(185);
});
});
describe('changed', () => {
it('should return false if object was built from database', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
return User.sync().then(() => {
return User.create({ name: 'Jan Meier' }).then(user => {
expect(user.changed('name')).to.be.false;
expect(user.changed()).not.to.be.ok;
});
}).then(() => {
return User.bulkCreate([{ name: 'Jan Meier' }]).then(([user]) => {
expect(user.changed('name')).to.be.false;
expect(user.changed()).not.to.be.ok;
});
});
});
it('should return true if previous value is different', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
const user = User.build({
name: 'Jan Meier'
});
user.set('name', 'Mick Hansen');
expect(user.changed('name')).to.be.true;
expect(user.changed()).to.be.ok;
});
it('should return false immediately after saving', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
return User.sync().then(() => {
const user = User.build({
name: 'Jan Meier'
});
user.set('name', 'Mick Hansen');
expect(user.changed('name')).to.be.true;
expect(user.changed()).to.be.ok;
return user.save().then(() => {
expect(user.changed('name')).to.be.false;
expect(user.changed()).not.to.be.ok;
});
});
});
it('should be available to a afterUpdate hook', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
let changed;
User.afterUpdate(instance => {
changed = instance.changed();
return;
});
return User.sync({ force: true }).then(() => {
return User.create({
name: 'Ford Prefect'
});
}).then(user => {
return user.update({
name: 'Arthur Dent'
});
}).then(user => {
expect(changed).to.be.ok;
expect(changed.length).to.be.ok;
expect(changed).to.include('name');
expect(user.changed()).not.to.be.ok;
});
});
});
describe('previous', () => {
it('should return an object with the previous values', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING },
title: { type: DataTypes.STRING }
});
const user = User.build({
name: 'Jan Meier',
title: 'Mr'
});
user.set('name', 'Mick Hansen');
user.set('title', 'Dr');
expect(user.previous()).to.eql({ name: 'Jan Meier', title: 'Mr' });
});
it('should return the previous value', function() {
const User = this.sequelize.define('User', {
name: { type: DataTypes.STRING }
});
const user = User.build({
name: 'Jan Meier'
});
user.set('name', 'Mick Hansen');
expect(user.previous('name')).to.equal('Jan Meier');
expect(user.get('name')).to.equal('Mick Hansen');
});
});
});
});