scope.test.js
9.68 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
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() {
var Project = current.define('project')
, User = current.define('user')
, Company;
var scopes = {
complexFunction: function(value) {
return {
where: [(value + ' IN (SELECT foobar FROM some_sql_function(foo.bar))')]
};
},
somethingTrue: {
where: {
something: true,
somethingElse: 42
},
limit: 5
},
somethingFalse: {
where: {
something: false
}
},
users: {
include: [
{ model: User }
]
},
alsoUsers: {
include: [
{ model: User, where: { something: 42}}
]
},
projects: {
include: [Project]
},
noArgs: function () {
// This does not make much sense, since it does not actually need to be in a function,
// In reality it could be used to do for example new Date or random in the scope - but we want it deterministic
return {
where: {
other_value: 7
}
};
},
actualValue: function(value) {
return {
where: {
other_value: value
}
};
},
};
Company = current.define('company', {}, {
defaultScope: {
include: [Project],
where: { active: true }
},
scopes: scopes
});
describe('.scope', function () {
it('should apply default scope', function () {
expect(Company.$scope).to.deep.equal({
include: [{ model: Project }],
where: { active: true }
});
});
it('should be able to unscope', function () {
expect(Company.scope(null).$scope).to.be.empty;
expect(Company.unscoped().$scope).to.be.empty;
});
it('should be able to merge scopes', function() {
expect(Company.scope('somethingTrue', 'somethingFalse').$scope).to.deep.equal({
where: {
something: false,
somethingElse: 42
},
limit: 5
});
});
it('should support multiple, coexistent scoped models', function () {
var scoped1 = Company.scope('somethingTrue')
, scoped2 = Company.scope('somethingFalse');
expect(scoped1.$scope).to.deep.equal(scopes.somethingTrue);
expect(scoped2.$scope).to.deep.equal(scopes.somethingFalse);
});
it('should work with function scopes', function () {
expect(Company.scope({method: ['actualValue', 11]}).$scope).to.deep.equal({
where: {
other_value: 11
}
});
expect(Company.scope('noArgs').$scope).to.deep.equal({
where: {
other_value: 7
}
});
});
it('should be able to merge two scoped includes', function () {
expect(Company.scope('users', 'projects').$scope).to.deep.equal({
include: [
{ model: User },
{ model: Project }
]
});
});
it('should be able to override the default scope', function() {
expect(Company.scope('somethingTrue').$scope).to.deep.equal(scopes.somethingTrue);
});
it('should be able to combine default with another scope', function () {
expect(Company.scope(['defaultScope', {method: ['actualValue', 11]}]).$scope).to.deep.equal({
include: [{ model: Project }],
where: {
active: true,
other_value: 11
}
});
});
it('should be able to use raw queries', function () {
expect(Company.scope([{method: ['complexFunction', 'qux']}]).$scope).to.deep.equal({
where: [ 'qux IN (SELECT foobar FROM some_sql_function(foo.bar))' ]
});
});
it('should override the default scope', function () {
expect(Company.scope(['defaultScope', {method: ['complexFunction', 'qux']}]).$scope).to.deep.equal({
include: [{ model: Project }],
where: [ 'qux IN (SELECT foobar FROM some_sql_function(foo.bar))' ]
});
});
it('should emit an error for scopes that dont exist', function() {
expect(function () {
Company.scope('doesntexist');
}).to.throw('Invalid scope doesntexist called.');
});
});
describe('addScope', function () {
it('works if the model does not have any initial scopes', function () {
var Model = current.define('model');
expect(function () {
Model.addScope('anything', {});
}).not.to.throw();
});
it('allows me to add a new scope', function () {
expect(function () {
Company.scope('newScope');
}).to.throw('Invalid scope newScope called.');
Company.addScope('newScope', {
where: {
this: 'that'
},
include: [Project]
});
expect(Company.scope('newScope').$scope).to.deep.equal({
where: { this: 'that' },
include: [{ model: Project }]
});
});
it('warns me when overriding an existing scope', function () {
expect(function () {
Company.addScope('somethingTrue', {});
}).to.throw('The scope somethingTrue already exists. Pass { override: true } as options to silence this error');
});
it('allows me to override an existing scope', function () {
Company.addScope('somethingTrue', {
where: {
something: false
}
}, { override: true });
expect(Company.scope('somethingTrue').$scope).to.deep.equal({
where: { something: false },
});
});
it('warns me when overriding an existing default scope', function () {
expect(function () {
Company.addScope('defaultScope', {});
}).to.throw('The scope defaultScope already exists. Pass { override: true } as options to silence this error');
});
it('allows me to override a default scope', function () {
Company.addScope('defaultScope', {
include: [Project]
}, { override: true });
expect(Company.$scope).to.deep.equal({
include: [{ model: Project }]
});
});
});
describe('$injectScope', function () {
it('should be able to merge scope and where', function () {
var scope = {
where: {
something: true,
somethingElse: 42
},
limit: 15,
offset: 3
};
var options = {
where: {
something: false
},
limit: 9
};
current.Model.$injectScope(scope, options);
expect(options).to.deep.equal({
where: {
something: false,
somethingElse: 42
},
limit: 9,
offset: 3
});
});
it('should be able to overwrite multiple scopes with the same include', function () {
var scope = {
include: [
{ model: Project, where: { something: false }},
{ model: Project, where: { something: true }}
]
};
var options = {};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
});
it('should be able to override scoped include', function () {
var scope = {
include: [{ model: Project, where: { something: false }}]
};
var options = {
include: [{ model: Project, where: { something: true }}]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
});
it('should be able to merge aliassed includes with the same model', function () {
var scope = {
include: [{model: User, as: 'someUser'}]
};
var options = {
include: [{model: User, as: 'otherUser'}]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({model: User, as: 'otherUser'});
expect(options.include[1]).to.deep.equal({model: User, as: 'someUser'});
});
it('should be able to merge scoped include with include in find', function () {
var scope = {
include: [
{ model: Project, where: { something: false }}
]
};
var options = {
include: [
{ model: User, where: { something: true }}
]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }});
expect(options.include[1]).to.deep.equal({ model: Project, where: { something: false }});
});
describe('include all', function () {
it('scope with all', function () {
var scope = {
include: [
{ all: true }
]
};
var options = {
include: [
{ model: User, where: { something: true }}
]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }});
expect(options.include[1]).to.deep.equal({ all: true });
});
it('options with all', function () {
var scope = {
include: [
{ model: User, where: { something: true }}
]
};
var options = {
include: [
{ all: true }
]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ all: true });
expect(options.include[1]).to.deep.equal({ model: User, where: { something: true }});
});
});
});
});