has-many.test.js
8.55 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
'use strict';
const chai = require('chai'),
sinon = require('sinon'),
expect = chai.expect,
stub = sinon.stub,
_ = require('lodash'),
Support = require(__dirname + '/../support'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
HasMany = require(__dirname + '/../../../lib/associations/has-many'),
Op = require(__dirname + '/../../../lib/operators'),
current = Support.sequelize,
Promise = current.Promise;
describe(Support.getTestDialectTeaser('hasMany'), () => {
it('throws when invalid model is passed', () => {
const User = current.define('User');
expect(() => {
User.hasMany();
}).to.throw('User.hasMany called with something that\'s not a subclass of Sequelize.Model');
});
describe('optimizations using bulk create, destroy and update', () => {
const User =current.define('User', { username: DataTypes.STRING }),
Task = current.define('Task', { title: DataTypes.STRING });
User.hasMany(Task);
const user = User.build({
id: 42
}),
task1 = Task.build({
id: 15
}),
task2 = Task.build({
id: 16
});
beforeEach(function() {
this.findAll = stub(Task, 'findAll').returns(Promise.resolve([]));
this.update = stub(Task, 'update').returns(Promise.resolve([]));
});
afterEach(function() {
this.findAll.restore();
this.update.restore();
});
it('uses one update statement for addition', function() {
return user.setTasks([task1, task2]).bind(this). then(function() {
expect(this.findAll).to.have.been.calledOnce;
expect(this.update).to.have.been.calledOnce;
});
});
it('uses one delete from statement', function() {
this.findAll
.onFirstCall().returns(Promise.resolve([]))
.onSecondCall().returns(Promise.resolve([
{ userId: 42, taskId: 15 },
{ userId: 42, taskId: 16 }
]));
return user.setTasks([task1, task2]).bind(this).then(function() {
this.update.resetHistory();
return user.setTasks(null);
}).then(function() {
expect(this.findAll).to.have.been.calledTwice;
expect(this.update).to.have.been.calledOnce;
});
});
});
describe('mixin', () => {
const User =current.define('User'),
Task = current.define('Task');
it('should mixin association methods', () => {
const as = Math.random().toString(),
association = new HasMany(User, Task, {as}),
obj = {};
association.mixin(obj);
expect(obj[association.accessors.get]).to.be.an('function');
expect(obj[association.accessors.set]).to.be.an('function');
expect(obj[association.accessors.addMultiple]).to.be.an('function');
expect(obj[association.accessors.add]).to.be.an('function');
expect(obj[association.accessors.remove]).to.be.an('function');
expect(obj[association.accessors.removeMultiple]).to.be.an('function');
expect(obj[association.accessors.hasSingle]).to.be.an('function');
expect(obj[association.accessors.hasAll]).to.be.an('function');
expect(obj[association.accessors.count]).to.be.an('function');
});
it('should not override custom methods', () => {
const methods = {
getTasks: 'get',
countTasks: 'count',
hasTask: 'has',
hasTasks: 'has',
setTasks: 'set',
addTask: 'add',
addTasks: 'add',
removeTask: 'remove',
removeTasks: 'remove',
createTask: 'create'
};
_.each(methods, (alias, method) => {
User.prototype[method] = function() {
const realMethod = this.constructor.associations.task[alias];
expect(realMethod).to.be.a('function');
return realMethod;
};
});
User.hasMany(Task, { as: 'task' });
const user = User.build();
_.each(methods, (alias, method) => {
expect(user[method]()).to.be.a('function');
});
});
});
describe('get', () => {
const User =current.define('User', {}),
Task = current.define('Task', {}),
idA = Math.random().toString(),
idB = Math.random().toString(),
idC = Math.random().toString(),
foreignKey = 'user_id';
it('should fetch associations for a single instance', () => {
const findAll = stub(Task, 'findAll').returns(Promise.resolve([
Task.build({}),
Task.build({})
])),
where = {};
User.Tasks = User.hasMany(Task, {foreignKey});
const actual = User.Tasks.get(User.build({id: idA}));
where[foreignKey] = idA;
expect(findAll).to.have.been.calledOnce;
expect(findAll.firstCall.args[0].where).to.deep.equal(where);
return actual.then(results => {
expect(results).to.be.an('array');
expect(results.length).to.equal(2);
}).finally(() => {
findAll.restore();
});
});
it('should fetch associations for multiple source instances', () => {
const findAll = stub(Task, 'findAll').returns(
Promise.resolve([
Task.build({
'user_id': idA
}),
Task.build({
'user_id': idA
}),
Task.build({
'user_id': idA
}),
Task.build({
'user_id': idB
})
]));
User.Tasks = User.hasMany(Task, {foreignKey});
const actual = User.Tasks.get([
User.build({id: idA}),
User.build({id: idB}),
User.build({id: idC})
]);
expect(findAll).to.have.been.calledOnce;
expect(findAll.firstCall.args[0].where).to.have.property(foreignKey);
expect(findAll.firstCall.args[0].where[foreignKey]).to.have.property(Op.in);
expect(findAll.firstCall.args[0].where[foreignKey][Op.in]).to.deep.equal([idA, idB, idC]);
return actual.then(result => {
expect(result).to.be.an('object');
expect(Object.keys(result)).to.deep.equal([idA, idB, idC]);
expect(result[idA].length).to.equal(3);
expect(result[idB].length).to.equal(1);
expect(result[idC].length).to.equal(0);
}).finally(() => {
findAll.restore();
});
});
});
describe('association hooks', () => {
beforeEach(function() {
this.Projects = this.sequelize.define('Project', { title: DataTypes.STRING });
this.Tasks = this.sequelize.define('Task', { title: DataTypes.STRING });
});
describe('beforeHasManyAssociate', () => {
it('should trigger', function() {
const beforeAssociate = sinon.spy();
this.Projects.beforeAssociate(beforeAssociate);
this.Projects.hasMany(this.Tasks, {hooks: true});
const beforeAssociateArgs = beforeAssociate.getCall(0).args;
expect(beforeAssociate).to.have.been.called;
expect(beforeAssociateArgs.length).to.equal(2);
const firstArg = beforeAssociateArgs[0];
expect(Object.keys(firstArg).join()).to.equal('source,target,type');
expect(firstArg.source).to.equal(this.Projects);
expect(firstArg.target).to.equal(this.Tasks);
expect(firstArg.type.name).to.equal('HasMany');
expect(beforeAssociateArgs[1].sequelize.constructor.name).to.equal('Sequelize');
});
it('should not trigger association hooks', function() {
const beforeAssociate = sinon.spy();
this.Projects.beforeAssociate(beforeAssociate);
this.Projects.hasMany(this.Tasks, {hooks: false});
expect(beforeAssociate).to.not.have.been.called;
});
});
describe('afterHasManyAssociate', () => {
it('should trigger', function() {
const afterAssociate = sinon.spy();
this.Projects.afterAssociate(afterAssociate);
this.Projects.hasMany(this.Tasks, {hooks: true});
const afterAssociateArgs = afterAssociate.getCall(0).args;
expect(afterAssociate).to.have.been.called;
const firstArg = afterAssociateArgs[0];
expect(Object.keys(firstArg).join()).to.equal('source,target,type,association');
expect(firstArg.source).to.equal(this.Projects);
expect(firstArg.target).to.equal(this.Tasks);
expect(firstArg.type.name).to.equal('HasMany');
expect(firstArg.association.constructor.name).to.equal('HasMany');
expect(afterAssociateArgs[1].sequelize.constructor.name).to.equal('Sequelize');
});
it('should not trigger association hooks', function() {
const afterAssociate = sinon.spy();
this.Projects.afterAssociate(afterAssociate);
this.Projects.hasMany(this.Tasks, {hooks: false});
expect(afterAssociate).to.not.have.been.called;
});
});
});
});