/
results_test.dart
1015 lines (808 loc) · 33.5 KB
/
results_test.dart
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
// Copyright 2021 MongoDB, Inc.
// SPDX-License-Identifier: Apache-2.0
// ignore_for_file: unused_local_variable
import 'dart:typed_data';
import 'package:test/test.dart' hide test, throws;
import 'package:realm_dart/realm.dart';
import 'test.dart';
void main() {
setupTests();
test('Results all should not return null', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
final cars = realm.all<Car>();
expect(cars, isNotNull);
});
test('Results length after deletedMany', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
var cars = realm.all<Car>();
expect(cars.length, 0);
final carOne = Car("Toyota 1");
final carTwo = Car("Toyota 2");
final carThree = Car("Renault");
realm.write(() => realm.addAll([carOne, carTwo, carThree]));
expect(cars.length, 3);
final filteredCars = realm.query<Car>('make BEGINSWITH "Toyot"');
expect(filteredCars.length, 2);
realm.write(() => realm.deleteMany(filteredCars));
expect(filteredCars.length, 0);
expect(cars.length, 1);
});
test('Results length', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
var cars = realm.all<Car>();
expect(cars.length, 0);
final carOne = Car("Toyota");
final carTwo = Car("Toyota 1");
realm.write(() => realm.addAll([carOne, carTwo]));
expect(cars.length, 2);
final filteredCars = realm.query<Car>('make == "Toyota"');
expect(filteredCars.length, 1);
});
test('Results isEmpty', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
var cars = realm.all<Car>();
expect(cars.isEmpty, true);
final car = Car("Opel");
realm.write(() => realm.add(car));
expect(cars.isEmpty, false);
realm.write(() => realm.delete(car));
expect(cars.isEmpty, true);
});
test('Results from query isEmpty', () {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
final dogOne = Dog("Pupu", age: 1);
final dogTwo = Dog("Ostin", age: 2);
realm.write(() => realm.addAll([dogOne, dogTwo]));
var dogs = realm.query<Dog>('age == 0');
expect(dogs.isEmpty, true);
dogs = realm.query<Dog>('age == 1');
expect(dogs.isEmpty, false);
realm.write(() => realm.deleteMany(dogs));
expect(dogs.isEmpty, true);
dogs = realm.all<Dog>();
expect(dogs.isEmpty, false);
});
test('Results get by index', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
final car = Car('');
realm.write(() => realm.add(car));
final cars = realm.all<Car>();
expect(cars[0].make, car.make);
});
test('Results requested wrong index throws', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
final cars = realm.all<Car>();
expect(() => cars[0], throws<RangeError>());
});
test('Results iteration test', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
//Create two teams
realm.write(() {
realm.add(Team("team One"));
realm.add(Team("team Two"));
});
//Reload teams from realm and ensure they exist
var teams = realm.all<Team>();
expect(teams.length, 2);
//Iterate through teams and add realm objects to a list
List<Team> list = [];
for (Team team in teams) {
list.add(team);
}
//Ensure list size is the same like teams collection size
expect(list.length, teams.length);
});
test('Results snapshot iteration test', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
//Create two teams
realm.write(() {
realm.add(Team("team One"));
realm.add(Team("team Two"));
});
//Reload teams from realm and ensure they exist
var teams = realm.all<Team>();
expect(teams.length, 2);
//Adding new teams to real while iterating through them.
//Iterator use a snapshot of results collection and ignores newly added teams.
List<Team> list = [];
for (Team team in teams) {
list.add(team);
realm.write(() {
realm.add(Team("new team"));
});
}
//Ensure list size is the same like teams collection size was at the beginnig
expect(list.length, 2);
//Ensure teams collection is increased
expect(teams.length, 4);
//Iterating teams again will create a snapshot with the newly added items
list.clear();
for (Team team in teams) {
list.add(team);
}
expect(list.length, teams.length);
});
test('Results query', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
realm.write(() => realm
..add(Car("Audi"))
..add(Car("Tesla")));
final cars = realm.all<Car>().query('make == "Tesla"');
expect(cars.length, 1);
expect(cars[0].make, "Tesla");
});
test('Results query with parameter', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
realm.write(() => realm
..add(Car("Audi"))
..add(Car("Tesla")));
final cars = realm.all<Car>().query(r'make == $0', ['Tesla']);
expect(cars.length, 1);
expect(cars[0].make, "Tesla");
});
test('Results query with multiple parameters', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
final p1 = Person('p1');
final p2 = Person('p2');
final t1 = Team("A1", players: [p1]); // match
final t2 = Team("A2", players: [p2]); // correct prefix, but wrong player
final t3 = Team("B1", players: [p1, p2]); // wrong prefix, but correct player
realm.write(() => realm.addAll([t1, t2, t3]));
expect(t1.players, [p1]);
expect(t2.players, [p2]);
expect(t3.players, [p1, p2]);
final filteredTeams = realm.all<Team>().query(r'$0 IN players AND name BEGINSWITH $1', [p1, 'A']);
expect(filteredTeams.length, 1);
expect(filteredTeams, [t1]);
});
test('Query results with no arguments throws', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
realm.write(() => realm.add(Car("Audi")));
expect(() => realm.all<Car>().query(r'make == $0'), throws<RealmException>("no arguments are provided"));
});
test('Results query with wrong argument types (int for string) throws', () {
var config = Configuration.local([Car.schema]);
var realm = getRealm(config);
realm.write(() => realm.add(Car("Audi")));
expect(() => realm.all<Car>().query(r'make == $0', [1]), throws<RealmException>("Cannot compare argument"));
});
test('Results query with wrong argument types (bool for int) throws ', () {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
realm.write(() => realm.add(Dog("Foxi")));
expect(() => realm.all<Dog>().query(r'age == $0', [true]), throws<RealmException>("Unsupported comparison between type"));
});
test('Results sort', () {
var config = Configuration.local([Person.schema]);
var realm = getRealm(config);
realm.write(() => realm.addAll([
Person("Michael"),
Person("Sebastian"),
Person("Kimi"),
]));
final result = realm.query<Person>('TRUEPREDICATE SORT(name ASC)');
final resultNames = result.map((p) => p.name).toList();
final sortedNames = [...resultNames]..sort();
expect(resultNames, sortedNames);
});
test('Results sort order is preserved', () {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
final dog1 = Dog("Bella", age: 1);
final dog2 = Dog("Fido", age: 2);
final dog3 = Dog("Oliver", age: 3);
realm.write(() => realm.addAll([dog1, dog2, dog3]));
var result = realm.query<Dog>('TRUEPREDICATE SORT(name ASC)');
final snapshot = result.toList();
expect(result, orderedEquals(snapshot));
expect(result.map((d) => d.name), snapshot.map((d) => d.name));
result = realm.query<Dog>('TRUEPREDICATE SORT(name ASC)'); // redoing query won't change that
expect(result, orderedEquals(snapshot));
expect(result.map((d) => d.name), snapshot.map((d) => d.name));
realm.write(() => realm.delete(dog1));
expect(() => snapshot[0].name, throws<RealmException>());
snapshot.removeAt(0); // remove dead object
expect(result, orderedEquals(snapshot));
expect(result.map((d) => d.name), snapshot.map((d) => d.name));
realm.write(() => realm.add(Dog("Bella", age: 4)));
expect(result, isNot(orderedEquals(snapshot)));
expect(result, containsAllInOrder(snapshot));
});
test('Results - get query length after realm is closed throws', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
var team = Team("TeamOne");
realm.write(() => realm.add(team));
final teams = realm.query<Team>('name BEGINSWITH "Team"');
realm.close();
expect(() => teams.length, throws<RealmException>("Access to invalidated Results objects"));
});
test('Results - query with null', () {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
var ben = Person('Ben');
final fido = Dog('Fido', owner: ben);
final laika = Dog('Laika'); // no owner;
realm.write(() {
realm.addAll([fido, laika]);
});
expect(realm.query<Dog>(r'owner = $0', [ben]), [fido]);
expect(realm.query<Dog>(r'owner = $0', [null]), [laika]);
});
test('Results access after realm closed throws', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
var team = Team("TeamOne");
realm.write(() => realm.add(team));
var teams = realm.all<Team>();
realm.close();
expect(() => teams[0], throws<RealmException>("Access to invalidated Results objects"));
});
test('Realm deleteMany from results', () {
var config = Configuration.local([Team.schema, Person.schema]);
var realm = getRealm(config);
//Create two teams
realm.write(() {
realm.add(Team("Ferrari"));
realm.add(Team("Maserati"));
});
//Ensule teams exist in realm
var teams = realm.all<Team>();
expect(teams.length, 2);
//Delete all objects in realmResults from realm
realm.write(() => realm.deleteMany(teams));
expect(teams.length, 0);
//Reload teams from realm and ensure they are deleted
teams = realm.all<Team>();
expect(teams.length, 0);
});
test('Results notifications', () async {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
realm.write(() {
realm.add(Dog("Fido"));
realm.add(Dog("Fido1"));
realm.add(Dog("Fido2"));
});
var firstCall = true;
final subscription = realm.all<Dog>().changes.listen((changes) {
if (firstCall) {
firstCall = false;
expect(changes.inserted.isEmpty, true);
expect(changes.modified.isEmpty, true);
expect(changes.deleted.isEmpty, true);
expect(changes.newModified.isEmpty, true);
expect(changes.moved.isEmpty, true);
} else {
expect(changes.inserted, [3]); //new object at index 3
expect(changes.modified, [0]); //object at index 0 changed
expect(changes.deleted.isEmpty, true);
expect(changes.newModified, [0]);
expect(changes.moved.isEmpty, true);
}
});
await Future<void>.delayed(const Duration(milliseconds: 20));
realm.write(() {
realm.all<Dog>().first.age = 2;
realm.add(Dog("Fido4"));
});
await Future<void>.delayed(const Duration(milliseconds: 20));
subscription.cancel();
await Future<void>.delayed(const Duration(milliseconds: 20));
});
test('Results notifications can be paused', () async {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
realm.write(() {
realm.add(Dog("Lassy"));
});
var callbackCalled = false;
final subscription = realm.all<Dog>().changes.listen((changes) {
callbackCalled = true;
});
await Future<void>.delayed(const Duration(milliseconds: 20));
expect(callbackCalled, true);
subscription.pause();
callbackCalled = false;
realm.write(() {
realm.add(Dog("Lassy1"));
});
expect(callbackCalled, false);
await Future<void>.delayed(const Duration(milliseconds: 20));
await subscription.cancel();
await Future<void>.delayed(const Duration(milliseconds: 20));
});
test('Results notifications can be resumed', () async {
var config = Configuration.local([Dog.schema, Person.schema]);
var realm = getRealm(config);
var callbackCalled = false;
final subscription = realm.all<Dog>().changes.listen((changes) {
callbackCalled = true;
});
await Future<void>.delayed(const Duration(milliseconds: 20));
expect(callbackCalled, true);
subscription.pause();
callbackCalled = false;
realm.write(() {
realm.add(Dog("Lassy"));
});
await Future<void>.delayed(const Duration(milliseconds: 20));
expect(callbackCalled, false);
subscription.resume();
callbackCalled = false;
realm.write(() {
realm.add(Dog("Lassy1"));
});
await Future<void>.delayed(const Duration(milliseconds: 20));
expect(callbackCalled, true);
await subscription.cancel();
await Future<void>.delayed(const Duration(milliseconds: 20));
});
test('Results notifications can leak', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final leak = realm.all<Dog>().changes.listen((data) {});
await Future<void>.delayed(const Duration(milliseconds: 20));
});
test('Results.freeze freezes query', () {
final config = Configuration.local([Person.schema]);
final realm = getRealm(config);
realm.write(() {
realm.add(Person('Peter'));
});
final livePeople = realm.all<Person>();
final frozenPeople = freezeResults(livePeople);
expect(frozenPeople.length, 1);
expect(frozenPeople.isFrozen, true);
expect(frozenPeople.realm.isFrozen, true);
expect(frozenPeople.single.isFrozen, true);
realm.write(() {
livePeople.single.name = 'Peter II';
realm.add(Person('George'));
});
expect(livePeople.length, 2);
expect(livePeople.first.name, 'Peter II');
expect(frozenPeople.length, 1);
expect(frozenPeople.single.name, 'Peter');
});
test("FrozenResults.changes throws", () {
final config = Configuration.local([Person.schema]);
final realm = getRealm(config);
final frozenPeople = freezeResults(realm.all<Person>());
expect(() => frozenPeople.changes, throws<RealmStateError>('Results are frozen and cannot emit changes'));
});
test('Results.freeze when frozen returns same object', () {
final config = Configuration.local([Person.schema]);
final realm = getRealm(config);
final people = realm.all<Person>();
final frozenPeople = freezeResults(people);
final deepFrozenPeople = freezeResults(frozenPeople);
expect(identical(frozenPeople, deepFrozenPeople), true);
final frozenPeopleAgain = freezeResults(people);
expect(identical(frozenPeople, frozenPeopleAgain), false);
});
test('Results.query', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);
final alice = Person('Alice');
final bob = Person('Bob');
final carol = Person('Carol');
final dan = Person('Dan');
final players = [alice, bob, carol, dan];
final team = Team('Class of 92', players: [alice, bob]);
realm.write(() {
realm.addAll(players);
return realm.add(team);
});
expect(realm.all<Person>(), [alice, bob, carol, dan]);
expect(realm.query<Person>('FALSEPREDICATE').query('TRUEPREDICATE'), isEmpty);
expect(realm.query<Person>('FALSEPREDICATE').query('TRUEPREDICATE'), isNot(realm.all<Person>()));
expect(realm.query<Person>("name CONTAINS 'a'"), [carol, dan]); // Alice is capital 'a'
expect(realm.query<Person>("name CONTAINS 'l'"), [alice, carol]);
expect(realm.query<Person>("name CONTAINS 'a'").query("name CONTAINS 'l'"), isNot([alice, carol]));
expect(realm.query<Person>("name CONTAINS 'a'").query("name CONTAINS 'l'"), [carol]);
});
test('Results of primitives', () {
var config = Configuration.local([Player.schema, Game.schema]);
var realm = getRealm(config);
final scores = [-1, null, 0, 1];
final alice = Player('Alice', scoresByRound: scores);
realm.write(() => realm.add(alice));
expect(alice.scoresByRound, scores);
expect(alice.scoresByRound.asResults(), scores);
});
test('Results of primitives notifications', () {
var config = Configuration.local([Player.schema, Game.schema]);
var realm = getRealm(config);
final scores = [-1, null, 0, 1];
final alice = Player('Alice', scoresByRound: scores);
realm.write(() => realm.add(alice));
final results = alice.scoresByRound.asResults();
expectLater(
results.changes,
emitsInOrder(<Matcher>[
isA<RealmResultsChanges>().having((ch) => ch.inserted, 'inserted', <int>[]),
isA<RealmResultsChanges>().having((ch) => ch.inserted, 'inserted', [4]),
isA<RealmResultsChanges>() //
.having((ch) => ch.inserted, 'inserted', [0]) //
.having((ch) => ch.deleted, 'deleted', [1]),
isA<RealmResultsChanges>().having((ch) => ch.deleted, 'deleted', [2, 4]),
]));
realm.write(() => alice.scoresByRound.add(2));
realm.write(() {
alice.scoresByRound.insert(0, 3);
alice.scoresByRound.remove(null);
});
realm.write(() {
alice.scoresByRound.removeAt(2);
alice.scoresByRound.removeAt(3); // index 4 in old list
});
});
test('List.asResults().query', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);
final alice = Person('Alice');
final bob = Person('Bob');
final carol = Person('Carol');
final dan = Person('Dan');
final team = realm.write(() {
return realm.add(Team('Class of 92', players: [alice, bob, carol, dan]));
});
final playersAsResults = team.players.asResults();
expect(playersAsResults, [alice, bob, carol, dan]);
expect(playersAsResults.query('FALSEPREDICATE').query('TRUEPREDICATE'), isEmpty);
expect(playersAsResults.query('FALSEPREDICATE').query('TRUEPREDICATE'), isNot(realm.all<Person>()));
expect(playersAsResults.query("name CONTAINS 'a'"), [carol, dan]); // Alice is capital 'a'
expect(playersAsResults.query("name CONTAINS 'l'"), [alice, carol]);
expect(playersAsResults.query("name CONTAINS 'a'").query("name CONTAINS 'l'"), isNot([alice, carol]));
expect(playersAsResults.query("name CONTAINS 'a'").query("name CONTAINS 'l'"), [carol]);
expect(() => realm.write(() => realm.deleteMany(playersAsResults.query("name CONTAINS 'a'"))), returnsNormally);
expect(team.players, [alice, bob]); // Alice is capital 'a'
});
test('Results.query with remapped property', () {
final config = Configuration.local([RemappedClass.schema]);
final realm = getRealm(config);
realm.write(() {
realm.add(RemappedClass('Peter'));
realm.add(RemappedClass('George'));
});
final queryByModelName = realm.query<RemappedClass>('remappedProperty = "Peter"');
expect(queryByModelName.single.remappedProperty, 'Peter');
final queryByInternalName = realm.query<RemappedClass>('primitive_property = "Peter"');
expect(queryByInternalName.single.remappedProperty, 'Peter');
});
test('RealmResults.changes - await for with yield ', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final wait = const Duration(seconds: 1);
Stream<bool> trueWaitFalse() async* {
yield true;
await Future<void>.delayed(wait);
yield false; // nothing has happened in the meantime
}
// ignore: prefer_function_declarations_over_variables
final awaitForWithYield = () async* {
await for (final c in realm.all<Dog>().changes) {
yield c;
}
};
int count = 0;
await for (final c in awaitForWithYield().map((_) => trueWaitFalse()).switchLatest()) {
if (!c) break; // saw false after waiting
++count; // saw true due to new event from changes
if (count > 1) fail('Should only receive one event');
}
});
test('RealmResults.isValid', () {
final config = Configuration.local([Team.schema, Person.schema]);
final realm = getRealm(config);
final alice = Person('Alice');
final bob = Person('Bob');
final carol = Person('Carol');
final dan = Person('Dan');
final team = realm.write(() {
return realm.add(Team('Class of 92', players: [alice, bob, carol, dan]));
});
final players = team.players;
final playersAsResults = team.players.asResults();
expect(players.isValid, isTrue);
expect(playersAsResults.isValid, isTrue);
expect(playersAsResults, [alice, bob, carol, dan]);
realm.write(() => realm.delete(team));
expect(team.isValid, isFalse); // dead object
expect(players.isValid, isFalse); // parent is dead
expect(() => players.isEmpty, throwsException); // illegal to access properties on dead object
expect(playersAsResults.isValid, isTrue); // Results are still valid..
expect(playersAsResults, isEmpty); // .. but obviously empty
expect(() => playersAsResults.freeze(), returnsNormally);
final frozeResults = playersAsResults.freeze();
expect(frozeResults.isFrozen, isTrue);
expect(frozeResults, isEmpty);
});
test('query by condition on decimal128', () {
final config = Configuration.local([ObjectWithDecimal.schema]);
final realm = getRealm(config);
final small = Decimal128.fromInt(1);
final big = Decimal128.fromInt(1 << 62) * Decimal128.fromInt(1 << 62);
realm.write(() => realm.addAll([
ObjectWithDecimal(small),
ObjectWithDecimal(big),
]));
expect(realm.query<ObjectWithDecimal>(r'decimal < $0', [small]), isEmpty);
expect(realm.query<ObjectWithDecimal>(r'decimal < $0', [big]).map((e) => e.decimal), [small]);
expect(realm.query<ObjectWithDecimal>(r'decimal <= $0', [big]).map((e) => e.decimal), [small, big]);
expect(realm.query<ObjectWithDecimal>(r'decimal == $0', [small]).map((e) => e.decimal), [small]);
expect(realm.query<ObjectWithDecimal>(r'decimal == $0', [big]).map((e) => e.decimal), [big]);
expect(realm.query<ObjectWithDecimal>(r'decimal > $0', [big]), isEmpty);
expect(realm.query<ObjectWithDecimal>(r'decimal > $0', [small]).map((e) => e.decimal), [big]);
expect(realm.query<ObjectWithDecimal>(r'decimal >= $0', [small]).map((e) => e.decimal), [small, big]);
});
test('Query parser works with long query string with OR/AND', () async {
final config = Configuration.local([Product.schema]);
final realm = getRealm(config);
List<String> list = [];
for (var i = 0; i <= 1500; i++) {
list.add("_id == oid(${ObjectId()})");
}
final ids = list.join(" OR ");
final result = realm.query<Product>(ids);
expect(result.length, 0);
});
test('Query parser works with IN clause and large set of items ', () async {
final config = Configuration.local([Product.schema]);
final realm = getRealm(config);
List<String> list = [];
for (var i = 0; i < 1500; i++) {
list.add("oid(${ObjectId()})");
}
final ids = "_id IN {${list.join(",")}}";
final items = realm.query<Product>(ids);
expect(items.length, 0);
});
test('RealmResults.first throws on empty', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final empty = Iterable<int>.empty();
// To illustrate the behavior of Iterable.single on a regular iterable
expect(() => empty.first, throws<StateError>('No element'));
expect(() => realm.all<Dog>().first, throws<StateError>('No element'));
expect(() => realm.all<Dog>().first, throws<RealmStateError>('No element'));
});
test('RealmResults.last throws on empty', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final empty = Iterable<int>.empty();
// To illustrate the behavior of Iterable.single on a regular iterable
expect(() => empty.last, throws<StateError>('No element'));
expect(() => realm.all<Dog>().last, throws<StateError>('No element'));
expect(() => realm.all<Dog>().last, throws<RealmStateError>('No element'));
});
test('RealmResult.single throws on empty', () {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final empty = Iterable<int>.empty();
// To illustrate the behavior of Iterable.single on a regular iterable
expect(() => empty.single, throws<StateError>('No element'));
expect(() => realm.all<Dog>().single, throws<StateError>('No element'));
expect(() => realm.all<Dog>().single, throws<RealmStateError>('No element'));
});
test('RealmResult.single throws on two items', () {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);
final twoItems = [1, 2];
// To illustrate the behavior of Iterable.single on a regular list
expect(() => twoItems.single, throws<StateError>('Too many elements'));
realm.write(() => realm.addAll([Dog('Fido'), Dog('Spot')]));
expect(() => realm.all<Dog>().single, throws<StateError>('Too many elements'));
expect(() => realm.all<Dog>().single, throws<RealmStateError>('Too many elements'));
});
test('Query with argument lists of different types and null', () {
final id_1 = ObjectId();
final id_2 = ObjectId();
final uid_1 = Uuid.v4();
final uid_2 = Uuid.v4();
final date_1 = DateTime.now().add(const Duration(days: 1));
final date_2 = DateTime.now().add(const Duration(days: 2));
final text_1 = generateRandomUnicodeString();
final config = Configuration.local([AllTypes.schema]);
Realm realm = getRealm(config);
realm.write(() => realm.addAll([
AllTypes(text_1, false, DateTime.now(), 1.1, id_1, uid_1, 1, Decimal128.one, Uint8List.fromList([1, 2])),
AllTypes('text2', true, date_1, 2.2, id_2, uid_2, 2, Decimal128.ten, Uint8List(16)),
AllTypes('text3', true, date_2, 3.3, ObjectId(), Uuid.v4(), 3, Decimal128.infinity, Uint8List.fromList([3, 4])),
]));
void queryWithListArg(String propName, Object? argument, {int expected = 0}) {
final results = realm.query<AllTypes>("$propName IN \$0", [argument]);
expect(results.length, expected);
}
queryWithListArg("stringProp", [null, text_1, 'text3'], expected: 2);
queryWithListArg("nullableStringProp", [null, 'text2'], expected: 3);
queryWithListArg("boolProp", [false, true, null], expected: 3);
queryWithListArg("nullableBoolProp", [null], expected: 3);
queryWithListArg("dateProp", [date_1, null, date_2], expected: 2);
queryWithListArg("nullableDateProp", [null], expected: 3);
queryWithListArg("doubleProp", [1.1, null, 3.3], expected: 2);
queryWithListArg("nullableDoubleProp", [null], expected: 3);
queryWithListArg("objectIdProp", [id_1, id_2, null], expected: 2);
queryWithListArg("nullableObjectIdProp", [null], expected: 3);
queryWithListArg("uuidProp", [uid_1, uid_2, null], expected: 2);
queryWithListArg("nullableUuidProp", [null], expected: 3);
queryWithListArg("intProp", [1, 2, null], expected: 2);
queryWithListArg("nullableIntProp", [null], expected: 3);
queryWithListArg("decimalProp", [Decimal128.one, null], expected: 1);
queryWithListArg("nullableDecimalProp", [null], expected: 3);
queryWithListArg(
"binaryProp",
[
Uint8List.fromList([1, 2]),
null,
Uint8List(16)
],
expected: 2);
queryWithListArg("nullableBinaryProp", [null], expected: 3);
});
test('Query with list, sets and iterable arguments', () {
final config = Configuration.local([Person.schema]);
Realm realm = getRealm(config);
realm.write(() => realm.addAll([
Person('Ani'),
Person('Teddy'),
Person('Poly'),
]));
final listOfNames = ['Ani', 'Teddy'];
var result = realm.query<Person>(r'name IN $0', [listOfNames]);
expect(result.length, 2);
final setOfNames = {'Poly', 'Teddy'};
result = realm.query<Person>(r'name IN $0', [setOfNames]);
expect(result.length, 2);
final iterableNames = result.map((e) => e.name);
result = realm.query<Person>(r'name IN $0', [iterableNames]);
expect(result.length, 2);
result = realm.query<Person>(r'name IN $0 || name IN $1 || name IN $2', [listOfNames, setOfNames, iterableNames]);
expect(result.length, 3);
});
test('Query with ANY, ALL and NONE operators with iterable arguments', () {
final config = Configuration.local([School.schema, Student.schema]);
final realm = getRealm(config);
realm.write(() => realm.addAll([
School('primary school 1', branches: [
School('131', city: "NY city"),
School('144', city: "Garden city"),
School('128'),
]),
School('secondary school 1', students: [
Student(1, name: 'NP'),
Student(2, name: 'KR'),
]),
]));
var result = realm.query<School>(r'ANY $0 IN branches.city', [
["NY city"]
]);
expect(result.length, 1);
expect(result.first.name, 'primary school 1');
result = realm.query<School>(r'ALL $0 IN branches.city', [
["NY city", "Garden city", null]
]);
expect(result.length, 1);
expect(result.first.name, 'primary school 1');
result = realm.query<School>(r'students.@count > $0 && NONE $1 IN students.name', [
0,
{'Non-existing name', null}
]);
expect(result.length, 1);
expect(result.first.name, 'secondary school 1');
});
test('RealmResults.skip', () {
final config = Configuration.local([Task.schema]);
final realm = getRealm(config);
const max = 10;
realm.write(() {
realm.addAll(List.generate(max, (_) => Task(ObjectId())));
});
final results = realm.all<Task>();
for (var i = 0; i < max; i++) {
expect(results.skip(i).length, max - i);
for (var j = 0; j < max - i; j++) {
expect(results.skip(i)[j], results[i + j]);
expect(results.skip(i).skip(j)[0], results[i + j]); // chained skip
}
expect(
() => results.skip(max + i + 1),
throwsA(isA<RangeError>().having((e) => e.invalidValue, 'count', max + i + 1)),
);
expect(
() => results.skip(-i - 1),
throwsA(isA<RangeError>().having((e) => e.invalidValue, 'count', -i - 1)),
);
}
});
test('_RealmResultsIterator', () {
// behavior of normal iterator
final list = [1];
final it = list.iterator;
// you are not supposed to call current before first moveNext
expect(() => it.current, throwsA(isA<TypeError>()));
expect(it.moveNext(), isTrue);
expect(it.moveNext(), isFalse);
// you are not supposed to call current, if moveNext return false
expect(() => it.current, throwsA(isA<TypeError>()));
// behavior of _RealmResultsIterator
final config = Configuration.local([Task.schema]);
final realm = getRealm(config);
realm.write(() {
realm.add(Task(ObjectId()));
});
final results = realm.all<Task>();
expect(results.length, 1);
final rit = results.iterator;
// you are not supposed to call current before first moveNext
expect(() => rit.current, throwsA(isA<RangeError>()));
expect(rit.moveNext(), isTrue);
expect(rit.moveNext(), isFalse);
// you are not supposed to call current, if moveNext return false
expect(() => rit.current, throwsA(isA<RangeError>()));
});
test('RealmResults.indexOf', () {
final config = Configuration.local([Task.schema]);
final realm = getRealm(config);
const max = 10;
realm.write(() {
realm.addAll(List.generate(max, (_) => Task(ObjectId())));
});
final results = realm.all<Task>();
expect(() => results.indexOf(Task(ObjectId())), throws<RealmStateError>());
int i = 0;
for (final t in results) {
expect(results.indexOf(t), i++);
}
});
test('RealmResults.contains', () {
final config = Configuration.local([Task.schema]);
final realm = getRealm(config);
const max = 10;
final tasks = List.generate(max, (_) => Task(ObjectId()));
realm.write(() {
realm.addAll(tasks);
});
final all = realm.all<Task>();
final none = realm.query<Task>('FALSEPREDICATE');
expect(() => all.contains(Task(ObjectId())), throws<RealmStateError>());
// ignore: unnecessary_cast, iterable_contains_unrelated_type
expect(() => (all as Iterable<Task>).contains(1), throwsA(isA<TypeError>()));
int i = 0;
for (final t in all) {
expect(all.contains(t), isTrue);
expect(none.contains(t), isFalse);
}
for (int i = 0; i < max; i++) {
for (var j = 0; j < max - i; j++) {
expect(all.skip(i).contains(tasks[i + j]), true);
expect(all.skip(i + j + 1).contains(tasks[i + j]), false);
}
}
});