paranoid.test.js
2.71 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
sinon = require('sinon'),
Support = require('../support'),
DataTypes = require('../../../lib/data-types');
describe(Support.getTestDialectTeaser('Paranoid'), () => {
beforeEach(async function() {
const S = this.sequelize,
DT = DataTypes,
A = this.A = S.define('A', { name: DT.STRING }, { paranoid: true }),
B = this.B = S.define('B', { name: DT.STRING }, { paranoid: true }),
C = this.C = S.define('C', { name: DT.STRING }, { paranoid: true }),
D = this.D = S.define('D', { name: DT.STRING }, { paranoid: true });
A.belongsTo(B);
A.belongsToMany(D, { through: 'a_d' });
A.hasMany(C);
B.hasMany(A);
B.hasMany(C);
C.belongsTo(A);
C.belongsTo(B);
D.belongsToMany(A, { through: 'a_d' });
await S.sync({ force: true });
});
before(function() {
this.clock = sinon.useFakeTimers();
});
after(function() {
this.clock.restore();
});
it('paranoid with timestamps: false should be ignored / not crash', async function() {
const S = this.sequelize,
Test = S.define('Test', {
name: DataTypes.STRING
}, {
timestamps: false,
paranoid: true
});
await S.sync({ force: true });
await Test.findByPk(1);
});
it('test if non required is marked as false', async function() {
const A = this.A,
B = this.B,
options = {
include: [
{
model: B,
required: false
}
]
};
await A.findOne(options);
expect(options.include[0].required).to.be.equal(false);
});
it('test if required is marked as true', async function() {
const A = this.A,
B = this.B,
options = {
include: [
{
model: B,
required: true
}
]
};
await A.findOne(options);
expect(options.include[0].required).to.be.equal(true);
});
it('should not load paranoid, destroyed instances, with a non-paranoid parent', async function() {
const X = this.sequelize.define('x', {
name: DataTypes.STRING
}, {
paranoid: false
});
const Y = this.sequelize.define('y', {
name: DataTypes.STRING
}, {
timestamps: true,
paranoid: true
});
X.hasMany(Y);
await this.sequelize.sync({ force: true });
const [x0, y] = await Promise.all([
X.create(),
Y.create()
]);
this.x = x0;
this.y = y;
await x0.addY(y);
await this.y.destroy();
//prevent CURRENT_TIMESTAMP to be same
this.clock.tick(1000);
const obj = await X.findAll({
include: [Y]
});
const x = await obj[0];
expect(x.ys).to.have.length(0);
});
});