-
Notifications
You must be signed in to change notification settings - Fork 3
/
mongo.e2e.ts
547 lines (460 loc) · 17.2 KB
/
mongo.e2e.ts
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import { MongoClient, Db, Collection, Timestamp } from 'mongodb';
import {
$,
$addToSet,
$and,
$bit,
$currentDate,
$inc,
$max,
$min,
$mul,
$or,
$pop,
$pull,
$pullAll,
$push,
$rename,
$setOnInsert,
$timestamp,
$unset,
$xor,
flatten,
} from '../dist';
const MongoUrl = process.env.MONGODB_URL as string;
describe('End-to-end tests', () => {
let client: MongoClient;
let db: Db;
beforeAll(async () => {
client = await MongoClient.connect(MongoUrl);
db = client.db();
});
afterAll(() => client.close());
describe('Fields operators', () => {
const userMock = {
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
age: 30,
address: {
country: 'USA',
city: 'NY',
postCode: 'AB1234',
},
};
let collection: Collection<typeof userMock>;
const updateUser = async (value: any) => {
const searchCriteria = { email: userMock.email };
await collection.updateOne(searchCriteria, value);
const usr = await collection.findOne(searchCriteria);
if (usr === null) {
throw new Error('user not found');
}
return usr;
};
beforeAll(() => {
collection = db.collection('users');
});
beforeEach(() => collection.insertOne(userMock));
afterEach(() => collection.drop());
it('User should exist', async () => {
const usr = await collection.findOne({ email: userMock.email });
expect(usr).toMatchObject(userMock);
});
it('Update name', async () => {
const user = await updateUser(flatten({ firstName: 'Jack' }));
expect(user).toHaveProperty('firstName', 'Jack');
});
it('Update name to null', async () => {
const user = await updateUser(flatten({ firstName: null }));
expect(user).toHaveProperty('firstName', null);
});
it('Update address city', async () => {
const user = await updateUser(flatten({ address: { city: 'Boston' } }));
expect(user.address).toStrictEqual({ ...userMock.address, city: 'Boston' });
});
it('Add new `number` property to address', async () => {
const user = await updateUser(flatten({ address: { number: 9 } }));
expect(user.address).toStrictEqual({ ...userMock.address, number: 9 });
});
it('Increment age with a default value', async () => {
const user = await updateUser(flatten({ age: $inc() }));
expect(user).toHaveProperty('age', userMock.age + 1);
});
it('Increment age with 5', async () => {
const user = await updateUser(flatten({ age: $inc(5) }));
expect(user).toHaveProperty('age', userMock.age + 5);
});
it('Multiply age with a default value', async () => {
const user = await updateUser(flatten({ age: $mul() }));
expect(user).toHaveProperty('age', userMock.age);
});
it('Multiply age by two', async () => {
const user = await updateUser(flatten({ age: $mul(2) }));
expect(user).toHaveProperty('age', userMock.age * 2);
});
it('Rename firstName to first_name', async () => {
const user = await updateUser(flatten({ firstName: $rename('first_name') }));
expect(user).not.toHaveProperty('firstName');
expect(user).toHaveProperty('first_name', userMock.firstName);
});
it('Unset lastName', async () => {
const user = await updateUser(flatten({ lastName: $unset() }));
expect(user).not.toHaveProperty('lastName');
});
it('Update age to min when less than current value', async () => {
const user = await updateUser(flatten({ age: $min(userMock.age - 5) }));
expect(user).toHaveProperty('age', userMock.age - 5);
});
it('Update age to min when greater than current value', async () => {
const user = await updateUser(flatten({ age: $min(userMock.age + 5) }));
expect(user).toHaveProperty('age', userMock.age);
});
it('Update age to max when less than current value', async () => {
const user = await updateUser(flatten({ age: $max(userMock.age - 5) }));
expect(user).toHaveProperty('age', userMock.age);
});
it('Update age to max when greater than current value', async () => {
const user = await updateUser(flatten({ age: $max(userMock.age + 5) }));
expect(user).toHaveProperty('age', userMock.age + 5);
});
it('Set `updatedOn` to current date', async () => {
const user = await updateUser(flatten({ updatedOn: $currentDate() }));
expect(user).toHaveProperty('updatedOn');
expect((user as any).updatedOn).toBeInstanceOf(Date);
});
it('Set `time` to current timestamp', async () => {
const user = await updateUser(flatten({ time: $timestamp() }));
expect(user).toHaveProperty('time');
expect((user as any).time).toBeInstanceOf(Timestamp);
});
it('Update `pass` with $setOnInsert', async () => {
const user = await updateUser(flatten({ pass: $setOnInsert('change-me-next-time') }));
expect(user).not.toHaveProperty('pass');
});
it('Insert `pass` with $setOnInsert', async () => {
const criteria = { email: '[email protected]' };
const value = 'change-me-next-time';
await collection.updateOne(criteria, flatten({ pass: $setOnInsert(value) }), {
upsert: true,
});
const user = await collection.findOne(criteria);
expect(user).toHaveProperty('pass', value);
});
});
describe('Array operators', () => {
let collection: Collection<any>;
const searchCriteria = { userId: 1 };
const updateUser = async (updateCriteria: any, value: any) => {
await collection.updateOne(updateCriteria, value);
return await collection.findOne(searchCriteria);
};
beforeAll(() => {
collection = db.collection('users');
});
beforeEach(() =>
collection.insertOne({ userId: searchCriteria.userId, scores: [0, 2, 5, 5, 1, 3] })
);
afterEach(() => collection.drop());
it('Increment by 2 the score with value 1', async () => {
const user = await updateUser(
{ ...searchCriteria, scores: { $eq: 1 } },
flatten({ scores: $().$inc(2) })
);
expect(user.scores).toStrictEqual([0, 2, 5, 5, 3, 3]);
});
it('Unset the score with value 0', async () => {
const user = await updateUser(
{ ...searchCriteria, scores: { $eq: 0 } },
flatten({ scores: $().$unset() })
);
expect(user.scores).toStrictEqual([null, 2, 5, 5, 1, 3]);
});
it('Multiply all values by 10', async () => {
const user = await updateUser({ ...searchCriteria }, flatten({ scores: $('[]').$mul(10) }));
expect(user.scores).toStrictEqual([0, 20, 50, 50, 10, 30]);
});
it('Set the nested field for an element with a given grade value', async () => {
await collection.drop();
await collection.insertOne({
...searchCriteria,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 },
],
});
await collection.updateOne(
{ ...searchCriteria, 'grades.grade': 85 },
flatten({ grades: $('std').$set(6) })
);
const user = await collection.findOne({ ...searchCriteria });
expect(user.grades).toStrictEqual([
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 6 },
{ grade: 90, mean: 85, std: 3 },
]);
});
it('Increment all grades by 10 in nested documents', async () => {
await collection.drop();
await collection.insertOne({
...searchCriteria,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 },
],
});
await collection.updateOne(
{ ...searchCriteria },
flatten({ grades: $('[].grade').$inc(10) })
);
const user = await collection.findOne({ ...searchCriteria });
expect(user.grades).toStrictEqual([
{ grade: 90, mean: 75, std: 8 },
{ grade: 95, mean: 90, std: 5 },
{ grade: 100, mean: 85, std: 3 },
]);
});
it('Increment the grades by 10 in nested documents with std lower than 8', async () => {
await collection.drop();
await collection.insertOne({
...searchCriteria,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 },
],
});
await collection.updateOne(
{ ...searchCriteria },
flatten({ grades: $('[element].grade').$inc(10) }),
{ arrayFilters: [{ 'element.std': { $lt: 7 } }] }
);
const user = await collection.findOne({ ...searchCriteria });
expect(user.grades).toStrictEqual([
{ grade: 80, mean: 75, std: 8 },
{ grade: 95, mean: 90, std: 5 },
{ grade: 100, mean: 85, std: 3 },
]);
});
it('Update element at position 2', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $(2).$set(100) }));
expect(user.scores).toStrictEqual([0, 2, 100, 5, 1, 3]);
});
it('Update nested element at position 1', async () => {
await collection.drop();
await collection.insertOne({
...searchCriteria,
grades: [
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 },
],
});
await collection.updateOne(searchCriteria, flatten({ grades: $('1.mean').$inc(9) }));
const user = await collection.findOne(searchCriteria);
expect(user.grades).toStrictEqual([
{ grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 99, std: 5 },
{ grade: 90, mean: 85, std: 3 },
]);
});
it('Add a new element to the set', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $addToSet(9) }));
expect(user.scores).toStrictEqual([0, 2, 5, 5, 1, 3, 9]);
});
it('Add an existing element to the set', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $addToSet(2) }));
expect(user.scores).toStrictEqual([0, 2, 5, 5, 1, 3]);
});
it('Add multiple array elements', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $addToSet([2, 9]).$each() }));
expect(user.scores).toStrictEqual([0, 2, 5, 5, 1, 3, 9]);
});
it('Pop the first element', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pop().first() }));
expect(user.scores).toStrictEqual([2, 5, 5, 1, 3]);
});
it('Pop the last element', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pop().last() }));
expect(user.scores).toStrictEqual([0, 2, 5, 5, 1]);
});
it('Pull all values of 5', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pullAll(5) }));
expect(user.scores).toStrictEqual([0, 2, 1, 3]);
});
it('Pull all existing values', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $pullAll([0, 2, 5, 5, 1, 3]) })
);
expect(user.scores).toHaveLength(0);
});
it('Pull value 5', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pull(5) }));
expect(user.scores).toStrictEqual([0, 2, 1, 3]);
});
it('Pull values greater than 2', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pull({ $gte: 2 }) }));
expect(user.scores).toStrictEqual([0, 1]);
});
it('Pull multiple values', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $pull([0, 1]) }));
expect(user.scores).toStrictEqual([2, 5, 5, 3]);
});
it('Push 9', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $push(9) }));
expect(user.scores).toStrictEqual([0, 2, 5, 5, 1, 3, 9]);
});
it('Push 9 at zero position', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push(9).$each().$position(0) })
);
expect(user.scores).toStrictEqual([9, 0, 2, 5, 5, 1, 3]);
});
it('Push 9 and slice last 3 values', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push(9).$each().$slice(-3) })
);
expect(user.scores).toStrictEqual([1, 3, 9]);
});
it('Push 9 and sort ASC', async () => {
const user = await updateUser(searchCriteria, flatten({ scores: $push(9).$each().$sort() }));
expect(user.scores).toStrictEqual([0, 1, 2, 3, 5, 5, 9]);
});
it('Push 9 and sort DESC', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push(9).$each().$sort(-1) })
);
expect(user.scores).toStrictEqual([9, 5, 5, 3, 2, 1, 0]);
});
it('Push [9, 99], sort ASC and slice last 3', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push([9, 99]).$each().$sort().$slice(-3) })
);
expect(user.scores).toStrictEqual([5, 9, 99]);
});
it('Push [9, 99], sort ASC and slice first 3', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push([9, 99]).$each().$sort().$slice(3) })
);
expect(user.scores).toStrictEqual([0, 1, 2]);
});
it('Push [9, 99], at position 1 and slice first 3', async () => {
const user = await updateUser(
searchCriteria,
flatten({ scores: $push([9, 99]).$each().$position(1).$slice(3) })
);
expect(user.scores).toStrictEqual([0, 9, 99]);
});
describe('Nested arrays', () => {
let students: Collection<any>;
beforeEach(() => {
students = db.collection('students');
});
afterEach(() => students.drop());
it('Update filtered elements', async () => {
await students.insertOne({
_id: 1,
grades: [
{ type: 'quiz', questions: [10, 8, 5] },
{ type: 'quiz', questions: [8, 9, 6] },
{ type: 'hw', questions: [5, 4, 3] },
{ type: 'exam', questions: [25, 10, 23, 0] },
],
});
const data = {
grades: $('[t]').merge({
questions: $('[score]').$inc(2),
}),
};
await students.updateMany({}, flatten(data), {
arrayFilters: [{ 't.type': 'quiz' }, { score: { $gte: 8 } }],
});
const value = await students.findOne({ _id: 1 });
expect(value).toStrictEqual({
_id: 1,
grades: [
{ type: 'quiz', questions: [12, 10, 5] },
{ type: 'quiz', questions: [10, 11, 6] },
{ type: 'hw', questions: [5, 4, 3] },
{ type: 'exam', questions: [25, 10, 23, 0] },
],
});
});
it('Update all elements', async () => {
await students.insertOne({
_id: 1,
grades: [
{ type: 'quiz', questions: [10, 8, 5] },
{ type: 'quiz', questions: [8, 9, 6] },
{ type: 'hw', questions: [5, 4, 3] },
{ type: 'exam', questions: [25, 10, 23, 0] },
],
});
const data = {
grades: $('[]').merge({
questions: $('[score]').$inc(2),
}),
};
await students.updateMany({}, flatten(data), {
arrayFilters: [{ score: { $gte: 8 } }],
});
const value = await students.findOne({ _id: 1 });
expect(value).toStrictEqual({
_id: 1,
grades: [
{ type: 'quiz', questions: [12, 10, 5] },
{ type: 'quiz', questions: [10, 11, 6] },
{ type: 'hw', questions: [5, 4, 3] },
{ type: 'exam', questions: [27, 12, 25, 0] },
],
});
});
});
});
describe('Bitwise operators', function () {
const pointMock = { map: 'NY', value: 11 };
let collection: Collection<typeof pointMock>;
const updatePoint = async (value: any) => {
await collection.updateOne({ map: pointMock.map }, value);
const result = await collection.findOne({ map: pointMock.map });
if (!result) {
throw new Error('Not found.');
}
return result;
};
beforeAll(() => {
collection = db.collection('users');
});
beforeEach(async () => collection.insertOne(pointMock));
afterEach(async () => collection.drop());
it.each([$and(7), $bit().$and(7)])(
'Perform bitwise AND operation 11 & 7 = 1011 & 0111 = 0011 = 3',
async (value) => {
const point = await updatePoint(flatten({ value }));
expect(point.value).toStrictEqual(pointMock.value & 7);
}
);
it.each([$or(7), $bit().$or(7)])(
'Perform bitwise OR operation 11 | 7 = 1011 & 0111 = 1111 = 15',
async (value) => {
const point = await updatePoint(flatten({ value }));
expect(point.value).toStrictEqual(pointMock.value | 7);
}
);
it.each([$xor(7), $bit().$xor(7)])(
'Perform bitwise XOR operation 11 | 7 = 1011 & 0111 = 1100 = 12',
async (value) => {
const point = await updatePoint(flatten({ value }));
expect(point.value).toStrictEqual(pointMock.value ^ 7);
}
);
});
});