instance.test.js
26.3 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('./support'),
DataTypes = require('../../lib/data-types'),
dialect = Support.getTestDialect(),
sinon = require('sinon'),
isUUID = require('validator').isUUID;
describe(Support.getTestDialectTeaser('Instance'), () => {
before(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.reset();
});
after(function() {
this.clock.restore();
});
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: { type: DataTypes.STRING },
uuidv1: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV1 },
uuidv4: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 },
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER },
bNumber: { type: DataTypes.INTEGER },
aDate: { type: DataTypes.DATE },
validateTest: {
type: DataTypes.INTEGER,
allowNull: true,
validate: { isInt: true }
},
validateCustom: {
type: DataTypes.STRING,
allowNull: true,
validate: { len: { msg: 'Length failed.', args: [1, 20] } }
},
dateAllowNullTrue: {
type: DataTypes.DATE,
allowNull: true
},
isSuperUser: {
type: DataTypes.BOOLEAN,
defaultValue: false
}
});
return this.User.sync({ force: true });
});
describe('Escaping', () => {
it('is done properly for special characters', function() {
// Ideally we should test more: "\0\n\r\b\t\\\'\"\x1a"
// But this causes sqlite to fail and exits the entire test suite immediately
const bio = `${dialect}'"\n`; // Need to add the dialect here so in case of failure I know what DB it failed for
return this.User.create({ username: bio }).then(u1 => {
return this.User.findByPk(u1.id).then(u2 => {
expect(u2.username).to.equal(bio);
});
});
});
});
describe('isNewRecord', () => {
it('returns true for non-saved objects', function() {
const user = this.User.build({ username: 'user' });
expect(user.id).to.be.null;
expect(user.isNewRecord).to.be.ok;
});
it('returns false for saved objects', function() {
return this.User.build({ username: 'user' }).save().then(user => {
expect(user.isNewRecord).to.not.be.ok;
});
});
it('returns false for created objects', function() {
return this.User.create({ username: 'user' }).then(user => {
expect(user.isNewRecord).to.not.be.ok;
});
});
it('returns false for objects found by find method', function() {
return this.User.create({ username: 'user' }).then(() => {
return this.User.create({ username: 'user' }).then(user => {
return this.User.findByPk(user.id).then(user => {
expect(user.isNewRecord).to.not.be.ok;
});
});
});
});
it('returns false for objects found by findAll method', function() {
const users = [];
for (let i = 0; i < 10; i++) {
users[i] = { username: 'user' };
}
return this.User.bulkCreate(users).then(() => {
return this.User.findAll().then(users => {
users.forEach(u => {
expect(u.isNewRecord).to.not.be.ok;
});
});
});
});
});
describe('default values', () => {
describe('uuid', () => {
it('should store a string in uuidv1 and uuidv4', function() {
const user = this.User.build({ username: 'a user' });
expect(user.uuidv1).to.be.a('string');
expect(user.uuidv4).to.be.a('string');
});
it('should store a string of length 36 in uuidv1 and uuidv4', function() {
const user = this.User.build({ username: 'a user' });
expect(user.uuidv1).to.have.length(36);
expect(user.uuidv4).to.have.length(36);
});
it('should store a valid uuid in uuidv1 and uuidv4 that conforms to the UUID v1 and v4 specifications', function() {
const user = this.User.build({ username: 'a user' });
expect(isUUID(user.uuidv1)).to.be.true;
expect(isUUID(user.uuidv4, 4)).to.be.true;
});
it('should store a valid uuid if the multiple primary key fields used', function() {
const Person = this.sequelize.define('Person', {
id1: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
},
id2: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
}
});
const person = Person.build({});
expect(person.id1).to.be.ok;
expect(person.id1).to.have.length(36);
expect(person.id2).to.be.ok;
expect(person.id2).to.have.length(36);
});
});
describe('current date', () => {
it('should store a date in touchedAt', function() {
const user = this.User.build({ username: 'a user' });
expect(user.touchedAt).to.be.instanceof(Date);
});
it('should store the current date in touchedAt', function() {
const clock = sinon.useFakeTimers();
clock.tick(5000);
const user = this.User.build({ username: 'a user' });
clock.restore();
expect(+user.touchedAt).to.be.equal(5000);
});
});
describe('allowNull date', () => {
it('should be just "null" and not Date with Invalid Date', function() {
return this.User.build({ username: 'a user' }).save().then(() => {
return this.User.findOne({ where: { username: 'a user' } }).then(user => {
expect(user.dateAllowNullTrue).to.be.null;
});
});
});
it('should be the same valid date when saving the date', function() {
const date = new Date();
return this.User.build({ username: 'a user', dateAllowNullTrue: date }).save().then(() => {
return this.User.findOne({ where: { username: 'a user' } }).then(user => {
expect(user.dateAllowNullTrue.toString()).to.equal(date.toString());
});
});
});
});
describe('super user boolean', () => {
it('should default to false', function() {
return this.User.build({
username: 'a user'
})
.save()
.then(() => {
return this.User.findOne({
where: {
username: 'a user'
}
})
.then(user => {
expect(user.isSuperUser).to.be.false;
});
});
});
it('should override default when given truthy boolean', function() {
return this.User.build({
username: 'a user',
isSuperUser: true
})
.save()
.then(() => {
return this.User.findOne({
where: {
username: 'a user'
}
})
.then(user => {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should override default when given truthy boolean-string ("true")', function() {
return this.User.build({
username: 'a user',
isSuperUser: 'true'
})
.save()
.then(() => {
return this.User.findOne({
where: {
username: 'a user'
}
})
.then(user => {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should override default when given truthy boolean-int (1)', function() {
return this.User.build({
username: 'a user',
isSuperUser: 1
})
.save()
.then(() => {
return this.User.findOne({
where: {
username: 'a user'
}
})
.then(user => {
expect(user.isSuperUser).to.be.true;
});
});
});
it('should throw error when given value of incorrect type', function() {
let callCount = 0;
return this.User.build({
username: 'a user',
isSuperUser: 'INCORRECT_VALUE_TYPE'
})
.save()
.then(() => {
callCount += 1;
})
.catch(err => {
expect(callCount).to.equal(0);
expect(err).to.exist;
expect(err.message).to.exist;
});
});
});
});
describe('complete', () => {
it('gets triggered if an error occurs', function() {
return this.User.findOne({ where: ['asdasdasd'] }).catch(err => {
expect(err).to.exist;
expect(err.message).to.exist;
});
});
it('gets triggered if everything was ok', function() {
return this.User.count().then(result => {
expect(result).to.exist;
});
});
});
describe('findAll', () => {
beforeEach(function() {
this.ParanoidUser = this.sequelize.define('ParanoidUser', {
username: { type: DataTypes.STRING }
}, { paranoid: true });
this.ParanoidUser.hasOne(this.ParanoidUser);
return this.ParanoidUser.sync({ force: true });
});
it('sql should have paranoid condition', function() {
return this.ParanoidUser.create({ username: 'cuss' })
.then(() => {
return this.ParanoidUser.findAll();
})
.then(users => {
expect(users).to.have.length(1);
return users[0].destroy();
})
.then(() => {
return this.ParanoidUser.findAll();
})
.then(users => {
expect(users).to.have.length(0);
});
});
it('sequelize.and as where should include paranoid condition', function() {
return this.ParanoidUser.create({ username: 'cuss' })
.then(() => {
return this.ParanoidUser.findAll({
where: this.sequelize.and({
username: 'cuss'
})
});
})
.then(users => {
expect(users).to.have.length(1);
return users[0].destroy();
})
.then(() => {
return this.ParanoidUser.findAll({
where: this.sequelize.and({
username: 'cuss'
})
});
})
.then(users => {
expect(users).to.have.length(0);
});
});
it('sequelize.or as where should include paranoid condition', function() {
return this.ParanoidUser.create({ username: 'cuss' })
.then(() => {
return this.ParanoidUser.findAll({
where: this.sequelize.or({
username: 'cuss'
})
});
})
.then(users => {
expect(users).to.have.length(1);
return users[0].destroy();
})
.then(() => {
return this.ParanoidUser.findAll({
where: this.sequelize.or({
username: 'cuss'
})
});
})
.then(users => {
expect(users).to.have.length(0);
});
});
it('escapes a single single quotes properly in where clauses', function() {
return this.User
.create({ username: "user'name" })
.then(() => {
return this.User.findAll({
where: { username: "user'name" }
}).then(users => {
expect(users.length).to.equal(1);
expect(users[0].username).to.equal("user'name");
});
});
});
it('escapes two single quotes properly in where clauses', function() {
return this.User
.create({ username: "user''name" })
.then(() => {
return this.User.findAll({
where: { username: "user''name" }
}).then(users => {
expect(users.length).to.equal(1);
expect(users[0].username).to.equal("user''name");
});
});
});
it('returns the timestamps if no attributes have been specified', function() {
return this.User.create({ username: 'fnord' }).then(() => {
return this.User.findAll().then(users => {
expect(users[0].createdAt).to.exist;
});
});
});
it('does not return the timestamps if the username attribute has been specified', function() {
return this.User.create({ username: 'fnord' }).then(() => {
return this.User.findAll({ attributes: ['username'] }).then(users => {
expect(users[0].createdAt).not.to.exist;
expect(users[0].username).to.exist;
});
});
});
it('creates the deletedAt property, when defining paranoid as true', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
expect(users[0].deletedAt).to.be.null;
});
});
});
it('destroys a record with a primary key of something other than id', function() {
const UserDestroy = this.sequelize.define('UserDestroy', {
newId: {
type: DataTypes.STRING,
primaryKey: true
},
email: DataTypes.STRING
});
return UserDestroy.sync().then(() => {
return UserDestroy.create({ newId: '123ABC', email: 'hello' }).then(() => {
return UserDestroy.findOne({ where: { email: 'hello' } }).then(user => {
return user.destroy();
});
});
});
});
it('sets deletedAt property to a specific date when deleting an instance', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
return users[0].destroy().then(() => {
expect(users[0].deletedAt.getMonth).to.exist;
return users[0].reload({ paranoid: false }).then(user => {
expect(user.deletedAt.getMonth).to.exist;
});
});
});
});
});
it('keeps the deletedAt-attribute with value null, when running update', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
return users[0].update({ username: 'newFnord' }).then(user => {
expect(user.deletedAt).not.to.exist;
});
});
});
});
it('keeps the deletedAt-attribute with value null, when updating associations', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
return this.ParanoidUser.create({ username: 'linkedFnord' }).then(linkedUser => {
return users[0].setParanoidUser(linkedUser).then(user => {
expect(user.deletedAt).not.to.exist;
});
});
});
});
});
it('can reuse query option objects', function() {
return this.User.create({ username: 'fnord' }).then(() => {
const query = { where: { username: 'fnord' } };
return this.User.findAll(query).then(users => {
expect(users[0].username).to.equal('fnord');
return this.User.findAll(query).then(users => {
expect(users[0].username).to.equal('fnord');
});
});
});
});
});
describe('findOne', () => {
it('can reuse query option objects', function() {
return this.User.create({ username: 'fnord' }).then(() => {
const query = { where: { username: 'fnord' } };
return this.User.findOne(query).then(user => {
expect(user.username).to.equal('fnord');
return this.User.findOne(query).then(user => {
expect(user.username).to.equal('fnord');
});
});
});
});
it('returns null for null, undefined, and unset boolean values', function() {
const Setting = this.sequelize.define('SettingHelper', {
setting_key: DataTypes.STRING,
bool_value: { type: DataTypes.BOOLEAN, allowNull: true },
bool_value2: { type: DataTypes.BOOLEAN, allowNull: true },
bool_value3: { type: DataTypes.BOOLEAN, allowNull: true }
}, { timestamps: false, logging: false });
return Setting.sync({ force: true }).then(() => {
return Setting.create({ setting_key: 'test', bool_value: null, bool_value2: undefined }).then(() => {
return Setting.findOne({ where: { setting_key: 'test' } }).then(setting => {
expect(setting.bool_value).to.equal(null);
expect(setting.bool_value2).to.equal(null);
expect(setting.bool_value3).to.equal(null);
});
});
});
});
});
describe('equals', () => {
it('can compare records with Date field', function() {
return this.User.create({ username: 'fnord' }).then(user1 => {
return this.User.findOne({ where: { username: 'fnord' } }).then(user2 => {
expect(user1.equals(user2)).to.be.true;
});
});
});
it('does not compare the existence of associations', function() {
this.UserAssociationEqual = this.sequelize.define('UserAssociationEquals', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
}, { timestamps: false });
this.ProjectAssociationEqual = this.sequelize.define('ProjectAssocationEquals', {
title: DataTypes.STRING,
overdue_days: DataTypes.INTEGER
}, { timestamps: false });
this.UserAssociationEqual.hasMany(this.ProjectAssociationEqual, { as: 'Projects', foreignKey: 'userId' });
this.ProjectAssociationEqual.belongsTo(this.UserAssociationEqual, { as: 'Users', foreignKey: 'userId' });
return this.UserAssociationEqual.sync({ force: true }).then(() => {
return this.ProjectAssociationEqual.sync({ force: true }).then(() => {
return this.UserAssociationEqual.create({ username: 'jimhalpert' }).then(user1 => {
return this.ProjectAssociationEqual.create({ title: 'A Cool Project' }).then(project1 => {
return user1.setProjects([project1]).then(() => {
return this.UserAssociationEqual.findOne({ where: { username: 'jimhalpert' }, include: [{ model: this.ProjectAssociationEqual, as: 'Projects' }] }).then(user2 => {
return this.UserAssociationEqual.create({ username: 'pambeesly' }).then(user3 => {
expect(user1.get('Projects')).to.not.exist;
expect(user2.get('Projects')).to.exist;
expect(user1.equals(user2)).to.be.true;
expect(user2.equals(user1)).to.be.true;
expect(user1.equals(user3)).to.not.be.true;
expect(user3.equals(user1)).to.not.be.true;
});
});
});
});
});
});
});
});
});
describe('values', () => {
it('returns all values', function() {
const User = this.sequelize.define('UserHelper', {
username: DataTypes.STRING
}, { timestamps: false, logging: false });
return User.sync().then(() => {
const user = User.build({ username: 'foo' });
expect(user.get({ plain: true })).to.deep.equal({ username: 'foo', id: null });
});
});
});
describe('isSoftDeleted', () => {
beforeEach(function() {
this.ParanoidUser = this.sequelize.define('ParanoidUser', {
username: { type: DataTypes.STRING }
}, { paranoid: true });
return this.ParanoidUser.sync({ force: true });
});
it('should return false when model is just created', function() {
return this.ParanoidUser.create({ username: 'foo' }).then(user => {
expect(user.isSoftDeleted()).to.be.false;
});
});
it('returns false if user is not soft deleted', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
expect(users[0].isSoftDeleted()).to.be.false;
});
});
});
it('returns true if user is soft deleted', function() {
return this.ParanoidUser.create({ username: 'fnord' }).then(() => {
return this.ParanoidUser.findAll().then(users => {
return users[0].destroy().then(() => {
expect(users[0].isSoftDeleted()).to.be.true;
return users[0].reload({ paranoid: false }).then(user => {
expect(user.isSoftDeleted()).to.be.true;
});
});
});
});
});
it('works with custom `deletedAt` field name', function() {
this.ParanoidUserWithCustomDeletedAt = this.sequelize.define('ParanoidUserWithCustomDeletedAt', {
username: { type: DataTypes.STRING }
}, {
deletedAt: 'deletedAtThisTime',
paranoid: true
});
this.ParanoidUserWithCustomDeletedAt.hasOne(this.ParanoidUser);
return this.ParanoidUserWithCustomDeletedAt.sync({ force: true }).then(() => {
return this.ParanoidUserWithCustomDeletedAt.create({ username: 'fnord' }).then(() => {
return this.ParanoidUserWithCustomDeletedAt.findAll().then(users => {
expect(users[0].isSoftDeleted()).to.be.false;
return users[0].destroy().then(() => {
expect(users[0].isSoftDeleted()).to.be.true;
return users[0].reload({ paranoid: false }).then(user => {
expect(user.isSoftDeleted()).to.be.true;
});
});
});
});
});
});
});
describe('restore', () => {
it('returns an error if the model is not paranoid', function() {
return this.User.create({ username: 'Peter', secretValue: '42' }).then(user => {
expect(() => {user.restore();}).to.throw(Error, 'Model is not paranoid');
});
});
it('restores a previously deleted model', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
secretValue: DataTypes.STRING,
data: DataTypes.STRING,
intVal: { type: DataTypes.INTEGER, defaultValue: 1 }
}, {
paranoid: true
}),
data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '43' },
{ username: 'Bob', secretValue: '44' }];
return ParanoidUser.sync({ force: true }).then(() => {
return ParanoidUser.bulkCreate(data);
}).then(() => {
return ParanoidUser.findOne({ where: { secretValue: '42' } });
}).then(user => {
return user.destroy().then(() => {
return user.restore();
});
}).then(() => {
return ParanoidUser.findOne({ where: { secretValue: '42' } });
}).then(user => {
expect(user).to.be.ok;
expect(user.username).to.equal('Peter');
});
});
it('supports custom deletedAt field', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
destroyTime: DataTypes.DATE
}, { paranoid: true, deletedAt: 'destroyTime' });
return ParanoidUser.sync({ force: true }).then(() => {
return ParanoidUser.create({
username: 'username'
});
}).then(user => {
return user.destroy();
}).then(user => {
expect(user.destroyTime).to.be.ok;
expect(user.deletedAt).to.not.be.ok;
return user.restore();
}).then(user => {
expect(user.destroyTime).to.not.be.ok;
return ParanoidUser.findOne({ where: { username: 'username' } });
}).then(user => {
expect(user).to.be.ok;
expect(user.destroyTime).to.not.be.ok;
expect(user.deletedAt).to.not.be.ok;
});
});
it('supports custom deletedAt field name', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
deletedAt: { type: DataTypes.DATE, field: 'deleted_at' }
}, { paranoid: true });
return ParanoidUser.sync({ force: true }).then(() => {
return ParanoidUser.create({
username: 'username'
});
}).then(user => {
return user.destroy();
}).then(user => {
expect(user.dataValues.deletedAt).to.be.ok;
expect(user.dataValues.deleted_at).to.not.be.ok;
return user.restore();
}).then(user => {
expect(user.dataValues.deletedAt).to.not.be.ok;
expect(user.dataValues.deleted_at).to.not.be.ok;
return ParanoidUser.findOne({ where: { username: 'username' } });
}).then(user => {
expect(user).to.be.ok;
expect(user.deletedAt).to.not.be.ok;
expect(user.deleted_at).to.not.be.ok;
});
});
it('supports custom deletedAt field and database column', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
destroyTime: { type: DataTypes.DATE, field: 'destroy_time' }
}, { paranoid: true, deletedAt: 'destroyTime' });
return ParanoidUser.sync({ force: true }).then(() => {
return ParanoidUser.create({
username: 'username'
});
}).then(user => {
return user.destroy();
}).then(user => {
expect(user.dataValues.destroyTime).to.be.ok;
expect(user.dataValues.deletedAt).to.not.be.ok;
expect(user.dataValues.destroy_time).to.not.be.ok;
return user.restore();
}).then(user => {
expect(user.dataValues.destroyTime).to.not.be.ok;
expect(user.dataValues.destroy_time).to.not.be.ok;
return ParanoidUser.findOne({ where: { username: 'username' } });
}).then(user => {
expect(user).to.be.ok;
expect(user.destroyTime).to.not.be.ok;
expect(user.destroy_time).to.not.be.ok;
});
});
it('supports custom default value', function() {
const ParanoidUser = this.sequelize.define('ParanoidUser', {
username: DataTypes.STRING,
deletedAt: { type: DataTypes.DATE, defaultValue: new Date(0) }
}, { paranoid: true });
return ParanoidUser.sync({ force: true }).then(() => {
return ParanoidUser.create({
username: 'username'
});
}).then(user => {
return user.destroy();
}).then(user => {
return user.restore();
}).then(user => {
expect(user.dataValues.deletedAt.toISOString()).to.equal(new Date(0).toISOString());
return ParanoidUser.findOne({ where: { username: 'username' } });
}).then(user => {
expect(user).to.be.ok;
expect(user.deletedAt.toISOString()).to.equal(new Date(0).toISOString());
});
});
});
});