reload.test.js
10.7 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Sequelize = require('../../../index'),
Support = require('../support'),
DataTypes = require('../../../lib/data-types'),
sinon = require('sinon'),
current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), () => {
before(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.reset();
});
after(function() {
this.clock.restore();
});
beforeEach(async 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
}
});
await this.User.sync({ force: true });
});
describe('reload', () => {
if (current.dialect.supports.transactions) {
it('supports transactions', async function() {
const sequelize = await Support.prepareTransactionTest(this.sequelize);
const User = sequelize.define('User', { username: Support.Sequelize.STRING });
await User.sync({ force: true });
const user = await User.create({ username: 'foo' });
const t = await sequelize.transaction();
await User.update({ username: 'bar' }, { where: { username: 'foo' }, transaction: t });
const user1 = await user.reload();
expect(user1.username).to.equal('foo');
const user0 = await user1.reload({ transaction: t });
expect(user0.username).to.equal('bar');
await t.rollback();
});
}
it('should return a reference to the same DAO instead of creating a new one', async function() {
const originalUser = await this.User.create({ username: 'John Doe' });
await originalUser.update({ username: 'Doe John' });
const updatedUser = await originalUser.reload();
expect(originalUser === updatedUser).to.be.true;
});
it('should use default internal where', async function() {
const user = await this.User.create({ username: 'Balak Bukhara' });
const anotherUser = await this.User.create({ username: 'John Smith' });
const primaryKey = user.get('id');
await user.reload();
expect(user.get('id')).to.equal(primaryKey);
// options.where should be ignored
await user.reload({ where: { id: anotherUser.get('id') } });
expect(user.get('id')).to.equal(primaryKey).and.not.equal(anotherUser.get('id'));
});
it('should update the values on all references to the DAO', async function() {
const originalUser = await this.User.create({ username: 'John Doe' });
const updater = await this.User.findByPk(originalUser.id);
await updater.update({ username: 'Doe John' });
// We used a different reference when calling update, so originalUser is now out of sync
expect(originalUser.username).to.equal('John Doe');
const updatedUser = await originalUser.reload();
expect(originalUser.username).to.equal('Doe John');
expect(updatedUser.username).to.equal('Doe John');
});
it('should support updating a subset of attributes', async function() {
const user1 = await this.User.create({
aNumber: 1,
bNumber: 1
});
await this.User.update({
bNumber: 2
}, {
where: {
id: user1.get('id')
}
});
const user0 = user1;
const user = await user0.reload({
attributes: ['bNumber']
});
expect(user.get('aNumber')).to.equal(1);
expect(user.get('bNumber')).to.equal(2);
});
it('should update read only attributes as well (updatedAt)', async function() {
const originalUser = await this.User.create({ username: 'John Doe' });
this.originallyUpdatedAt = originalUser.updatedAt;
this.originalUser = originalUser;
// Wait for a second, so updatedAt will actually be different
this.clock.tick(1000);
const updater = await this.User.findByPk(originalUser.id);
const updatedUser = await updater.update({ username: 'Doe John' });
this.updatedUser = updatedUser;
await this.originalUser.reload();
expect(this.originalUser.updatedAt).to.be.above(this.originallyUpdatedAt);
expect(this.updatedUser.updatedAt).to.be.above(this.originallyUpdatedAt);
});
it('should update the associations as well', async function() {
const Book = this.sequelize.define('Book', { title: DataTypes.STRING }),
Page = this.sequelize.define('Page', { content: DataTypes.TEXT });
Book.hasMany(Page);
Page.belongsTo(Book);
await Book.sync({ force: true });
await Page.sync({ force: true });
const book = await Book.create({ title: 'A very old book' });
const page = await Page.create({ content: 'om nom nom' });
await book.setPages([page]);
const leBook = await Book.findOne({
where: { id: book.id },
include: [Page]
});
const page0 = await page.update({ content: 'something totally different' });
expect(leBook.Pages.length).to.equal(1);
expect(leBook.Pages[0].content).to.equal('om nom nom');
expect(page0.content).to.equal('something totally different');
const leBook0 = await leBook.reload();
expect(leBook0.Pages.length).to.equal(1);
expect(leBook0.Pages[0].content).to.equal('something totally different');
expect(page0.content).to.equal('something totally different');
});
it('should update internal options of the instance', async function() {
const Book = this.sequelize.define('Book', { title: DataTypes.STRING }),
Page = this.sequelize.define('Page', { content: DataTypes.TEXT });
Book.hasMany(Page);
Page.belongsTo(Book);
await Book.sync({ force: true });
await Page.sync({ force: true });
const book = await Book.create({ title: 'A very old book' });
const page = await Page.create();
await book.setPages([page]);
const leBook = await Book.findOne({
where: { id: book.id }
});
const oldOptions = leBook._options;
const leBook0 = await leBook.reload({
include: [Page]
});
expect(oldOptions).not.to.equal(leBook0._options);
expect(leBook0._options.include.length).to.equal(1);
expect(leBook0.Pages.length).to.equal(1);
expect(leBook0.get({ plain: true }).Pages.length).to.equal(1);
});
it('should return an error when reload fails', async function() {
const user = await this.User.create({ username: 'John Doe' });
await user.destroy();
await expect(user.reload()).to.be.rejectedWith(
Sequelize.InstanceError,
'Instance could not be reloaded because it does not exist anymore (find call returned null)'
);
});
it('should set an association to null after deletion, 1-1', async function() {
const Shoe = this.sequelize.define('Shoe', { brand: DataTypes.STRING }),
Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Player.hasOne(Shoe);
Shoe.belongsTo(Player);
await this.sequelize.sync({ force: true });
const shoe = await Shoe.create({
brand: 'the brand',
Player: {
name: 'the player'
}
}, { include: [Player] });
const lePlayer1 = await Player.findOne({
where: { id: shoe.Player.id },
include: [Shoe]
});
expect(lePlayer1.Shoe).not.to.be.null;
await lePlayer1.Shoe.destroy();
const lePlayer0 = lePlayer1;
const lePlayer = await lePlayer0.reload();
expect(lePlayer.Shoe).to.be.null;
});
it('should set an association to empty after all deletion, 1-N', async function() {
const Team = this.sequelize.define('Team', { name: DataTypes.STRING }),
Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player);
Player.belongsTo(Team);
await this.sequelize.sync({ force: true });
const team = await Team.create({
name: 'the team',
Players: [{
name: 'the player1'
}, {
name: 'the player2'
}]
}, { include: [Player] });
const leTeam1 = await Team.findOne({
where: { id: team.id },
include: [Player]
});
expect(leTeam1.Players).not.to.be.empty;
await leTeam1.Players[1].destroy();
await leTeam1.Players[0].destroy();
const leTeam0 = leTeam1;
const leTeam = await leTeam0.reload();
expect(leTeam.Players).to.be.empty;
});
it('should update the associations after one element deleted', async function() {
const Team = this.sequelize.define('Team', { name: DataTypes.STRING }),
Player = this.sequelize.define('Player', { name: DataTypes.STRING });
Team.hasMany(Player);
Player.belongsTo(Team);
await this.sequelize.sync({ force: true });
const team = await Team.create({
name: 'the team',
Players: [{
name: 'the player1'
}, {
name: 'the player2'
}]
}, { include: [Player] });
const leTeam1 = await Team.findOne({
where: { id: team.id },
include: [Player]
});
expect(leTeam1.Players).to.have.length(2);
await leTeam1.Players[0].destroy();
const leTeam0 = leTeam1;
const leTeam = await leTeam0.reload();
expect(leTeam.Players).to.have.length(1);
});
it('should inject default scope when reloading', async function() {
const Bar = this.sequelize.define('Bar', {
name: DataTypes.TEXT
});
const Foo = this.sequelize.define('Foo', {
name: DataTypes.TEXT
}, {
defaultScope: {
include: [{ model: Bar }]
}
});
Bar.belongsTo(Foo);
Foo.hasMany(Bar);
await this.sequelize.sync();
const foo = await Foo.create({ name: 'foo' });
await foo.createBar({ name: 'bar' });
const fooFromFind = await Foo.findByPk(foo.id);
expect(fooFromFind.Bars).to.be.ok;
expect(fooFromFind.Bars[0].name).to.equal('bar');
await foo.reload();
expect(foo.Bars).to.be.ok;
expect(foo.Bars[0].name).to.equal('bar');
});
});
});