forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfflineQuery-test.js
915 lines (762 loc) · 31.5 KB
/
OfflineQuery-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
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
jest.autoMockOff();
const matchesQuery = require('../OfflineQuery').matchesQuery;
const validateQuery = require('../OfflineQuery').validateQuery;
const ParseError = require('../ParseError').default;
const ParseObject = require('../ParseObject').default;
const ParseQuery = require('../ParseQuery').default;
const ParseGeoPoint = require('../ParseGeoPoint').default;
const ParsePolygon = require('../ParsePolygon').default;
const ParseUser = require('../ParseUser').default;
describe('OfflineQuery', () => {
it('matches blank queries', () => {
const obj = new ParseObject('Item');
const q = new ParseQuery('Item');
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
expect(matchesQuery(q.className, obj._toFullJSON(), [], q.toJSON().where)).toBe(true);
});
it('can handle unknown query', () => {
const obj = new ParseObject('Item');
const q = new ParseQuery('Item');
q._addCondition('key', 'unknown', 'value');
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
expect(matchesQuery(q.className, obj._toFullJSON(), [], q.toJSON().where)).toBe(false);
});
it('matches queries null field', () => {
const obj = new ParseObject('Item');
const q = new ParseQuery('Item');
q.equalTo('field', null);
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
});
it('matches queries invalid key', () => {
const obj = new ParseObject('Item');
const q = new ParseQuery('Item');
q.equalTo('$foo', 'bar');
try {
matchesQuery(q.className, obj, [], q);
} catch (e) {
expect(e.message).toBe('Invalid Key: $foo');
}
});
it('matches queries date field', () => {
const date = new Date();
const obj = new ParseObject('Item');
obj.set('field', date);
const q = new ParseQuery('Item');
q.greaterThanOrEqualTo('field', date);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
});
it('matches queries relativeTime date field', () => {
const now = Date.now();
const obj = new ParseObject('Item', {
name: 'obj1',
ttl: new Date(now + 2 * 24 * 60 * 60 * 1000), // 2 days from now
});
let q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: 'in 1 day' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: '1 day ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.lessThan('ttl', { $relativeTime: '5 days ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q = new ParseQuery('Item');
q.lessThan('ttl', { $relativeTime: '0 yr 0 wk 5 d 0 hr 0 min 0 sec ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: 'now' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: 'now' });
q.lessThan('ttl', { $relativeTime: 'in 3 day' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: '1 years 3 weeks 1 hours 3 minutes 2 seconds ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: '2 year 2 week 1 hour 4 day 3 minute 2 second ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: '2 yrs 2 wks 1 hrs 3 mins 2 secs ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.greaterThan('ttl', { $relativeTime: '0 yr 0 wk 0 d 24 hr 3 min 2 sec ago' });
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
});
it('matches invalid queries relativeTime date field', () => {
const date = new Date('1995-12-17T03:24:00');
const obj = new ParseObject('Item');
obj.set('field', date);
const q = new ParseQuery('Item');
q.equalTo('field', { $relativeTime: 'in 0 day' });
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
});
it('handles invalid queries relativeTime date errors', () => {
const obj = new ParseObject('Item');
obj.set('field', new Date('1995-12-17T03:24:00'));
const q = new ParseQuery('Item');
q.greaterThan('field', { $relativeTime: '1 unknown ago' });
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
"bad $relativeTime (field) value. Invalid interval: 'unknown'"
);
q.greaterThan('field', { $relativeTime: 'in 1 ago' });
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
"bad $relativeTime (field) value. Time cannot have both 'in' and 'ago'"
);
q.greaterThan('field', { $relativeTime: 'ago 1 in' });
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
"bad $relativeTime (field) value. Time should either start with 'in' or end with 'ago'"
);
q.greaterThan('field', { $relativeTime: '1 ago' });
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
'bad $relativeTime (field) value. Invalid time string. Dangling unit or number.'
);
q.greaterThan('field', { $relativeTime: 'in N/A days' });
expect(() => matchesQuery(q.className, obj, [], q)).toThrow(
"bad $relativeTime (field) value. 'n/a' is not an integer."
);
});
it('matches queries relation', () => {
const obj = new ParseObject('Item');
const relation = obj.relation('author');
const q = relation.query();
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
});
it('matches existence queries', () => {
const obj = new ParseObject('Item');
obj.set('count', 100);
const q = new ParseQuery('Item');
q.exists('count');
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q.exists('name');
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
});
it('matches queries with doesNotExist constraint', () => {
const obj = new ParseObject('Item');
obj.set('count', 100);
let q = new ParseQuery('Item');
q.doesNotExist('name');
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Item');
q.doesNotExist('count');
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
});
it('matches on equality queries', () => {
const day = new Date();
const location = new ParseGeoPoint({
latitude: 37.484815,
longitude: -122.148377,
});
const obj = new ParseObject('Person');
obj.set('score', 12).set('name', 'Bill').set('birthday', day).set('lastLocation', location);
let q = new ParseQuery('Person');
q.equalTo('score', 12);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Person');
q.equalTo('name', 'Bill');
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q.equalTo('name', 'Jeff');
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q = new ParseQuery('Person');
q.containedIn('name', ['Adam', 'Ben', 'Charles']);
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q.containedIn('name', ['Adam', 'Bill', 'Charles']);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Person');
q.notContainedIn('name', ['Adam', 'Bill', 'Charles']);
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q.notContainedIn('name', ['Adam', 'Ben', 'Charles']);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q = new ParseQuery('Person');
q.equalTo('birthday', day);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q.equalTo('birthday', new Date(1990, 1));
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q = new ParseQuery('Person');
q.equalTo('lastLocation', location);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q.equalTo(
'lastLocation',
new ParseGeoPoint({
latitude: 37.4848,
longitude: -122.1483,
})
);
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
q.equalTo(
'lastLocation',
new ParseGeoPoint({
latitude: 37.484815,
longitude: -122.148377,
})
);
q.equalTo('score', 12);
q.equalTo('name', 'Bill');
q.equalTo('birthday', day);
expect(matchesQuery(q.className, obj, [], q)).toBe(true);
q.equalTo('name', 'bill');
expect(matchesQuery(q.className, obj, [], q)).toBe(false);
let img = new ParseObject('Image');
img.set('tags', ['nofilter', 'latergram', 'tbt']);
q = new ParseQuery('Image');
q.equalTo('tags', 'selfie');
expect(matchesQuery(q.className, img, [], q)).toBe(false);
q.equalTo('tags', 'tbt');
expect(matchesQuery(q.className, img, [], q)).toBe(true);
const q2 = new ParseQuery('Image');
q2.containsAll('tags', ['latergram', 'nofilter']);
expect(matchesQuery(q.className, img, [], q2)).toBe(true);
q2.containsAll('tags', ['latergram', 'selfie']);
expect(matchesQuery(q.className, img, [], q2)).toBe(false);
const u = new ParseUser();
u.id = 'U2';
q = new ParseQuery('Image');
q.equalTo('owner', u);
img = new ParseObject('Image');
img.set('owner', u);
expect(matchesQuery(q.className, img, [], q)).toBe(true);
let json = img.toJSON();
json.owner.objectId = 'U3';
expect(matchesQuery(json, q)).toBe(false);
// pointers in arrays
q = new ParseQuery('Image');
q.equalTo('owners', u);
img = new ParseObject('Image');
img.set('owners', [u]);
expect(matchesQuery(q.className, img, [], q)).toBe(true);
json = img.toJSON();
json.owners[0].objectId = 'U3';
expect(matchesQuery(json, q)).toBe(false);
const u3 = new ParseUser();
u3.id = 'U3';
img = new ParseObject('Image');
img.set('owners', [u3]);
expect(matchesQuery(q.className, img, [], q)).toBe(false);
});
it('matches on inequalities', () => {
const player = new ParseObject('Person');
player
.set('score', 12)
.set('name', 'Bill')
.set('birthday', new Date(1980, 2, 4));
let q = new ParseQuery('Person');
q.lessThan('score', 15);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.lessThan('score', 10);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q = new ParseQuery('Person');
q.lessThanOrEqualTo('score', 15);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.lessThanOrEqualTo('score', 12);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.lessThanOrEqualTo('score', 10);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q = new ParseQuery('Person');
q.greaterThan('score', 15);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q.greaterThan('score', 10);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q = new ParseQuery('Person');
q.greaterThanOrEqualTo('score', 15);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q.greaterThanOrEqualTo('score', 12);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.greaterThanOrEqualTo('score', 10);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q = new ParseQuery('Person');
q.notEqualTo('score', 12);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q.notEqualTo('score', 40);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
});
it('matches an $or query', () => {
const player = new ParseObject('Player');
player.set('score', 12).set('name', 'Player 1');
const q = new ParseQuery('Player');
q.equalTo('name', 'Player 1');
const q2 = new ParseQuery('Player');
q2.equalTo('name', 'Player 2');
const q3 = new ParseQuery('Player');
q3.equalTo('name', 'Player 3');
const orQuery1 = ParseQuery.or(q, q2);
const orQuery2 = ParseQuery.or(q2, q3);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
expect(matchesQuery(q.className, player, [], q2)).toBe(false);
expect(matchesQuery(q.className, player, [], orQuery1)).toBe(true);
expect(matchesQuery(q.className, player, [], orQuery2)).toBe(false);
});
it('matches an $and query', () => {
const player = new ParseObject('Player');
player.set('score', 12).set('name', 'Player 1');
const q = new ParseQuery('Player');
q.equalTo('name', 'Player 1');
const q2 = new ParseQuery('Player');
q2.equalTo('score', 12);
const q3 = new ParseQuery('Player');
q3.equalTo('score', 100);
const andQuery1 = ParseQuery.and(q, q2);
const andQuery2 = ParseQuery.and(q, q3);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
expect(matchesQuery(q.className, player, [], q2)).toBe(true);
expect(matchesQuery(q.className, player, [], andQuery1)).toBe(true);
expect(matchesQuery(q.className, player, [], andQuery2)).toBe(false);
});
it('matches an $nor query', () => {
const player = new ParseObject('Player');
player.set('score', 12).set('name', 'Player 1');
const q = new ParseQuery('Player');
q.equalTo('name', 'Player 1');
const q2 = new ParseQuery('Player');
q2.equalTo('name', 'Player 2');
const q3 = new ParseQuery('Player');
q3.equalTo('name', 'Player 3');
const norQuery1 = ParseQuery.nor(q, q2);
const norQuery2 = ParseQuery.nor(q2, q3);
expect(matchesQuery(q.className, player, [], q)).toBe(true);
expect(matchesQuery(q.className, player, [], q2)).toBe(false);
expect(matchesQuery(q.className, player, [], q3)).toBe(false);
expect(matchesQuery(q.className, player, [], norQuery1)).toBe(false);
expect(matchesQuery(q.className, player, [], norQuery2)).toBe(true);
});
it('matches $regex queries', () => {
const player = new ParseObject('Player');
player.set('score', 12).set('name', 'Player 1');
let q = new ParseQuery('Player');
q.startsWith('name', 'Play');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.startsWith('name', 'Ploy');
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q = new ParseQuery('Player');
q.endsWith('name', ' 1');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.endsWith('name', ' 2');
expect(matchesQuery(q.className, player, [], q)).toBe(false);
// Check that special characters are escaped
player.set('name', 'Android-7');
q = new ParseQuery('Player');
q.contains('name', 'd-7');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q = new ParseQuery('Player');
q.matches('name', /A.d/, 'm');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.matches('name', /A[^n]d/);
expect(matchesQuery(q.className, player, [], q)).toBe(false);
// Check that the string \\E is returned to normal
player.set('name', 'Slash \\E');
q = new ParseQuery('Player');
q.endsWith('name', 'h \\E');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.endsWith('name', 'h \\Ee');
expect(matchesQuery(q.className, player, [], q)).toBe(false);
player.set('name', 'Slash \\Q and more');
q = new ParseQuery('Player');
q.contains('name', 'h \\Q and');
expect(matchesQuery(q.className, player, [], q)).toBe(true);
q.contains('name', 'h \\Q or');
expect(matchesQuery(q.className, player, [], q)).toBe(false);
q = new ParseQuery('Player');
q._addCondition('status', '$regex', {
test: function () {
return true;
},
});
expect(matchesQuery(q.className, player, [], q)).toBe(true);
});
it('matches $nearSphere queries', () => {
let q = new ParseQuery('Checkin');
q.near('location', new ParseGeoPoint(20, 20));
// With no max distance, any GeoPoint is 'near'
const pt = new ParseObject('Checkin');
pt.set('location', new ParseGeoPoint(40, 40));
const ptUndefined = new ParseObject('Checkin');
const ptNull = new ParseObject('Checkin');
ptNull.set('location', null);
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
expect(matchesQuery(q.className, ptUndefined, [], q)).toBe(false);
expect(matchesQuery(q.className, ptNull, [], q)).toBe(false);
q = new ParseQuery('Checkin');
pt.set('location', new ParseGeoPoint(40, 40));
q.withinRadians('location', new ParseGeoPoint(30, 30), 0.3);
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
q.withinRadians('location', new ParseGeoPoint(30, 30), 0.2);
expect(matchesQuery(q.className, pt, [], q)).toBe(false);
q = new ParseQuery('Checkin');
q._addCondition('location', '$maxDistance', 100);
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
});
it('matches $centerSphere queries', () => {
const pt = new ParseObject('Checkin');
pt.set('location', new ParseGeoPoint(40, 40));
const q = new ParseQuery('Checkin');
q.withinRadians('location', new ParseGeoPoint(30, 30), 0.3, false);
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
});
it('matches $within queries', () => {
const caltrainStation = new ParseObject('Checkin');
caltrainStation
.set('name', 'Caltrain')
.set('location', new ParseGeoPoint(37.776346, -122.394218));
const santaClara = new ParseObject('Checkin');
santaClara
.set('name', 'Santa Clara')
.set('location', new ParseGeoPoint(37.325635, -121.945753));
const noLocation = new ParseObject('Checkin');
noLocation.set('name', 'Santa Clara');
const nullLocation = new ParseObject('Checkin');
nullLocation.set('name', 'Santa Clara').set('location', null);
let q = new ParseQuery('Checkin').withinGeoBox(
'location',
new ParseGeoPoint(37.708813, -122.526398),
new ParseGeoPoint(37.822802, -122.373962)
);
expect(matchesQuery(q.className, caltrainStation, [], q)).toBe(true);
expect(matchesQuery(q.className, santaClara, [], q)).toBe(false);
expect(matchesQuery(q.className, noLocation, [], q)).toBe(false);
expect(matchesQuery(q.className, nullLocation, [], q)).toBe(false);
// Invalid rectangles
q = new ParseQuery('Checkin').withinGeoBox(
'location',
new ParseGeoPoint(37.822802, -122.373962),
new ParseGeoPoint(37.708813, -122.526398)
);
expect(matchesQuery(q.className, caltrainStation, [], q)).toBe(false);
expect(matchesQuery(q.className, santaClara, [], q)).toBe(false);
q = new ParseQuery('Checkin').withinGeoBox(
'location',
new ParseGeoPoint(37.708813, -122.373962),
new ParseGeoPoint(37.822802, -122.526398)
);
expect(matchesQuery(q.className, caltrainStation, [], q)).toBe(false);
expect(matchesQuery(q.className, santaClara, [], q)).toBe(false);
});
it('matches on subobjects with dot notation', () => {
const message = new ParseObject('Message');
message.set('test', 'content').set('status', { x: 'read', y: 'delivered' });
let q = new ParseQuery('Message');
q.equalTo('status.x', 'read');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.equalTo('status.z', 'read');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.equalTo('status.x', 'delivered');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.notEqualTo('status.x', 'read');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.notEqualTo('status.z', 'read');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.notEqualTo('status.x', 'delivered');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.exists('status.x');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.exists('status.z');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.exists('nonexistent.x');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.doesNotExist('status.x');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q.doesNotExist('status.z');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.doesNotExist('nonexistent.z');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.equalTo('status.x', 'read');
q.doesNotExist('status.y');
expect(matchesQuery(q.className, message, [], q)).toBe(false);
q = new ParseQuery('Message');
q._addCondition('status', '$exists', 'invalid');
expect(matchesQuery(q.className, message, [], q)).toBe(true);
});
it('should support containedIn with pointers', () => {
const profile = new ParseObject('Profile');
profile.id = 'abc';
const message = new ParseObject('Message');
message.set('profile', profile);
let q = new ParseQuery('Message');
q.containedIn('profile', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'abc' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'def' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.containedIn('profile', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'ghi' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'def' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});
it('should support containedIn with array of pointers', () => {
const profile1 = new ParseObject('Profile');
profile1.id = 'yeahaw';
const profile2 = new ParseObject('Profile');
profile2.id = 'yes';
const message = new ParseObject('Message');
message.id = 'O2';
message.set('profiles', [profile1, profile2]);
let q = new ParseQuery('Message');
q.containedIn('profiles', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'no' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'yes' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.containedIn('profiles', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'no' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'nope' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});
it('should support notContainedIn with pointers', () => {
const profile = new ParseObject('Profile');
profile.id = 'abc';
let message = new ParseObject('Message');
message.id = 'O1';
message.set('profile', profile);
let q = new ParseQuery('Message');
q.notContainedIn('profile', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'def' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'ghi' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(true);
profile.id = 'def';
message = new ParseObject('Message');
message.set('profile', profile);
q = new ParseQuery('Message');
q.notContainedIn('profile', [
ParseObject.fromJSON({ className: 'Profile', objectId: 'ghi' }),
ParseObject.fromJSON({ className: 'Profile', objectId: 'def' }),
]);
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});
it('should support containedIn queries with [objectId]', () => {
const profile = new ParseObject('Profile');
profile.id = 'abc';
let message = new ParseObject('Message');
message.set('profile', profile);
let q = new ParseQuery('Message');
q.containedIn('profile', ['abc', 'def']);
expect(matchesQuery(q.className, message, [], q)).toBe(true);
profile.id = 'ghi';
message = new ParseObject('Message');
message.set('profile', profile);
q = new ParseQuery('Message');
q.containedIn('profile', ['abc', 'def']);
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});
it('should support notContainedIn queries with [objectId]', () => {
const profile = new ParseObject('Profile');
profile.id = 'ghi';
const message = new ParseObject('Message');
message.set('profile', profile);
let q = new ParseQuery('Message');
q.notContainedIn('profile', ['abc', 'def']);
expect(matchesQuery(q.className, message, [], q)).toBe(true);
q = new ParseQuery('Message');
q.notContainedIn('profile', ['abc', 'def', 'ghi']);
expect(matchesQuery(q.className, message, [], q)).toBe(false);
});
it('should support matchesKeyInQuery', () => {
const restaurant = new ParseObject('Restaurant');
restaurant.set('ratings', 5);
restaurant.set('location', 'Earth');
const person1 = new ParseObject('Person');
person1.set('hometown', 'Earth');
const person2 = new ParseObject('Person');
person2.set('hometown', 'Mars');
let query = new ParseQuery('Restaurant');
query.greaterThan('rating', 4);
let mainQuery = new ParseQuery('Person');
mainQuery.matchesKeyInQuery('hometown', 'location', query);
expect(
matchesQuery(mainQuery.className, person1, [person1, person2, restaurant], mainQuery)
).toBe(true);
expect(
matchesQuery(mainQuery.className, person2, [person1, person2, restaurant], mainQuery)
).toBe(false);
expect(matchesQuery(mainQuery.className, person1, [], mainQuery)).toBe(false);
query = new ParseQuery('Restaurant');
query.greaterThan('rating', 4);
mainQuery = new ParseQuery('Person');
mainQuery.doesNotMatchKeyInQuery('hometown', 'location', query);
expect(
matchesQuery(
mainQuery.className,
person1,
[person1, person2, restaurant._toFullJSON()],
mainQuery
)
).toBe(false);
expect(
matchesQuery(mainQuery.className, person2, [person1, person2, restaurant], mainQuery)
).toBe(true);
expect(matchesQuery(mainQuery.className, person1, [], mainQuery)).toBe(false);
});
it('should support matchesQuery', () => {
const parentObjects = [];
const childObjects = [];
for (let i = 0; i < 10; i += 1) {
const child = new ParseObject('ChildObject');
child.id = 100 + i;
child.set('x', i);
const parent = new ParseObject('ParentObject');
parent.id = 10 + i;
parent.set('child', child);
childObjects.push(child);
parentObjects.push(parent);
}
let subQuery = new ParseQuery('ChildObject');
subQuery.greaterThan('x', 5);
let q = new ParseQuery('ParentObject');
q.matchesQuery('child', subQuery);
expect(
matchesQuery(q.className, parentObjects[0], [...parentObjects, ...childObjects], q)
).toBe(false);
expect(
matchesQuery(q.className, parentObjects[9], [...parentObjects, ...childObjects], q)
).toBe(true);
expect(matchesQuery(q.className, parentObjects[0], [], q)).toBe(false);
subQuery = new ParseQuery('ChildObject');
subQuery.greaterThan('x', 5);
q = new ParseQuery('ParentObject');
q.doesNotMatchQuery('child', subQuery);
expect(
matchesQuery(q.className, parentObjects[0], [...parentObjects, ...childObjects], q)
).toBe(true);
expect(
matchesQuery(q.className, parentObjects[9], [...parentObjects, ...childObjects], q)
).toBe(false);
expect(matchesQuery(q.className, parentObjects[0], [], q)).toBe(true);
});
it('should support containedBy query', () => {
const obj1 = new ParseObject('Numbers');
const obj2 = new ParseObject('Numbers');
const obj3 = new ParseObject('Numbers');
obj1.set('numbers', [0, 1, 2]);
obj2.set('numbers', [2, 0]);
obj3.set('numbers', [1, 2, 3, 4]);
const q = new ParseQuery('Numbers');
q.containedBy('numbers', [1, 2, 3, 4, 5]);
expect(matchesQuery(q.className, obj1, [], q)).toBe(false);
expect(matchesQuery(q.className, obj2, [], q)).toBe(false);
expect(matchesQuery(q.className, obj3, [], q)).toBe(true);
});
it('should support withinPolygon query', () => {
const sacramento = new ParseObject('Location');
sacramento.set('location', new ParseGeoPoint(38.52, -121.5));
sacramento.set('name', 'Sacramento');
const honolulu = new ParseObject('Location');
honolulu.set('location', new ParseGeoPoint(21.35, -157.93));
honolulu.set('name', 'Honolulu');
const sf = new ParseObject('Location');
sf.set('location', new ParseGeoPoint(37.75, -122.68));
sf.set('name', 'San Francisco');
const points = [
new ParseGeoPoint(37.85, -122.33),
new ParseGeoPoint(37.85, -122.9),
new ParseGeoPoint(37.68, -122.9),
new ParseGeoPoint(37.68, -122.33),
];
const q = new ParseQuery('Location');
q.withinPolygon('location', points);
expect(matchesQuery(q.className, sacramento, [], q)).toBe(false);
expect(matchesQuery(q.className, honolulu, [], q)).toBe(false);
expect(matchesQuery(q.className, sf, [], q)).toBe(true);
});
it('should support polygonContains query', () => {
const p1 = [
[0, 0],
[0, 1],
[1, 1],
[1, 0],
];
const p2 = [
[0, 0],
[0, 2],
[2, 2],
[2, 0],
];
const p3 = [
[10, 10],
[10, 15],
[15, 15],
[15, 10],
[10, 10],
];
const polygon1 = new ParsePolygon(p1);
const polygon2 = new ParsePolygon(p2);
const polygon3 = new ParsePolygon(p3);
const obj1 = new ParseObject('Bounds');
const obj2 = new ParseObject('Bounds');
const obj3 = new ParseObject('Bounds');
obj1.set('polygon', polygon1);
obj2.set('polygon', polygon2);
obj3.set('polygon', polygon3);
const point = new ParseGeoPoint(0.5, 0.5);
const q = new ParseQuery('Bounds');
q.polygonContains('polygon', point);
expect(matchesQuery(q.className, obj1, [], q)).toBe(true);
expect(matchesQuery(q.className, obj2, [], q)).toBe(true);
expect(matchesQuery(q.className, obj3, [], q)).toBe(false);
});
it('should not support invalid $geoWithin query', () => {
const sacramento = new ParseObject('Location');
sacramento.set('location', new ParseGeoPoint(38.52, -121.5));
sacramento.set('name', 'Sacramento');
const honolulu = new ParseObject('Location');
honolulu.set('location', new ParseGeoPoint(21.35, -157.93));
honolulu.set('name', 'Honolulu');
const sf = new ParseObject('Location');
sf.set('location', new ParseGeoPoint(37.75, -122.68));
sf.set('name', 'San Francisco');
const points = [
new ParseGeoPoint(37.85, -122.33),
new ParseGeoPoint(37.85, -122.9),
new ParseGeoPoint(37.68, -122.9),
new ParseGeoPoint(37.68, -122.33),
];
const q = new ParseQuery('Location');
q._addCondition('location', '$geoWithin', { $unknown: points });
expect(matchesQuery(q.className, sacramento, [], q)).toBe(false);
expect(matchesQuery(q.className, honolulu, [], q)).toBe(false);
expect(matchesQuery(q.className, sf, [], q)).toBe(false);
});
it('should validate query', () => {
let query = new ParseQuery('TestObject');
query.equalTo('foo', 'bar');
try {
validateQuery(query);
validateQuery(query.toJSON());
query.matches('myString', 'football', 'm');
validateQuery(query);
expect(true).toBe(true);
} catch (_) {
// Should not reach here
expect(false).toEqual(true);
}
query = new ParseQuery('TestObject');
query.matches('myString', 'football', 'some invalid thing');
try {
validateQuery(query);
expect(true).toBe(false);
} catch (e) {
expect(e.code).toEqual(ParseError.INVALID_QUERY);
}
query = new ParseQuery('TestObject');
query.equalTo('$foo', 'bar');
try {
validateQuery(query);
expect(true).toBe(false);
} catch (e) {
expect(e.code).toEqual(ParseError.INVALID_KEY_NAME);
}
});
});