paranoid.test.js
2.91 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
'use strict';
/* jshint -W030 */
var Support = require(__dirname + '/../support');
var DataTypes = require(__dirname + '/../../../lib/data-types');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var Support = require(__dirname + '/../support');
describe(Support.getTestDialectTeaser('Model'), function () {
describe('paranoid', function () {
before(function () {
this.clock = sinon.useFakeTimers();
});
after(function () {
this.clock.restore();
});
it('should be able to soft delete with timestamps', function () {
const Account = this.sequelize.define('Account', {
ownerId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'owner_id'
},
name: {
type: DataTypes.STRING
}
}, {
paranoid: true,
timestamps: true
});
return Account.sync({force: true})
.then(() => Account.create({ ownerId: 12 }))
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(1);
return Account.destroy({ where: { ownerId: 12 }})
.then((result) => {
expect(result).to.be.equal(1);
});
})
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(0);
return Account.count({ paranoid: false });
})
.then((count) => {
expect(count).to.be.equal(1);
return Account.restore({ where: { ownerId: 12 }});
})
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(1);
});
});
it('should be able to soft delete without timestamps', function () {
const Account = this.sequelize.define('Account', {
ownerId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'owner_id'
},
name: {
type: DataTypes.STRING
},
deletedAt: {
type: DataTypes.DATE,
allowNull: true,
field: 'deleted_at'
}
}, {
paranoid: true,
timestamps: true,
deletedAt: 'deletedAt',
createdAt: false,
updatedAt: false
});
return Account.sync({force: true})
.then(() => Account.create({ ownerId: 12 }))
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(1);
return Account.destroy({ where: { ownerId: 12 }});
})
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(0);
return Account.count({ paranoid: false });
})
.then((count) => {
expect(count).to.be.equal(1);
return Account.restore({ where: { ownerId: 12 }});
})
.then(() => Account.count())
.then((count) => {
expect(count).to.be.equal(1);
});
});
});
});