-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathquery_e2e.dart
More file actions
790 lines (655 loc) · 22.2 KB
/
Copy pathquery_e2e.dart
File metadata and controls
790 lines (655 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:firebase_core_platform_interface/firebase_core_platform_interface.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
void setupQueryTests() {
group('Query', () {
late DatabaseReference ref;
setUp(() async {
ref = FirebaseDatabase.instance.ref('tests');
// Wipe the database before each test
await ref.remove();
});
group('startAt', () {
test('returns null when no order modifier is applied', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
});
final snapshot = await ref.startAt(2).get();
expect(snapshot.value, isNull);
});
test(
'streams respect orderByChild with numeric startAt',
() async {
await ref.set({
't1': {'timestamp': 1, 'value': 'old'},
't2': {'timestamp': 1000, 'value': 'current'},
});
final events = await ref
.orderByChild('timestamp')
.startAt(1000)
.onChildAdded
.take(1)
.toList();
expect(events.single.snapshot.key, 't2');
expect(events.single.snapshot.child('value').value, 'current');
},
);
test(
'onValue with startAt(value, key) and no orderBy should not crash',
() async {
await ref.set({
't1': {'timestamp': 1, 'value': 'old'},
't2': {'timestamp': 1000, 'value': 'current'},
});
final event = await ref.startAt(1000, key: 't2').onValue.first;
expect(event.type, DatabaseEventType.value);
},
);
test('starts at the correct value', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
'd': 4,
});
final snapshot = await ref.orderByValue().startAt(2).get();
final expected = ['b', 'c', 'd'];
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('startAfter', () {
test('returns null when no order modifier is applied', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
});
final snapshot = await ref.startAfter(2).get();
expect(snapshot.value, isNull);
});
test('starts after the correct value', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
'd': 4,
});
// TODO(ehesp): Using `get` returns the wrong results. Have flagged with SDK team.
final e = await ref.orderByValue().startAfter(2).once();
final expected = ['c', 'd'];
expect(e.snapshot.children.length, expected.length);
e.snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('endAt', () {
test('returns all values when no order modifier is applied', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
});
final expected = ['a', 'b', 'c'];
final snapshot = await ref.endAt(2).get();
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
test('ends at the correct value', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
'd': 4,
});
final snapshot = await ref.orderByValue().endAt(2).get();
final expected = ['a', 'b'];
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('endBefore', () {
test('returns all values when no order modifier is applied', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
});
final expected = ['a', 'b', 'c'];
final snapshot = await ref.endBefore(2).get();
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
test('ends before the correct value', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
'd': 4,
});
final snapshot = await ref.orderByValue().endBefore(2).get();
final expected = ['a'];
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('equalTo', () {
test('returns null when no order modifier is applied', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
});
final snapshot = await ref.equalTo(2).get();
expect(snapshot.value, isNull);
});
test('returns the correct value', () async {
await ref.set({
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 2,
});
final snapshot = await ref.orderByValue().equalTo(2).get();
final expected = ['b', 'e'];
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('limitToFirst', () {
test('returns a limited array', () async {
await ref.set({
0: 'foo',
1: 'bar',
2: 'baz',
});
final snapshot = await ref.limitToFirst(2).get();
final expected = ['foo', 'bar'];
expect(snapshot.value, equals(expected));
});
test('returns a limited object', () async {
await ref.set({
'a': 'foo',
'b': 'bar',
'c': 'baz',
});
final snapshot = await ref.limitToFirst(2).get();
final expected = {
'a': 'foo',
'b': 'bar',
};
expect(snapshot.value, equals(expected));
});
test('returns null when no limit is possible', () async {
await ref.set('foo');
final snapshot = await ref.limitToFirst(2).get();
expect(snapshot.value, isNull);
});
test('streams emit limited maps', () async {
await ref.set({
'a': 'foo',
'b': 'bar',
'c': 'baz',
});
final event = await ref.orderByKey().limitToFirst(2).onValue.first;
expect(
event.snapshot.value,
equals({
'a': 'foo',
'b': 'bar',
}),
);
});
});
group('limitToLast', () {
test('returns a limited array', () async {
await ref.set({
0: 'foo',
1: 'bar',
2: 'baz',
});
final snapshot = await ref.limitToLast(2).get();
final expected = [null, 'bar', 'baz'];
expect(snapshot.value, equals(expected));
});
test('returns a limited object', () async {
await ref.set({
'a': 'foo',
'b': 'bar',
'c': 'baz',
});
final snapshot = await ref.limitToLast(2).get();
final expected = {
'b': 'bar',
'c': 'baz',
};
expect(snapshot.value, equals(expected));
});
test('returns null when no limit is possible', () async {
await ref.set('foo');
final snapshot = await ref.limitToLast(2).get();
expect(snapshot.value, isNull);
});
test('streams emit limited maps', () async {
await ref.set({
'a': 'foo',
'b': 'bar',
'c': 'baz',
});
final event = await ref.orderByKey().limitToLast(2).onValue.first;
expect(
event.snapshot.value,
equals({
'b': 'bar',
'c': 'baz',
}),
);
});
});
group('orderByChild', () {
test('orders by a child value', () async {
await ref.set({
'a': {
'string': 'foo',
'number': 10,
},
'b': {
'string': 'bar',
'number': 5,
},
'c': {
'string': 'baz',
'number': 8,
},
});
final snapshot = await ref.orderByChild('number').get();
final expected = ['b', 'c', 'a'];
expect(snapshot.children.length, equals(expected.length));
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('orderByKey', () {
test('orders by a key', () async {
await ref.set({
'b': {
'string': 'bar',
'number': 5,
},
'a': {
'string': 'foo',
'number': 10,
},
'c': {
'string': 'baz',
'number': 8,
},
});
final snapshot = await ref.orderByKey().get();
final expected = ['a', 'b', 'c'];
expect(snapshot.children.length, expected.length);
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('orderByPriority', () {
test('orders by priority', () async {
await ref.set({
'a': {
'string': 'foo',
'number': 10,
},
'b': {
'string': 'bar',
'number': 5,
},
'c': {
'string': 'baz',
'number': 8,
},
});
await Future.wait([
ref.child('a').setPriority(2),
ref.child('b').setPriority(3),
ref.child('c').setPriority(1),
]);
final snapshot = await ref.orderByPriority().get();
final expected = ['c', 'a', 'b'];
expect(snapshot.children.length, equals(expected.length));
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('orderByValue', () {
test('orders by a value', () async {
await ref.set({
'a': 2,
'b': 3,
'c': 1,
});
await Future.wait([
ref.child('a').setPriority(2),
ref.child('b').setPriority(3),
ref.child('c').setPriority(1),
]);
final snapshot = await ref.orderByValue().get();
final expected = ['c', 'a', 'b'];
expect(snapshot.children.length, equals(expected.length));
snapshot.children.toList().forEachIndexed((i, childSnapshot) {
expect(childSnapshot.key, expected[i]);
});
});
});
group('onChildAdded', () {
test(
'emits an event when a child is added',
() async {
// Set data first, then subscribe. onChildAdded fires for
// existing children on initial listen, avoiding race conditions
// with native listener registration.
// Use keys that sort alphabetically in the expected order,
// since onChildAdded returns children in key order.
await ref.child('a_first').set('foo');
await ref.child('b_second').set('bar');
final events = await ref.onChildAdded.take(2).toList();
expect(events[0].snapshot.value, 'foo');
expect(events[0].type, DatabaseEventType.childAdded);
expect(events[1].snapshot.value, 'bar');
expect(events[1].type, DatabaseEventType.childAdded);
},
);
});
group('onChildRemoved', () {
test(
'emits an event when a child is removed',
() async {
await ref.child('foo').set('foo');
await ref.child('bar').set('bar');
final completer = Completer<DatabaseEvent>();
final subscription = ref.onChildRemoved.listen((event) {
// Skip probe events used for listener registration
if (event.snapshot.key == '__probe__') return;
if (!completer.isCompleted) completer.complete(event);
});
// Wait for native listener registration by doing a round-trip
await ref.child('__probe__').set(true);
await ref.child('__probe__').remove();
await ref.child('bar').remove();
final event =
await completer.future.timeout(const Duration(seconds: 10));
expect(event.snapshot.value, 'bar');
expect(event.type, DatabaseEventType.childRemoved);
await subscription.cancel();
},
);
});
group('onChildChanged', () {
// Create own reference as this clashed with previous tests on web platform
late DatabaseReference childRef;
setUp(() async {
childRef = FirebaseDatabase.instance.ref('tests').child('child-ref');
// Wipe the database before each test
await childRef.remove();
});
test(
'emits an event when a child is changed',
() async {
await childRef.child('foo').set('foo');
await childRef.child('bar').set('bar');
final events = <DatabaseEvent>[];
final receivedTwo = Completer<void>();
final subscription = childRef.onChildChanged.listen((event) {
events.add(event);
if (events.length >= 2 && !receivedTwo.isCompleted) {
receivedTwo.complete();
}
});
// Wait for native listener registration by doing a round-trip
await childRef.child('__probe__').set(true);
await childRef.child('__probe__').remove();
await childRef.child('bar').set('baz');
await childRef.child('foo').set('bar');
await receivedTwo.future.timeout(const Duration(seconds: 10));
expect(events[0].snapshot.key, 'bar');
expect(events[0].snapshot.value, 'baz');
expect(events[0].type, DatabaseEventType.childChanged);
expect(events[1].snapshot.key, 'foo');
expect(events[1].snapshot.value, 'bar');
expect(events[1].type, DatabaseEventType.childChanged);
await subscription.cancel();
},
);
});
group('onChildMoved', () {
test(
'emits an event when a child is moved',
() async {
await ref.set({
'alex': {'nuggets': 60},
'rob': {'nuggets': 56},
'vassili': {'nuggets': 55.5},
'tony': {'nuggets': 52},
'greg': {'nuggets': 52},
});
final events = <DatabaseEvent>[];
final receivedTwo = Completer<void>();
final subscription =
ref.orderByChild('nuggets').onChildMoved.listen((event) {
events.add(event);
if (events.length >= 2 && !receivedTwo.isCompleted) {
receivedTwo.complete();
}
});
// Wait for native listener registration by doing a round-trip
await ref.child('__probe__').set(true);
await ref.child('__probe__').remove();
await ref.child('greg/nuggets').set(57);
await ref.child('rob/nuggets').set(61);
await receivedTwo.future.timeout(const Duration(seconds: 10));
expect(events[0].snapshot.value, {'nuggets': 57});
expect(events[0].type, DatabaseEventType.childMoved);
expect(events[1].snapshot.value, {'nuggets': 61});
expect(events[1].type, DatabaseEventType.childMoved);
await subscription.cancel();
},
);
});
group('onValue', () {
test('emits an event when the data changes', () async {
await ref.set({
'a': 2,
'b': 3,
'c': 1,
});
expect(
ref.onValue,
emitsInOrder([
isA<DatabaseEvent>().having((s) => s.snapshot.value, 'value', {
'a': 2,
'b': 3,
'c': 1,
}).having((e) => e.type, 'type', DatabaseEventType.value),
]),
);
});
test(
'cancels overlapping query streams without missing plugin',
() async {
const subscriptionCount = 128;
final queryRef = ref.child('overlapping-query-streams');
await queryRef.set({'value': 1});
final errors = <Object>[];
final subscriptions = <StreamSubscription<DatabaseEvent>>[];
final firstEventsReceived = Completer<void>();
var firstEventCount = 0;
for (var i = 0; i < subscriptionCount; i++) {
subscriptions.add(
queryRef.onValue.listen(
(_) {
firstEventCount++;
if (firstEventCount >= subscriptionCount &&
!firstEventsReceived.isCompleted) {
firstEventsReceived.complete();
}
},
onError: errors.add,
),
);
}
await firstEventsReceived.future.timeout(const Duration(seconds: 10));
await Future.wait(
subscriptions.map((subscription) => subscription.cancel()),
);
expect(errors, isEmpty);
},
skip: defaultTargetPlatform != TargetPlatform.android,
);
test(
'throw a `permission-denied` exception when accessing restricted data',
() async {
final Completer<FirebaseException> errorReceived =
Completer<FirebaseException>();
FirebaseDatabase.instance.ref().child('restricted').onValue.listen(
(event) {
// Do nothing
},
onError: (error) {
errorReceived.complete(error);
},
);
final streamError = await errorReceived.future;
expect(streamError, isA<FirebaseException>());
expect(streamError.code, 'permission-denied');
});
});
group('keepSynced', () {
test(
'multiple queries can enable keepSynced without crashing',
() async {
await ref.set({
'a': {'value': 1},
'b': {'value': 2},
'c': {'value': 3},
});
// Enable keepSynced on multiple different queries
final query1 = ref.orderByChild('value').limitToFirst(2);
final query2 = ref.orderByChild('value').limitToLast(2);
final query3 = ref.orderByKey().startAt('a');
final query4 = ref.orderByValue();
// These should all complete without throwing
await query1.keepSynced(true);
await query2.keepSynced(true);
await query3.keepSynced(true);
await query4.keepSynced(true);
// Verify data is still accessible after enabling keepSynced
final snapshot = await ref.get();
expect(snapshot.value, isNotNull);
},
skip: kIsWeb,
);
test(
'multiple queries can disable keepSynced without crashing',
() async {
await ref.set({
'a': {'value': 1},
'b': {'value': 2},
'c': {'value': 3},
});
final query1 = ref.orderByChild('value').limitToFirst(2);
final query2 = ref.orderByChild('value').limitToLast(2);
final query3 = ref.orderByKey().startAt('a');
// First enable keepSynced
await query1.keepSynced(true);
await query2.keepSynced(true);
await query3.keepSynced(true);
// Then disable keepSynced on all queries - should not crash
await query1.keepSynced(false);
await query2.keepSynced(false);
await query3.keepSynced(false);
// Verify data is still accessible after disabling keepSynced
final snapshot = await ref.get();
expect(snapshot.value, isNotNull);
},
skip: kIsWeb,
);
test(
'calling keepSynced multiple times on same query does not crash',
() async {
await ref.set({
'a': 1,
'b': 2,
});
final query = ref.orderByValue().limitToFirst(5);
// Call keepSynced multiple times on the same query
await query.keepSynced(true);
await query.keepSynced(true);
await query.keepSynced(false);
await query.keepSynced(true);
await query.keepSynced(false);
await query.keepSynced(false);
// Should complete without any issues
final snapshot = await ref.get();
expect(snapshot.value, isNotNull);
},
skip: kIsWeb,
);
test(
'keepSynced works with various query combinations',
() async {
await ref.set({
'item1': {'name': 'alpha', 'priority': 1},
'item2': {'name': 'beta', 'priority': 2},
'item3': {'name': 'gamma', 'priority': 3},
'item4': {'name': 'delta', 'priority': 4},
});
// Test various query combinations with keepSynced
final queries = [
ref.orderByChild('name'),
ref.orderByChild('priority').startAt(2),
ref.orderByChild('priority').endAt(3),
ref.orderByChild('priority').equalTo(2),
ref.orderByKey().limitToFirst(2),
ref.orderByKey().limitToLast(2),
];
// Enable keepSynced on all queries
for (final query in queries) {
await query.keepSynced(true);
}
// Disable keepSynced on all queries
for (final query in queries) {
await query.keepSynced(false);
}
// Verify everything still works
final snapshot = await ref.get();
expect(snapshot.children.length, 4);
},
skip: kIsWeb,
);
});
});
}