-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathParseQuery.Aggregate.spec.js
1503 lines (1413 loc) · 47.8 KB
/
ParseQuery.Aggregate.spec.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const Parse = require('parse/node');
const request = require('../lib/request');
const Config = require('../lib/Config');
const masterKeyHeaders = {
'X-Parse-Application-Id': 'test',
'X-Parse-Rest-API-Key': 'test',
'X-Parse-Master-Key': 'test',
'Content-Type': 'application/json',
};
const masterKeyOptions = {
headers: masterKeyHeaders,
json: true,
};
const PointerObject = Parse.Object.extend({
className: 'PointerObject',
});
const loadTestData = () => {
const data1 = {
score: 10,
name: 'foo',
sender: { group: 'A' },
views: 900,
size: ['S', 'M'],
};
const data2 = {
score: 10,
name: 'foo',
sender: { group: 'A' },
views: 800,
size: ['M', 'L'],
};
const data3 = {
score: 10,
name: 'bar',
sender: { group: 'B' },
views: 700,
size: ['S'],
};
const data4 = {
score: 20,
name: 'dpl',
sender: { group: 'B' },
views: 700,
size: ['S'],
};
const obj1 = new TestObject(data1);
const obj2 = new TestObject(data2);
const obj3 = new TestObject(data3);
const obj4 = new TestObject(data4);
return Parse.Object.saveAll([obj1, obj2, obj3, obj4]);
};
const get = function (url, options) {
options.qs = options.body;
delete options.body;
Object.keys(options.qs).forEach(key => {
options.qs[key] = JSON.stringify(options.qs[key]);
});
return request(Object.assign({}, { url }, options))
.then(response => response.data)
.catch(response => {
throw { error: response.data };
});
};
describe('Parse.Query Aggregate testing', () => {
beforeEach(async () => {
await loadTestData();
});
it('should only query aggregate with master key', done => {
Parse._request('GET', `aggregate/someClass`, {}).then(
() => {},
error => {
expect(error.message).toEqual('unauthorized: master key is required');
done();
}
);
});
it('invalid query group _id required', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: {},
},
});
get(Parse.serverURL + '/aggregate/TestObject', options).catch(error => {
expect(error.error.code).toEqual(Parse.Error.INVALID_QUERY);
done();
});
});
it_id('add7050f-65d5-4a13-b526-5bd1ee09c7f1')(it)('group by field', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: '$name' },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(3);
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(resp.results[1], 'objectId')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(resp.results[2], 'objectId')).toBe(true);
expect(resp.results[0].objectId).not.toBe(undefined);
expect(resp.results[1].objectId).not.toBe(undefined);
expect(resp.results[2].objectId).not.toBe(undefined);
done();
})
.catch(done.fail);
});
it_id('0ab0d776-e45d-419a-9b35-3d11933b77d1')(it)('group by pipeline operator', async () => {
const options = Object.assign({}, masterKeyOptions, {
body: {
pipeline: {
$group: { _id: '$name' },
},
},
});
const resp = await get(Parse.serverURL + '/aggregate/TestObject', options);
expect(resp.results.length).toBe(3);
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(resp.results[1], 'objectId')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(resp.results[2], 'objectId')).toBe(true);
expect(resp.results[0].objectId).not.toBe(undefined);
expect(resp.results[1].objectId).not.toBe(undefined);
expect(resp.results[2].objectId).not.toBe(undefined);
});
it_id('b6b42145-7eb4-47aa-ada6-8c1444420e07')(it)('group by empty object', done => {
const obj = new TestObject();
const pipeline = [
{
$group: { _id: {} },
},
];
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results[0].objectId).toEqual(null);
done();
});
});
it_id('0f5f6869-e675-41b9-9ad2-52b201124fb0')(it)('group by empty string', done => {
const obj = new TestObject();
const pipeline = [
{
$group: { _id: '' },
},
];
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results[0].objectId).toEqual(null);
done();
});
});
it_id('b9c4f1b4-47f4-4ff4-88fb-586711f57e4a')(it)('group by empty array', done => {
const obj = new TestObject();
const pipeline = [
{
$group: { _id: [] },
},
];
obj
.save()
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results[0].objectId).toEqual(null);
done();
});
});
it_id('bf5ee3e5-986c-4994-9c8d-79310283f602')(it)('group by multiple columns ', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
const obj3 = new TestObject();
const pipeline = [
{
$group: {
_id: {
score: '$score',
views: '$views',
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(5);
done();
});
});
it_id('3e652c61-78e1-4541-83ac-51ad1def9874')(it)('group by date object', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
const obj3 = new TestObject();
const pipeline = [
{
$group: {
_id: {
day: { $dayOfMonth: '$_updated_at' },
month: { $month: '$_created_at' },
year: { $year: '$_created_at' },
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
const createdAt = new Date(obj1.createdAt);
expect(results[0].objectId.day).toEqual(createdAt.getUTCDate());
expect(results[0].objectId.month).toEqual(createdAt.getUTCMonth() + 1);
expect(results[0].objectId.year).toEqual(createdAt.getUTCFullYear());
done();
});
});
it_id('5d3a0f73-1f49-46f3-9be5-caf1eaefec79')(it)('group by date object transform', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
const obj3 = new TestObject();
const pipeline = [
{
$group: {
_id: {
day: { $dayOfMonth: '$updatedAt' },
month: { $month: '$createdAt' },
year: { $year: '$createdAt' },
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
const createdAt = new Date(obj1.createdAt);
expect(results[0].objectId.day).toEqual(createdAt.getUTCDate());
expect(results[0].objectId.month).toEqual(createdAt.getUTCMonth() + 1);
expect(results[0].objectId.year).toEqual(createdAt.getUTCFullYear());
done();
});
});
it_id('1f9b10f7-dc0e-467f-b506-a303b9c36258')(it)('group by number', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: '$score' },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(2);
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(Object.prototype.hasOwnProperty.call(resp.results[1], 'objectId')).toBe(true);
expect(resp.results.sort((a, b) => (a.objectId > b.objectId ? 1 : -1))).toEqual([
{ objectId: 10 },
{ objectId: 20 },
]);
done();
})
.catch(done.fail);
});
it_id('c7695018-03de-49e4-8a72-d4d956f70deb')(it_exclude_dbs(['postgres']))('group and multiply transform', done => {
const obj1 = new TestObject({ name: 'item a', quantity: 2, price: 10 });
const obj2 = new TestObject({ name: 'item b', quantity: 5, price: 5 });
const pipeline = [
{
$group: {
_id: null,
total: { $sum: { $multiply: ['$quantity', '$price'] } },
},
},
];
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(1);
expect(results[0].total).toEqual(45);
done();
});
});
it_id('2d278175-7594-4b29-bef4-04c778b7a42f')(it_exclude_dbs(['postgres']))('project and multiply transform', done => {
const obj1 = new TestObject({ name: 'item a', quantity: 2, price: 10 });
const obj2 = new TestObject({ name: 'item b', quantity: 5, price: 5 });
const pipeline = [
{
$match: { quantity: { $exists: true } },
},
{
$project: {
name: 1,
total: { $multiply: ['$quantity', '$price'] },
},
},
];
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(2);
if (results[0].name === 'item a') {
expect(results[0].total).toEqual(20);
expect(results[1].total).toEqual(25);
} else {
expect(results[0].total).toEqual(25);
expect(results[1].total).toEqual(20);
}
done();
});
});
it_id('9c9d9318-3a9e-4c2a-8a09-d3aa52c7505b')(it_exclude_dbs(['postgres']))('project without objectId transform', done => {
const obj1 = new TestObject({ name: 'item a', quantity: 2, price: 10 });
const obj2 = new TestObject({ name: 'item b', quantity: 5, price: 5 });
const pipeline = [
{
$match: { quantity: { $exists: true } },
},
{
$project: {
_id: 0,
total: { $multiply: ['$quantity', '$price'] },
},
},
{
$sort: { total: 1 },
},
];
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(2);
expect(results[0].total).toEqual(20);
expect(results[0].objectId).toEqual(undefined);
expect(results[1].total).toEqual(25);
expect(results[1].objectId).toEqual(undefined);
done();
});
});
it_id('f92c82ac-1993-4758-b718-45689dfc4154')(it_exclude_dbs(['postgres']))('project updatedAt only transform', done => {
const pipeline = [
{
$project: { _id: 0, updatedAt: 1 },
},
];
const query = new Parse.Query(TestObject);
query.aggregate(pipeline).then(results => {
expect(results.length).toEqual(4);
for (let i = 0; i < results.length; i++) {
const item = results[i];
expect(Object.prototype.hasOwnProperty.call(item, 'updatedAt')).toEqual(true);
expect(Object.prototype.hasOwnProperty.call(item, 'objectId')).toEqual(false);
}
done();
});
});
it_id('99566b1d-778d-4444-9deb-c398108e659d')(it_exclude_dbs(['postgres']))('can group by any date field (it does not work if you have dirty data)',
done => {
// rows in your collection with non date data in the field that is supposed to be a date
const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) });
const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
const pipeline = [
{
$match: {
dateField2019: { $exists: true },
},
},
{
$group: {
_id: {
day: { $dayOfMonth: '$dateField2019' },
month: { $month: '$dateField2019' },
year: { $year: '$dateField2019' },
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
const counts = results.map(result => result.count);
expect(counts.length).toBe(2);
expect(counts.sort()).toEqual([1, 2]);
done();
})
.catch(done.fail);
}
);
it_only_db('postgres')(
'can group by any date field (it does not work if you have dirty data)', // rows in your collection with non date data in the field that is supposed to be a date
done => {
const obj1 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
const obj2 = new TestObject({ dateField2019: new Date(1990, 5, 1) });
const obj3 = new TestObject({ dateField2019: new Date(1990, 11, 1) });
const pipeline = [
{
$group: {
_id: {
day: { $dayOfMonth: '$dateField2019' },
month: { $month: '$dateField2019' },
year: { $year: '$dateField2019' },
},
count: { $sum: 1 },
},
},
];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
const counts = results.map(result => result.count);
expect(counts.length).toBe(3);
expect(counts.sort()).toEqual([1, 2, 4]);
done();
})
.catch(done.fail);
}
);
it_id('bf3c2704-b721-4b1b-92fa-e1b129ae4aff')(it)('group by pointer', done => {
const pointer1 = new TestObject();
const pointer2 = new TestObject();
const obj1 = new TestObject({ pointer: pointer1 });
const obj2 = new TestObject({ pointer: pointer2 });
const obj3 = new TestObject({ pointer: pointer1 });
const pipeline = [{ $group: { _id: '$pointer' } }];
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(3);
expect(results.some(result => result.objectId === pointer1.id)).toEqual(true);
expect(results.some(result => result.objectId === pointer2.id)).toEqual(true);
expect(results.some(result => result.objectId === null)).toEqual(true);
done();
});
});
it_id('9ee9e8c0-a590-4af9-97a9-4b8e5080ffae')(it)('group sum query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: null, total: { $sum: '$score' } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(resp.results[0].objectId).toBe(null);
expect(resp.results[0].total).toBe(50);
done();
})
.catch(done.fail);
});
it_id('39133cd6-5bdf-4917-b672-a9d7a9157b6f')(it)('group count query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: null, total: { $sum: 1 } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(resp.results[0].objectId).toBe(null);
expect(resp.results[0].total).toBe(4);
done();
})
.catch(done.fail);
});
it_id('48685ff3-066f-4353-82e7-87f39d812ff7')(it)('group min query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: null, minScore: { $min: '$score' } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(resp.results[0].objectId).toBe(null);
expect(resp.results[0].minScore).toBe(10);
done();
})
.catch(done.fail);
});
it_id('581efea6-6525-4e10-96d9-76d32c73e7a9')(it)('group max query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: null, maxScore: { $max: '$score' } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(resp.results[0].objectId).toBe(null);
expect(resp.results[0].maxScore).toBe(20);
done();
})
.catch(done.fail);
});
it_id('5f880de2-b97f-43d1-89b7-ad903a4be4e2')(it)('group avg query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$group: { _id: null, avgScore: { $avg: '$score' } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(Object.prototype.hasOwnProperty.call(resp.results[0], 'objectId')).toBe(true);
expect(resp.results[0].objectId).toBe(null);
expect(resp.results[0].avgScore).toBe(12.5);
done();
})
.catch(done.fail);
});
it_id('58e7a1a0-fae1-4993-b336-7bcbd5b7c786')(it)('limit query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$limit: 2,
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(2);
done();
})
.catch(done.fail);
});
it_id('c892a3d2-8ae8-4b88-bf2b-3c958e1cacd8')(it)('sort ascending query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$sort: { name: 1 },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(4);
expect(resp.results[0].name).toBe('bar');
expect(resp.results[1].name).toBe('dpl');
expect(resp.results[2].name).toBe('foo');
expect(resp.results[3].name).toBe('foo');
done();
})
.catch(done.fail);
});
it_id('79d4bc2e-8b69-42ec-8526-20d17e968ab3')(it)('sort decending query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$sort: { name: -1 },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(4);
expect(resp.results[0].name).toBe('foo');
expect(resp.results[1].name).toBe('foo');
expect(resp.results[2].name).toBe('dpl');
expect(resp.results[3].name).toBe('bar');
done();
})
.catch(done.fail);
});
it_id('b3d97d48-bd6b-444d-be64-cc1fd4738266')(it)('skip query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$skip: 2,
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(2);
done();
})
.catch(done.fail);
});
it_id('4a7daee3-5ba1-4c8b-b406-1846a73a64c8')(it)('match comparison date query', done => {
const today = new Date();
const yesterday = new Date();
const tomorrow = new Date();
yesterday.setDate(today.getDate() - 1);
tomorrow.setDate(today.getDate() + 1);
const obj1 = new TestObject({ dateField: yesterday });
const obj2 = new TestObject({ dateField: today });
const obj3 = new TestObject({ dateField: tomorrow });
const pipeline = [{ $match: { dateField: { $lt: tomorrow } } }];
Parse.Object.saveAll([obj1, obj2, obj3])
.then(() => {
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toBe(2);
done();
});
});
it_id('d98c8c20-6dac-4d74-8228-85a1ae46a7d0')(it)('should aggregate with Date object (directAccess)', async () => {
const rest = require('../lib/rest');
const auth = require('../lib/Auth');
const TestObject = Parse.Object.extend('TestObject');
const date = new Date();
await new TestObject({ date: date }).save(null, { useMasterKey: true });
const config = Config.get(Parse.applicationId);
const resp = await rest.find(
config,
auth.master(config),
'TestObject',
{},
{ pipeline: [{ $match: { date: { $lte: new Date() } } }] }
);
expect(resp.results.length).toBe(1);
});
it_id('3d73d23a-fce1-4ac0-972a-50f6a550f348')(it)('match comparison query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$match: { score: { $gt: 15 } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(1);
expect(resp.results[0].score).toBe(20);
done();
})
.catch(done.fail);
});
it_id('11772059-6c93-41ac-8dfe-e55b6c97e16f')(it)('match multiple comparison query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$match: { score: { $gt: 5, $lt: 15 } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(3);
expect(resp.results[0].score).toBe(10);
expect(resp.results[1].score).toBe(10);
expect(resp.results[2].score).toBe(10);
done();
})
.catch(done.fail);
});
it_id('ca2efb04-8f73-40ca-a5fc-79d0032bc398')(it)('match complex comparison query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$match: { score: { $gt: 5, $lt: 15 }, views: { $gt: 850, $lt: 1000 } },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(1);
expect(resp.results[0].score).toBe(10);
expect(resp.results[0].views).toBe(900);
done();
})
.catch(done.fail);
});
it_id('5ef9dcbe-fe54-4db2-b8fb-58c87c6ff072')(it)('match comparison and equality query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$match: { score: { $gt: 5, $lt: 15 }, views: 900 },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(1);
expect(resp.results[0].score).toBe(10);
expect(resp.results[0].views).toBe(900);
done();
})
.catch(done.fail);
});
it_id('c910a6af-58df-46aa-bbf8-da014a04cdcd')(it)('match $or query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$match: {
$or: [{ score: { $gt: 15, $lt: 25 } }, { views: { $gt: 750, $lt: 850 } }],
},
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
expect(resp.results.length).toBe(2);
// Match score { $gt: 15, $lt: 25 }
expect(resp.results.some(result => result.score === 20)).toEqual(true);
expect(resp.results.some(result => result.views === 700)).toEqual(true);
// Match view { $gt: 750, $lt: 850 }
expect(resp.results.some(result => result.score === 10)).toEqual(true);
expect(resp.results.some(result => result.views === 800)).toEqual(true);
done();
})
.catch(done.fail);
});
it_id('0f768dc2-0675-4e45-a763-5ca9c895fa5f')(it)('match objectId query', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const pipeline = [{ $match: { _id: obj1.id } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(1);
expect(results[0].objectId).toEqual(obj1.id);
done();
});
});
it_id('27349e04-0d9d-453f-ad85-1a811631582d')(it)('match field query', done => {
const obj1 = new TestObject({ name: 'TestObject1' });
const obj2 = new TestObject({ name: 'TestObject2' });
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const pipeline = [{ $match: { name: 'TestObject1' } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(1);
expect(results[0].objectId).toEqual(obj1.id);
done();
});
});
it_id('9222e025-d450-4699-8d5b-c5cf9a64fb24')(it)('match pointer query', done => {
const pointer1 = new PointerObject();
const pointer2 = new PointerObject();
const obj1 = new TestObject({ pointer: pointer1 });
const obj2 = new TestObject({ pointer: pointer2 });
const obj3 = new TestObject({ pointer: pointer1 });
Parse.Object.saveAll([pointer1, pointer2, obj1, obj2, obj3])
.then(() => {
const pipeline = [{ $match: { pointer: pointer1.id } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(2);
expect(results[0].pointer.objectId).toEqual(pointer1.id);
expect(results[1].pointer.objectId).toEqual(pointer1.id);
expect(results.some(result => result.objectId === obj1.id)).toEqual(true);
expect(results.some(result => result.objectId === obj3.id)).toEqual(true);
done();
});
});
it_id('3a1e2cdc-52c7-4060-bc90-b06d557d85ce')(it_exclude_dbs(['postgres']))('match exists query', done => {
const pipeline = [{ $match: { score: { $exists: true } } }];
const query = new Parse.Query(TestObject);
query.aggregate(pipeline).then(results => {
expect(results.length).toEqual(4);
done();
});
});
it_id('0adea3f4-73f7-4b48-a7dd-c764ceb947ec')(it)('match date query - createdAt', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const pipeline = [{ $match: { createdAt: { $gte: today } } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
// Four objects were created initially, we added two more.
expect(results.length).toEqual(6);
done();
});
});
it_id('cdc0eecb-f547-4881-84cc-c06fb46a636a')(it)('match date query - updatedAt', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const pipeline = [{ $match: { updatedAt: { $gte: today } } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
// Four objects were added initially, we added two more.
expect(results.length).toEqual(6);
done();
});
});
it_id('621fe00a-1127-4341-a8e1-fc579b7ed8bd')(it)('match date query - empty', done => {
const obj1 = new TestObject();
const obj2 = new TestObject();
Parse.Object.saveAll([obj1, obj2])
.then(() => {
const now = new Date();
const future = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate());
const pipeline = [{ $match: { createdAt: future } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(0);
done();
});
});
it_id('802ffc99-861b-4b72-90a6-0c666a2e3fd8')(it_exclude_dbs(['postgres']))('match pointer with operator query', done => {
const pointer = new PointerObject();
const obj1 = new TestObject({ pointer });
const obj2 = new TestObject({ pointer });
const obj3 = new TestObject();
Parse.Object.saveAll([pointer, obj1, obj2, obj3])
.then(() => {
const pipeline = [{ $match: { pointer: { $exists: true } } }];
const query = new Parse.Query(TestObject);
return query.aggregate(pipeline);
})
.then(results => {
expect(results.length).toEqual(2);
expect(results[0].pointer.objectId).toEqual(pointer.id);
expect(results[1].pointer.objectId).toEqual(pointer.id);
expect(results.some(result => result.objectId === obj1.id)).toEqual(true);
expect(results.some(result => result.objectId === obj2.id)).toEqual(true);
done();
});
});
it_id('28090280-7c3e-47f8-8bf6-bebf8566a36c')(it_exclude_dbs(['postgres']))('match null values', async () => {
const obj1 = new Parse.Object('MyCollection');
obj1.set('language', 'en');
obj1.set('otherField', 1);
const obj2 = new Parse.Object('MyCollection');
obj2.set('language', 'en');
obj2.set('otherField', 2);
const obj3 = new Parse.Object('MyCollection');
obj3.set('language', null);
obj3.set('otherField', 3);
const obj4 = new Parse.Object('MyCollection');
obj4.set('language', null);
obj4.set('otherField', 4);
const obj5 = new Parse.Object('MyCollection');
obj5.set('language', 'pt');
obj5.set('otherField', 5);
const obj6 = new Parse.Object('MyCollection');
obj6.set('language', 'pt');
obj6.set('otherField', 6);
await Parse.Object.saveAll([obj1, obj2, obj3, obj4, obj5, obj6]);
expect(
(
await new Parse.Query('MyCollection').aggregate([
{
$match: {
language: { $in: [null, 'en'] },
},
},
])
)
.map(value => value.otherField)
.sort()
).toEqual([1, 2, 3, 4]);
expect(
(
await new Parse.Query('MyCollection').aggregate([
{
$match: {
$or: [{ language: 'en' }, { language: null }],
},
},
])
)
.map(value => value.otherField)
.sort()
).toEqual([1, 2, 3, 4]);
});
it_id('df63d1f5-7c37-4ed9-8bc5-20d82f29f509')(it)('project query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$project: { name: 1 },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
resp.results.forEach(result => {
expect(result.objectId).not.toBe(undefined);
expect(result.name).not.toBe(undefined);
expect(result.sender).toBe(undefined);
expect(result.size).toBe(undefined);
expect(result.score).toBe(undefined);
});
done();
})
.catch(done.fail);
});
it_id('69224bbb-8ea0-4ab4-af23-398b6432f668')(it)('multiple project query', done => {
const options = Object.assign({}, masterKeyOptions, {
body: {
$project: { name: 1, score: 1, sender: 1 },
},
});
get(Parse.serverURL + '/aggregate/TestObject', options)
.then(resp => {
resp.results.forEach(result => {
expect(result.objectId).not.toBe(undefined);
expect(result.name).not.toBe(undefined);
expect(result.score).not.toBe(undefined);
expect(result.sender).not.toBe(undefined);
expect(result.size).toBe(undefined);
});
done();
})
.catch(done.fail);
});
it_id('97ce4c7c-8d9f-4ffd-9352-394bc9867bab')(it)('project pointer query', done => {
const pointer = new PointerObject();
const obj = new TestObject({ pointer, name: 'hello' });