/
basic-querying.test.js
450 lines (385 loc) · 14.4 KB
/
basic-querying.test.js
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// This test written in mocha+should.js
var should = require('./init.js');
var expect = require('expect');
var db, User;
/* global getSchema */
/* eslint max-nested-callbacks: [2, 6] */
describe('basic-querying', function() {
before(function(done) {
db = getSchema();
User = db.define('User', {
name: {type: String, sort: true, limit: 100},
email: {type: String, index: true, limit: 100},
role: {type: String, index: true, limit: 100},
order: {type: Number, index: true, sort: true, limit: 100}
});
db.automigrate(done);
});
describe('find', function() {
before(function(done) {
User.destroyAll(done);
});
it('should query by id: not found', function(done) {
User.find(1, function(err, u) {
should.not.exist(u);
should.not.exist(err);
done();
});
});
it('should query by id: found', function(done) {
User.create(function(err, u) {
should.not.exist(err);
should.exist(u.id);
User.find(u.id, function(err, u) {
should.exist(u);
should.not.exist(err);
u.should.be.an.instanceOf(User);
done();
});
});
});
});
describe('all', function() {
before(seed);
it('should query collection', function(done) {
User.all(function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(6);
done();
});
});
it('should query limited collection', function(done) {
User.all({limit: 3}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(3);
done();
});
});
it('should query offset collection with limit', function(done) {
User.all({skip: 1, limit: 4}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(4);
done();
});
});
it('should query filtered collection', function(done) {
User.all({where: {role: 'lead'}}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(2);
done();
});
});
it('should query collection sorted by numeric field', function(done) {
User.all({order: 'order'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
u.order.should.eql(i + 1);
});
done();
});
});
it('should query collection desc sorted by numeric field', function(done) {
User.all({order: 'order DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
u.order.should.eql(users.length - i);
});
done();
});
});
it('should query collection sorted by string field', function(done) {
User.all({order: 'name'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.shift().name.should.equal('George Harrison');
users.shift().name.should.equal('John Lennon');
users.pop().name.should.equal('Stuart Sutcliffe');
done();
});
});
it('should query collection desc sorted by string field', function(done) {
User.all({order: 'name DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.pop().name.should.equal('George Harrison');
users.pop().name.should.equal('John Lennon');
users.shift().name.should.equal('Stuart Sutcliffe');
done();
});
});
});
describe('#all.attributes', function() {
it('should query collection and return given attribute as an array of Objects', function(done) {
User.all({attributes: ['id']}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.be.instanceOf(Array);
users.pop().should.be.instanceOf(Object).and.have.property('id');
done();
});
});
it('should query collection and return given attribute as an array of Numbers', function(done) {
User.all({attributes: 'id'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.be.instanceOf(Array);
users.pop().should.be.a.Number;
done();
});
});
it('should query collection and return given attributes as an array of objects', function(done) {
User.all({attributes: ['id', 'name']}, function(err, users) {
should.exists(users);
should.not.exists(err);
should.not.exists(users.pop().mail);
should.not.exists(users.pop().order);
users.pop().should.be.instanceOf(Object).and.have.property('id');
users.pop().should.be.instanceOf(Object).and.have.property('name');
done();
});
});
});
describe('count', function() {
before(seed);
it('should query total count', function(done) {
User.count(function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(6);
done();
});
});
it('should query filtered count', function(done) {
User.count({role: 'lead'}, function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(2);
done();
});
});
});
describe('findOne', function() {
before(seed);
it('should find first record (default sort by id)', function(done) {
User.all({order: 'id'}, function(err, users) {
User.findOne(function(e, u) {
should.not.exist(e);
should.exist(u);
u.id.toString().should.equal(users[0].id.toString());
done();
});
});
});
it('should find first record', function(done) {
User.findOne({order: 'order'}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(1);
u.name.should.equal('Paul McCartney');
done();
});
});
it('should find last record', function(done) {
User.findOne({order: 'order DESC'}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(6);
u.name.should.equal('Ringo Starr');
done();
});
});
it('should find last record in filtered set', function(done) {
User.findOne({
where: {role: 'lead'},
order: 'order DESC'
}, function(e, u) {
should.not.exist(e);
should.exist(u);
u.order.should.equal(2);
u.name.should.equal('John Lennon');
done();
});
});
it('should work even when find by id', function(done) {
User.findOne(function(e, u) {
User.findOne({where: {id: u.id}}, function(err, user) {
should.not.exist(err);
should.exist(user);
done();
});
});
});
});
describe('exists', function() {
before(seed);
it('should check whether record exist', function(done) {
User.findOne(function(e, u) {
User.exists(u.id, function(err, exists) {
should.not.exist(err);
should.exist(exists);
exists.should.be.ok;
done();
});
});
});
it('should check whether record not exist', function(done) {
User.destroyAll(function() {
User.exists(42, function(err, exists) {
should.not.exist(err);
exists.should.not.be.ok;
done();
});
});
});
});
describe('updateOrCreate', function() {
it('should update existing record', function() {
var id;
return User.create({
name: 'anatoliy',
email: 'mail@example.co.uk',
order: 1602
})
.then(function(ud) {
id = ud.id;
return User.updateOrCreate({
id: id,
name: 'Anatoliy'
});
})
.then(function(ud) {
should.exist(ud);
ud.id.should.equal(id);
ud.name.should.equal('Anatoliy');
should.exist(ud.email);
ud.email.should.equal('mail@example.co.uk');
should.exist(ud.order);
ud.order.should.equal(1602);
});
});
it('should create when record does not exist', function() {
return User.updateOrCreate({
id: 100000,
name: 'Anatoliy'
});
});
});
describe('bulk', function() {
var Model;
before(function() {
Model = db.define('Model', {
foo: String,
bar: Number
});
return db.automigrate();
});
afterEach(function() { return Model.destroyAll(); });
context('single update', function() {
it('should throw when no sufficient params provided', function() {
return Model.bulkUpdate({ where: { foo: 1 }})
.then(function() { throw new Error('Unexpected success'); })
.catch(function(err) { expect(err.message).toBe('Required update'); });
});
it('should throw when no sufficient params provided', function() {
return Model.bulkUpdate({ update: { foo: 1 }})
.then(function() { throw new Error('Unexpected success'); })
.catch(function(err) { expect(err.message).toBe('Required where'); });
});
it('should update record in database', function() {
return Model.create([
{ foo: 'baz', bar: 1 },
{ foo: 'fuu', bar: 1 }
])
.then(function() {
return Model.bulkUpdate({
update: { bar: 2 },
where: { foo: 'fuu' }
});
})
.then(function() {
return Model.all({ where: { foo: 'fuu' }});
})
.then(function(records) {
expect(records.length).toBe(1);
expect(records[0].bar).toBe(2);
})
.then(function() {
return Model.all({ where: { foo: 'baz' }});
})
.then(function(records) {
expect(records.length).toBe(1);
expect(records[0].bar).toBe(1);
});
});
it('should allow to limit update', function() {
return Model.create([
{ foo: 'bar', bar: 1 },
{ foo: 'bar', bar: 1 }
])
.then(function() {
return Model.bulkUpdate({
update: { bar: 2 },
where: { foo: 'bar' },
limit: 1
});
})
.then(function() { return Model.count({ bar: 2 }); })
.then(function(count) {
expect(count).toBe(1);
})
.then(function() { return Model.count({ bar: 1 }); })
.then(function(count) {
expect(count).toBe(1);
});
});
});
context('multiple records', function() {
it('should throw when no sufficient params provided', function() {
return Model.bulkUpdate([{ where: { foo: 1 }}])
.then(function() { throw new Error('Unexpected success'); })
.catch(function(err) { expect(err.message).toBe('Required update'); });
});
it('should throw when no sufficient params provided', function() {
return Model.bulkUpdate([{ update: { foo: 1 }}])
.then(function() { throw new Error('Unexpected success'); })
.catch(function(err) { expect(err.message).toBe('Required where'); });
});
});
});
});
function seed(done) {
var count = 0;
var beatles = [
{
name: 'John Lennon',
mail: 'john@b3atl3s.co.uk',
role: 'lead',
order: 2
}, {
name: 'Paul McCartney',
mail: 'paul@b3atl3s.co.uk',
role: 'lead',
order: 1
},
{name: 'George Harrison', order: 5},
{name: 'Ringo Starr', order: 6},
{name: 'Pete Best', order: 4},
{name: 'Stuart Sutcliffe', order: 3}
];
User.destroyAll(function() {
beatles.forEach(function(beatle) {
User.create(beatle, ok);
});
});
function ok() {
if (++count === beatles.length) {
done();
}
}
}