-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathArrayPropertyTests.m
1543 lines (1255 loc) · 70.2 KB
/
ArrayPropertyTests.m
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 2014 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#import "RLMTestCase.h"
@implementation DogArrayObject
@end
@interface ArrayPropertyTests : RLMTestCase
@end
@implementation ArrayPropertyTests
-(void)testPopulateEmptyArray {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *array = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[], @[]]];
XCTAssertNotNil(array.array, @"Should be able to get an empty array");
XCTAssertEqual(array.array.count, 0U, @"Should start with no array elements");
StringObject *obj = [[StringObject alloc] init];
obj.stringCol = @"a";
[array.array addObject:obj];
[array.array addObject:[StringObject createInRealm:realm withValue:@[@"b"]]];
[array.array addObject:obj];
[realm commitWriteTransaction];
XCTAssertEqual(array.array.count, 3U, @"Should have three elements in array");
XCTAssertEqualObjects([array.array[0] stringCol], @"a", @"First element should have property value 'a'");
XCTAssertEqualObjects([array.array[1] stringCol], @"b", @"Second element should have property value 'b'");
XCTAssertEqualObjects([array.array[2] stringCol], @"a", @"Third element should have property value 'a'");
RLMArray *arrayProp = array.array;
RLMAssertThrowsWithReasonMatching([arrayProp addObject:obj], @"write transaction");
// make sure we can fast enumerate
for (RLMObject *obj in array.array) {
XCTAssertTrue(obj.description.length, @"Object should have description");
}
}
-(void)testModifyDetatchedArray {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *arObj = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[], @[]]];
XCTAssertNotNil(arObj.array, @"Should be able to get an empty array");
XCTAssertEqual(arObj.array.count, 0U, @"Should start with no array elements");
StringObject *obj = [[StringObject alloc] init];
obj.stringCol = @"a";
RLMArray *array = arObj.array;
[array addObject:obj];
[array addObject:[StringObject createInRealm:realm withValue:@[@"b"]]];
[realm commitWriteTransaction];
XCTAssertEqual(array.count, 2U, @"Should have two elements in array");
XCTAssertEqualObjects([array[0] stringCol], @"a", @"First element should have property value 'a'");
XCTAssertEqualObjects([arObj.array[1] stringCol], @"b", @"Second element should have property value 'b'");
RLMAssertThrowsWithReasonMatching([array addObject:obj], @"write transaction");
}
- (void)testDeleteUnmanagedObjectWithArrayProperty {
ArrayPropertyObject *arObj = [[ArrayPropertyObject alloc] initWithValue:@[@"arrayObject", @[@[@"a"]], @[]]];
RLMArray *stringArray = arObj.array;
XCTAssertFalse(stringArray.isInvalidated, @"stringArray should be valid after creation.");
arObj = nil;
XCTAssertFalse(stringArray.isInvalidated, @"stringArray should still be valid after parent deletion.");
}
- (void)testDeleteObjectWithArrayProperty {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *arObj = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[@[@"a"]], @[]]];
RLMArray *stringArray = arObj.array;
XCTAssertFalse(stringArray.isInvalidated, @"stringArray should be valid after creation.");
[realm deleteObject:arObj];
XCTAssertTrue(stringArray.isInvalidated, @"stringArray should be invalid after parent deletion.");
[realm commitWriteTransaction];
}
- (void)testDeleteObjectInArrayProperty {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *arObj = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[@[@"a"]], @[]]];
RLMArray *stringArray = arObj.array;
StringObject *firstObject = stringArray.firstObject;
[realm deleteObjects:[StringObject allObjectsInRealm:realm]];
XCTAssertFalse(stringArray.isInvalidated, @"stringArray should be valid after member object deletion.");
XCTAssertTrue(firstObject.isInvalidated, @"firstObject should be invalid after deletion.");
XCTAssertEqual(stringArray.count, 0U, @"stringArray.count should be zero after deleting its only member.");
[realm commitWriteTransaction];
}
-(void)testInsertMultiple {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *obj = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[], @[]]];
StringObject *child1 = [StringObject createInRealm:realm withValue:@[@"a"]];
StringObject *child2 = [[StringObject alloc] init];
child2.stringCol = @"b";
[obj.array addObjects:@[child2, child1]];
[realm commitWriteTransaction];
RLMResults *children = [StringObject allObjectsInRealm:realm];
XCTAssertEqualObjects([children[0] stringCol], @"a", @"First child should be 'a'");
XCTAssertEqualObjects([children[1] stringCol], @"b", @"Second child should be 'b'");
}
-(void)testInsertAtIndex {
RLMRealm *realm = [self realmWithTestPath];
[realm beginWriteTransaction];
ArrayPropertyObject *obj = [ArrayPropertyObject createInRealm:realm withValue:@[@"arrayObject", @[], @[]]];
StringObject *child1 = [StringObject createInRealm:realm withValue:@[@"a"]];
StringObject *child2 = [[StringObject alloc] init];
child2.stringCol = @"b";
[obj.array addObject:child2];
RLMAssertThrowsWithReasonMatching([obj.array insertObject:child1 atIndex:2], @"must be less than 2");
[realm commitWriteTransaction];
RLMArray *children = obj.array;
XCTAssertEqual(children.count, 1U);
XCTAssertEqualObjects([children[0] stringCol], @"b", @"Only child should be 'b'");
}
- (void)testMove {
RLMRealm *realm = [self realmWithTestPath];
ArrayPropertyObject *obj = [[ArrayPropertyObject alloc] initWithValue:@[@"arrayObject", @[@[@"a"], @[@"b"]], @[]]];
RLM_GENERIC_ARRAY(StringObject) *children = obj.array;
[children moveObjectAtIndex:1 toIndex:0];
XCTAssertEqualObjects([children[0] stringCol], @"b");
XCTAssertEqualObjects([children[1] stringCol], @"a");
[children moveObjectAtIndex:0 toIndex:1];
XCTAssertEqualObjects([children[0] stringCol], @"a");
XCTAssertEqualObjects([children[1] stringCol], @"b");
[children moveObjectAtIndex:0 toIndex:0];
XCTAssertEqualObjects([children[0] stringCol], @"a");
XCTAssertEqualObjects([children[1] stringCol], @"b");
RLMAssertThrowsWithReasonMatching([children moveObjectAtIndex:0 toIndex:2], @"must be less than 2");
RLMAssertThrowsWithReasonMatching([children moveObjectAtIndex:2 toIndex:0], @"must be less than 2");
[realm beginWriteTransaction];
[realm addObject:obj];
children = obj.array;
[children moveObjectAtIndex:1 toIndex:0];
XCTAssertEqualObjects([children[0] stringCol], @"b");
XCTAssertEqualObjects([children[1] stringCol], @"a");
[children moveObjectAtIndex:0 toIndex:1];
XCTAssertEqualObjects([children[0] stringCol], @"a");
XCTAssertEqualObjects([children[1] stringCol], @"b");
[children moveObjectAtIndex:0 toIndex:0];
XCTAssertEqualObjects([children[0] stringCol], @"a");
XCTAssertEqualObjects([children[1] stringCol], @"b");
RLMAssertThrowsWithReasonMatching([children moveObjectAtIndex:0 toIndex:2], @"must be less than 2");
RLMAssertThrowsWithReasonMatching([children moveObjectAtIndex:2 toIndex:0], @"must be less than 2");
[realm commitWriteTransaction];
RLMAssertThrowsWithReasonMatching([children moveObjectAtIndex:1 toIndex:0], @"write transaction");
}
- (void)testAddInvalidated {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
CompanyObject *company = [CompanyObject createInDefaultRealmWithValue:@[@"company", @[]]];
EmployeeObject *person = [[EmployeeObject alloc] init];
person.name = @"Mary";
[realm addObject:person];
[realm deleteObjects:[EmployeeObject allObjects]];
RLMAssertThrowsWithReasonMatching([company.employees addObject:person], @"invalidated");
RLMAssertThrowsWithReasonMatching([company.employees insertObject:person atIndex:0], @"invalidated");
[realm cancelWriteTransaction];
}
- (void)testAddNil {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
CompanyObject *company = [CompanyObject createInDefaultRealmWithValue:@[@"company", @[]]];
RLMAssertThrowsWithReason([company.employees addObject:self.nonLiteralNil],
@"Invalid nil value for array of 'EmployeeObject'.");
[realm cancelWriteTransaction];
}
- (void)testUnmanaged {
RLMRealm *realm = [self realmWithTestPath];
ArrayPropertyObject *array = [[ArrayPropertyObject alloc] init];
array.name = @"name";
XCTAssertNotNil(array.array, @"RLMArray property should get created on access");
XCTAssertNil(array.array.firstObject, @"No objects added yet");
XCTAssertNil(array.array.lastObject, @"No objects added yet");
StringObject *obj1 = [[StringObject alloc] init];
obj1.stringCol = @"a";
StringObject *obj2 = [[StringObject alloc] init];
obj2.stringCol = @"b";
StringObject *obj3 = [[StringObject alloc] init];
obj3.stringCol = @"c";
[array.array addObject:obj1];
[array.array addObject:obj2];
[array.array addObject:obj3];
XCTAssertEqualObjects(array.array.firstObject, obj1, @"Objects should be equal");
XCTAssertEqualObjects(array.array.lastObject, obj3, @"Objects should be equal");
XCTAssertEqualObjects([array.array objectAtIndex:1], obj2, @"Objects should be equal");
[realm beginWriteTransaction];
[realm addObject:array];
[realm commitWriteTransaction];
XCTAssertEqual(array.array.count, 3U, @"Should have two elements in array");
XCTAssertEqualObjects([array.array[0] stringCol], @"a", @"First element should have property value 'a'");
XCTAssertEqualObjects([array.array[1] stringCol], @"b", @"Second element should have property value 'b'");
[realm beginWriteTransaction];
[array.array replaceObjectAtIndex:0 withObject:obj3];
XCTAssertTrue([[array.array objectAtIndex:0] isEqualToObject:obj3], @"Objects should be replaced");
array.array[0] = obj1;
XCTAssertTrue([obj1 isEqualToObject:[array.array objectAtIndex:0]], @"Objects should be replaced");
[array.array removeLastObject];
XCTAssertEqual(array.array.count, 2U, @"2 objects left");
[array.array addObject:obj1];
[array.array removeAllObjects];
XCTAssertEqual(array.array.count, 0U, @"All objects removed");
[realm commitWriteTransaction];
ArrayPropertyObject *intArray = [[ArrayPropertyObject alloc] init];
IntObject *intObj = [[IntObject alloc] init];
intObj.intCol = 1;
RLMAssertThrowsWithReasonMatching([intArray.array addObject:(id)intObj], @"IntObject.*StringObject");
[intArray.intArray addObject:intObj];
XCTAssertThrows([intArray.intArray objectsWhere:@"intCol == 1"], @"Should throw on unmanaged RLMArray");
XCTAssertThrows(([intArray.intArray objectsWithPredicate:[NSPredicate predicateWithFormat:@"intCol == %i", 1]]), @"Should throw on unmanaged RLMArray");
XCTAssertThrows([intArray.intArray sortedResultsUsingKeyPath:@"intCol" ascending:YES], @"Should throw on unmanaged RLMArray");
XCTAssertEqual(0U, [intArray.intArray indexOfObjectWhere:@"intCol == 1"]);
XCTAssertEqual(0U, ([intArray.intArray indexOfObjectWithPredicate:[NSPredicate predicateWithFormat:@"intCol == %i", 1]]));
XCTAssertEqual([intArray.intArray indexOfObject:intObj], 0U, @"Should be first element");
XCTAssertEqual([intArray.intArray indexOfObject:intObj], 0U, @"Should be first element");
// test unmanaged with literals
__unused ArrayPropertyObject *obj = [[ArrayPropertyObject alloc] initWithValue:@[@"n", @[], @[[[IntObject alloc] initWithValue:@[@1]]]]];
}
- (void)testUnmanagedPrimitive {
AllPrimitiveArrays *obj = [[AllPrimitiveArrays alloc] init];
XCTAssertTrue([obj.intObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.floatObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.doubleObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.boolObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.stringObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.dataObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.dateObj isKindOfClass:[RLMArray class]]);
[obj.intObj addObject:@1];
XCTAssertEqualObjects(obj.intObj[0], @1);
XCTAssertThrows([obj.intObj addObject:@""]);
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
obj = [AllPrimitiveArrays createInRealm:realm withValue:@[@[],@[],@[],@[],@[],@[],@[]]];
XCTAssertTrue([obj.intObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.floatObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.doubleObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.boolObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.stringObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.dataObj isKindOfClass:[RLMArray class]]);
XCTAssertTrue([obj.dateObj isKindOfClass:[RLMArray class]]);
[obj.intObj addObject:@5];
XCTAssertEqualObjects(obj.intObj.firstObject, @5);
[realm cancelWriteTransaction];
}
- (void)testReplaceObjectAtIndexInUnmanagedArray {
ArrayPropertyObject *array = [[ArrayPropertyObject alloc] init];
array.name = @"name";
StringObject *stringObj1 = [[StringObject alloc] init];
stringObj1.stringCol = @"a";
StringObject *stringObj2 = [[StringObject alloc] init];
stringObj2.stringCol = @"b";
StringObject *stringObj3 = [[StringObject alloc] init];
stringObj3.stringCol = @"c";
[array.array addObject:stringObj1];
[array.array addObject:stringObj2];
[array.array addObject:stringObj3];
IntObject *intObj1 = [[IntObject alloc] init];
intObj1.intCol = 0;
IntObject *intObj2 = [[IntObject alloc] init];
intObj2.intCol = 1;
IntObject *intObj3 = [[IntObject alloc] init];
intObj3.intCol = 2;
[array.intArray addObject:intObj1];
[array.intArray addObject:intObj2];
[array.intArray addObject:intObj3];
XCTAssertEqualObjects(array.array[0], stringObj1, @"Objects should be equal");
XCTAssertEqualObjects(array.array[1], stringObj2, @"Objects should be equal");
XCTAssertEqualObjects(array.array[2], stringObj3, @"Objects should be equal");
XCTAssertEqual(array.array.count, 3U, @"Should have 3 elements in string array");
XCTAssertEqualObjects(array.intArray[0], intObj1, @"Objects should be equal");
XCTAssertEqualObjects(array.intArray[1], intObj2, @"Objects should be equal");
XCTAssertEqualObjects(array.intArray[2], intObj3, @"Objects should be equal");
XCTAssertEqual(array.intArray.count, 3U, @"Should have 3 elements in int array");
StringObject *stringObj4 = [[StringObject alloc] init];
stringObj4.stringCol = @"d";
[array.array replaceObjectAtIndex:0 withObject:stringObj4];
XCTAssertTrue([[array.array objectAtIndex:0] isEqualToObject:stringObj4], @"Objects should be replaced");
XCTAssertEqual(array.array.count, 3U, @"Should have 3 elements in int array");
IntObject *intObj4 = [[IntObject alloc] init];
intObj4.intCol = 3;
[array.intArray replaceObjectAtIndex:1 withObject:intObj4];
XCTAssertTrue([[array.intArray objectAtIndex:1] isEqualToObject:intObj4], @"Objects should be replaced");
XCTAssertEqual(array.intArray.count, 3U, @"Should have 3 elements in int array");
RLMAssertThrowsWithReasonMatching([array.array replaceObjectAtIndex:0 withObject:(id)intObj4],
@"IntObject.*StringObject");
RLMAssertThrowsWithReasonMatching([array.intArray replaceObjectAtIndex:1 withObject:(id)stringObj4],
@"StringObject.*IntObject");
}
- (void)testDeleteObjectInUnmanagedArray {
ArrayPropertyObject *array = [[ArrayPropertyObject alloc] init];
array.name = @"name";
StringObject *stringObj1 = [[StringObject alloc] init];
stringObj1.stringCol = @"a";
StringObject *stringObj2 = [[StringObject alloc] init];
stringObj2.stringCol = @"b";
StringObject *stringObj3 = [[StringObject alloc] init];
stringObj3.stringCol = @"c";
[array.array addObject:stringObj1];
[array.array addObject:stringObj2];
[array.array addObject:stringObj3];
IntObject *intObj1 = [[IntObject alloc] init];
intObj1.intCol = 0;
IntObject *intObj2 = [[IntObject alloc] init];
intObj2.intCol = 1;
IntObject *intObj3 = [[IntObject alloc] init];
intObj3.intCol = 2;
[array.intArray addObject:intObj1];
[array.intArray addObject:intObj2];
[array.intArray addObject:intObj3];
XCTAssertEqualObjects(array.array[0], stringObj1, @"Objects should be equal");
XCTAssertEqualObjects(array.array[1], stringObj2, @"Objects should be equal");
XCTAssertEqualObjects(array.array[2], stringObj3, @"Objects should be equal");
XCTAssertEqual(array.array.count, 3U, @"Should have 3 elements in string array");
XCTAssertEqualObjects(array.intArray[0], intObj1, @"Objects should be equal");
XCTAssertEqualObjects(array.intArray[1], intObj2, @"Objects should be equal");
XCTAssertEqualObjects(array.intArray[2], intObj3, @"Objects should be equal");
XCTAssertEqual(array.intArray.count, 3U, @"Should have 3 elements in int array");
[array.array removeLastObject];
XCTAssertEqualObjects(array.array[0], stringObj1, @"Objects should be equal");
XCTAssertEqualObjects(array.array[1], stringObj2, @"Objects should be equal");
XCTAssertEqual(array.array.count, 2U, @"Should have 2 elements in string array");
[array.array removeLastObject];
XCTAssertEqualObjects(array.array[0], stringObj1, @"Objects should be equal");
XCTAssertEqual(array.array.count, 1U, @"Should have 1 elements in string array");
[array.array removeLastObject];
XCTAssertEqual(array.array.count, 0U, @"Should have 0 elements in string array");
[array.intArray removeAllObjects];
XCTAssertEqual(array.intArray.count, 0U, @"Should have 0 elements in int array");
}
- (void)testExchangeObjectAtIndexWithObjectAtIndex {
void (^test)(RLMArray *) = ^(RLMArray *array) {
[array exchangeObjectAtIndex:0 withObjectAtIndex:1];
XCTAssertEqual(2U, array.count);
XCTAssertEqualObjects(@"b", [array[0] stringCol]);
XCTAssertEqualObjects(@"a", [array[1] stringCol]);
[array exchangeObjectAtIndex:1 withObjectAtIndex:1];
XCTAssertEqual(2U, array.count);
XCTAssertEqualObjects(@"b", [array[0] stringCol]);
XCTAssertEqualObjects(@"a", [array[1] stringCol]);
[array exchangeObjectAtIndex:1 withObjectAtIndex:0];
XCTAssertEqual(2U, array.count);
XCTAssertEqualObjects(@"a", [array[0] stringCol]);
XCTAssertEqualObjects(@"b", [array[1] stringCol]);
RLMAssertThrowsWithReasonMatching([array exchangeObjectAtIndex:1 withObjectAtIndex:20], @"less than 2");
};
ArrayPropertyObject *array = [[ArrayPropertyObject alloc] initWithValue:@[@"foo", @[@[@"a"], @[@"b"]], @[]]];
test(array.array);
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:array];
test(array.array);
[realm commitWriteTransaction];
}
- (void)testIndexOfObject
{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
EmployeeObject *po1 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Joe", @"age": @40, @"hired": @YES}];
EmployeeObject *po2 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"John", @"age": @30, @"hired": @NO}];
EmployeeObject *po3 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Jill", @"age": @25, @"hired": @YES}];
EmployeeObject *deleted = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Jill", @"age": @25, @"hired": @YES}];
EmployeeObject *indirectlyDeleted = [EmployeeObject allObjectsInRealm:realm].lastObject;
[realm deleteObject:deleted];
// create company
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[company.employees addObjects:[EmployeeObject allObjects]];
[company.employees removeObjectAtIndex:1];
// test unmanaged
XCTAssertEqual(0U, [company.employees indexOfObject:po1]);
XCTAssertEqual(1U, [company.employees indexOfObject:po3]);
XCTAssertEqual((NSUInteger)NSNotFound, [company.employees indexOfObject:po2]);
// add to realm
[realm addObject:company];
[realm commitWriteTransaction];
// test LinkView RLMArray
XCTAssertEqual(0U, [company.employees indexOfObject:po1]);
XCTAssertEqual(1U, [company.employees indexOfObject:po3]);
XCTAssertEqual((NSUInteger)NSNotFound, [company.employees indexOfObject:po2]);
// non realm employee
EmployeeObject *notInRealm = [[EmployeeObject alloc] initWithValue:@[@"NoName", @1, @NO]];
XCTAssertEqual((NSUInteger)NSNotFound, [company.employees indexOfObject:notInRealm]);
// invalid object
XCTAssertThrows([company.employees indexOfObject:(EmployeeObject *)company]);
RLMAssertThrowsWithReasonMatching([company.employees indexOfObject:deleted], @"invalidated");
RLMAssertThrowsWithReasonMatching([company.employees indexOfObject:indirectlyDeleted], @"invalidated");
RLMResults *employees = [company.employees objectsWhere:@"age = %@", @40];
XCTAssertEqual(0U, [employees indexOfObject:po1]);
XCTAssertEqual((NSUInteger)NSNotFound, [employees indexOfObject:po3]);
}
- (void)testIndexOfObjectWhere
{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
EmployeeObject *po1 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Joe", @"age": @40, @"hired": @YES}];
[EmployeeObject createInRealm:realm withValue:@{@"name": @"John", @"age": @30, @"hired": @NO}];
EmployeeObject *po3 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Jill", @"age": @25, @"hired": @YES}];
EmployeeObject *po4 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Bill", @"age": @55, @"hired": @YES}];
// create company
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[company.employees addObjects:@[po3, po1, po4]];
// test unmanaged
XCTAssertEqual(0U, [company.employees indexOfObjectWhere:@"name = 'Jill'"]);
XCTAssertEqual(1U, [company.employees indexOfObjectWhere:@"name = 'Joe'"]);
XCTAssertEqual((NSUInteger)NSNotFound, [company.employees indexOfObjectWhere:@"name = 'John'"]);
// add to realm
[realm addObject:company];
[realm commitWriteTransaction];
// test LinkView RLMArray
XCTAssertEqual(0U, [company.employees indexOfObjectWhere:@"name = 'Jill'"]);
XCTAssertEqual(1U, [company.employees indexOfObjectWhere:@"name = 'Joe'"]);
XCTAssertEqual((NSUInteger)NSNotFound, [company.employees indexOfObjectWhere:@"name = 'John'"]);
RLMResults *results = [company.employees objectsWhere:@"age > 30"];
XCTAssertEqual(0U, [results indexOfObjectWhere:@"name = 'Joe'"]);
XCTAssertEqual(1U, [results indexOfObjectWhere:@"name = 'Bill'"]);
XCTAssertEqual((NSUInteger)NSNotFound, [results indexOfObjectWhere:@"name = 'John'"]);
XCTAssertEqual((NSUInteger)NSNotFound, [results indexOfObjectWhere:@"name = 'Jill'"]);
}
- (void)testFastEnumeration
{
RLMRealm *realm = self.realmWithTestPath;
[realm beginWriteTransaction];
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[realm addObject:company];
[realm commitWriteTransaction];
// enumerate empty array
for (__unused id obj in company.employees) {
XCTFail(@"Should be empty");
}
[realm beginWriteTransaction];
for (int i = 0; i < 30; ++i) {
EmployeeObject *eo = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Joe", @"age": @40, @"hired": @YES}];
[company.employees addObject:eo];
}
[realm commitWriteTransaction];
XCTAssertEqual(company.employees.count, 30U);
__weak id objects[30];
NSInteger count = 0;
for (EmployeeObject *e in company.employees) {
XCTAssertNotNil(e, @"Object is not nil and accessible");
if (count > 16) {
// 16 is the size of blocks fast enumeration happens to ask for at
// the moment, but of course that's just an implementation detail
// that may change
XCTAssertNil(objects[count - 16]);
}
objects[count++] = e;
}
XCTAssertEqual(count, 30, @"should have enumerated 30 objects");
for (int i = 0; i < count; i++) {
XCTAssertNil(objects[i], @"Object should have been released");
}
@autoreleasepool {
for (EmployeeObject *e in company.employees) {
objects[0] = e;
break;
}
}
XCTAssertNil(objects[0], @"Object should have been released");
}
- (void)testModifyDuringEnumeration {
RLMRealm *realm = self.realmWithTestPath;
[realm beginWriteTransaction];
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[realm addObject:company];
const size_t totalCount = 40;
for (size_t i = 0; i < totalCount; ++i) {
[company.employees addObject:[EmployeeObject createInRealm:realm withValue:@[@"name", @(i), @NO]]];
}
size_t count = 0;
for (EmployeeObject *eo in company.employees) {
++count;
[company.employees addObject:eo];
}
XCTAssertEqual(totalCount, count);
XCTAssertEqual(totalCount * 2, company.employees.count);
[realm cancelWriteTransaction];
// Unmanaged array
company = [[CompanyObject alloc] init];
for (size_t i = 0; i < totalCount; ++i) {
[company.employees addObject:[[EmployeeObject alloc] initWithValue:@[@"name", @(i), @NO]]];
}
count = 0;
for (EmployeeObject *eo in company.employees) {
++count;
[company.employees addObject:eo];
}
XCTAssertEqual(totalCount, count);
XCTAssertEqual(totalCount * 2, company.employees.count);
}
- (void)testDeleteDuringEnumeration {
RLMRealm *realm = self.realmWithTestPath;
[realm beginWriteTransaction];
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[realm addObject:company];
const size_t totalCount = 40;
for (size_t i = 0; i < totalCount; ++i) {
[company.employees addObject:[EmployeeObject createInRealm:realm withValue:@[@"name", @(i), @NO]]];
}
[realm commitWriteTransaction];
[realm beginWriteTransaction];
for (__unused EmployeeObject *eo in company.employees) {
[realm deleteObjects:company.employees];
}
[realm commitWriteTransaction];
}
- (void)testValueForKey {
RLMRealm *realm = self.realmWithTestPath;
[realm beginWriteTransaction];
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
XCTAssertEqualObjects([company.employees valueForKey:@"name"], @[]);
[realm addObject:company];
[realm commitWriteTransaction];
XCTAssertEqualObjects([company.employees valueForKey:@"age"], @[]);
// managed
NSMutableArray *ages = [NSMutableArray array];
[realm beginWriteTransaction];
for (int i = 0; i < 30; ++i) {
[ages addObject:@(i)];
EmployeeObject *eo = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Joe", @"age": @(i), @"hired": @YES}];
[company.employees addObject:eo];
}
[realm commitWriteTransaction];
RLM_GENERIC_ARRAY(EmployeeObject) *employeeObjects = [company valueForKey:@"employees"];
NSMutableArray *kvcAgeProperties = [NSMutableArray array];
for (EmployeeObject *employee in employeeObjects) {
[kvcAgeProperties addObject:@(employee.age)];
}
XCTAssertEqualObjects(kvcAgeProperties, ages);
XCTAssertEqualObjects([company.employees valueForKey:@"age"], ages);
XCTAssertTrue([[[company.employees valueForKey:@"self"] firstObject] isEqualToObject:company.employees.firstObject]);
XCTAssertTrue([[[company.employees valueForKey:@"self"] lastObject] isEqualToObject:company.employees.lastObject]);
XCTAssertEqual([[company.employees valueForKeyPath:@"@count"] integerValue], 30);
XCTAssertEqual([[company.employees valueForKeyPath:@"@min.age"] integerValue], 0);
XCTAssertEqual([[company.employees valueForKeyPath:@"@max.age"] integerValue], 29);
XCTAssertEqualWithAccuracy([[company.employees valueForKeyPath:@"@avg.age"] doubleValue], 14.5, 0.1f);
XCTAssertEqualObjects([company.employees valueForKeyPath:@"@unionOfObjects.age"],
(@[@0, @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]));
XCTAssertEqualObjects([company.employees valueForKeyPath:@"@distinctUnionOfObjects.name"], (@[@"Joe"]));
RLMAssertThrowsWithReasonMatching([company.employees valueForKeyPath:@"@[email protected]"], @"Nested key paths.*not supported");
// unmanaged object
company = [[CompanyObject alloc] init];
ages = [NSMutableArray array];
for (int i = 0; i < 30; ++i) {
[ages addObject:@(i)];
EmployeeObject *eo = [[EmployeeObject alloc] initWithValue:@{@"name": @"Joe", @"age": @(i), @"hired": @YES}];
[company.employees addObject:eo];
}
XCTAssertEqualObjects([company.employees valueForKey:@"age"], ages);
XCTAssertTrue([[[company.employees valueForKey:@"self"] firstObject] isEqualToObject:company.employees.firstObject]);
XCTAssertTrue([[[company.employees valueForKey:@"self"] lastObject] isEqualToObject:company.employees.lastObject]);
XCTAssertEqual([[company.employees valueForKeyPath:@"@count"] integerValue], 30);
XCTAssertEqual([[company.employees valueForKeyPath:@"@min.age"] integerValue], 0);
XCTAssertEqual([[company.employees valueForKeyPath:@"@max.age"] integerValue], 29);
XCTAssertEqualWithAccuracy([[company.employees valueForKeyPath:@"@avg.age"] doubleValue], 14.5, 0.1f);
XCTAssertEqualObjects([company.employees valueForKeyPath:@"@unionOfObjects.age"],
(@[@0, @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]));
XCTAssertEqualObjects([company.employees valueForKeyPath:@"@distinctUnionOfObjects.name"], (@[@"Joe"]));
RLMAssertThrowsWithReasonMatching([company.employees valueForKeyPath:@"@[email protected]"], @"Nested key paths.*not supported");
}
- (void)testSetValueForKey {
RLMRealm *realm = self.realmWithTestPath;
[realm beginWriteTransaction];
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
[company.employees setValue:@"name" forKey:@"name"];
XCTAssertEqualObjects([company.employees valueForKey:@"name"], @[]);
[realm addObject:company];
[realm commitWriteTransaction];
XCTAssertThrows([company.employees setValue:@10 forKey:@"age"]);
XCTAssertEqualObjects([company.employees valueForKey:@"age"], @[]);
// managed
NSMutableArray *ages = [NSMutableArray array];
[realm beginWriteTransaction];
for (int i = 0; i < 30; ++i) {
[ages addObject:@(20)];
EmployeeObject *eo = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Joe", @"age": @(i), @"hired": @YES}];
[company.employees addObject:eo];
}
[company.employees setValue:@20 forKey:@"age"];
[realm commitWriteTransaction];
XCTAssertEqualObjects([company.employees valueForKey:@"age"], ages);
// unmanaged object
company = [[CompanyObject alloc] init];
ages = [NSMutableArray array];
for (int i = 0; i < 30; ++i) {
[ages addObject:@(20)];
EmployeeObject *eo = [[EmployeeObject alloc] initWithValue:@{@"name": @"Joe", @"age": @(i), @"hired": @YES}];
[company.employees addObject:eo];
}
[company.employees setValue:@20 forKey:@"age"];
XCTAssertEqualObjects([company.employees valueForKey:@"age"], ages);
}
- (void)testObjectAggregate {
RLMRealm *realm = [RLMRealm defaultRealm];
AggregateArrayObject *obj = [AggregateArrayObject new];
XCTAssertEqual(0, [obj.array sumOfProperty:@"intCol"].intValue);
XCTAssertNil([obj.array averageOfProperty:@"intCol"]);
XCTAssertNil([obj.array minOfProperty:@"intCol"]);
XCTAssertNil([obj.array maxOfProperty:@"intCol"]);
NSDate *dateMinInput = [NSDate date];
NSDate *dateMaxInput = [dateMinInput dateByAddingTimeInterval:1000];
[realm transactionWithBlock:^{
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[AggregateObject createInRealm:realm withValue:@[@1, @0.0f, @2.5, @NO, dateMaxInput]];
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[AggregateObject createInRealm:realm withValue:@[@1, @0.0f, @2.5, @NO, dateMaxInput]];
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[AggregateObject createInRealm:realm withValue:@[@1, @0.0f, @2.5, @NO, dateMaxInput]];
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[AggregateObject createInRealm:realm withValue:@[@1, @0.0f, @2.5, @NO, dateMaxInput]];
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[AggregateObject createInRealm:realm withValue:@[@0, @1.2f, @0.0, @YES, dateMinInput]];
[obj.array addObjects:[AggregateObject allObjectsInRealm:realm]];
}];
void (^test)(void) = ^{
RLMArray *array = obj.array;
// SUM
XCTAssertEqual([array sumOfProperty:@"intCol"].integerValue, 4);
XCTAssertEqualWithAccuracy([array sumOfProperty:@"floatCol"].floatValue, 7.2f, 0.1f);
XCTAssertEqualWithAccuracy([array sumOfProperty:@"doubleCol"].doubleValue, 10.0, 0.1f);
RLMAssertThrowsWithReasonMatching([array sumOfProperty:@"foo"], @"foo.*AggregateObject");
RLMAssertThrowsWithReasonMatching([array sumOfProperty:@"boolCol"], @"sum.*bool");
RLMAssertThrowsWithReasonMatching([array sumOfProperty:@"dateCol"], @"sum.*date");
// Average
XCTAssertEqualWithAccuracy([array averageOfProperty:@"intCol"].doubleValue, 0.4, 0.1f);
XCTAssertEqualWithAccuracy([array averageOfProperty:@"floatCol"].doubleValue, 0.72, 0.1f);
XCTAssertEqualWithAccuracy([array averageOfProperty:@"doubleCol"].doubleValue, 1.0, 0.1f);
RLMAssertThrowsWithReasonMatching([array averageOfProperty:@"foo"], @"foo.*AggregateObject");
RLMAssertThrowsWithReasonMatching([array averageOfProperty:@"boolCol"], @"average.*bool");
RLMAssertThrowsWithReasonMatching([array averageOfProperty:@"dateCol"], @"average.*date");
// MIN
XCTAssertEqual(0, [[array minOfProperty:@"intCol"] intValue]);
XCTAssertEqual(0.0f, [[array minOfProperty:@"floatCol"] floatValue]);
XCTAssertEqual(0.0, [[array minOfProperty:@"doubleCol"] doubleValue]);
XCTAssertEqualObjects(dateMinInput, [array minOfProperty:@"dateCol"]);
RLMAssertThrowsWithReasonMatching([array minOfProperty:@"foo"], @"foo.*AggregateObject");
RLMAssertThrowsWithReasonMatching([array minOfProperty:@"boolCol"], @"min.*bool");
// MAX
XCTAssertEqual(1, [[array maxOfProperty:@"intCol"] intValue]);
XCTAssertEqual(1.2f, [[array maxOfProperty:@"floatCol"] floatValue]);
XCTAssertEqual(2.5, [[array maxOfProperty:@"doubleCol"] doubleValue]);
XCTAssertEqualObjects(dateMaxInput, [array maxOfProperty:@"dateCol"]);
RLMAssertThrowsWithReasonMatching([array maxOfProperty:@"foo"], @"foo.*AggregateObject");
RLMAssertThrowsWithReasonMatching([array maxOfProperty:@"boolCol"], @"max.*bool");
};
test();
[realm transactionWithBlock:^{ [realm addObject:obj]; }];
test();
}
- (void)testRenamedPropertyAggregate {
RLMRealm *realm = [RLMRealm defaultRealm];
LinkToRenamedProperties1 *obj = [LinkToRenamedProperties1 new];
XCTAssertEqual(0, [obj.array sumOfProperty:@"propA"].intValue);
XCTAssertNil([obj.array averageOfProperty:@"propA"]);
XCTAssertNil([obj.array minOfProperty:@"propA"]);
XCTAssertNil([obj.array maxOfProperty:@"propA"]);
XCTAssertThrows([obj.array sumOfProperty:@"prop 1"]);
[realm transactionWithBlock:^{
[RenamedProperties1 createInRealm:realm withValue:@[@1, @""]];
[RenamedProperties1 createInRealm:realm withValue:@[@2, @""]];
[RenamedProperties1 createInRealm:realm withValue:@[@3, @""]];
[obj.array addObjects:[RenamedProperties1 allObjectsInRealm:realm]];
}];
XCTAssertEqual(6, [obj.array sumOfProperty:@"propA"].intValue);
XCTAssertEqual(2.0, [obj.array averageOfProperty:@"propA"].doubleValue);
XCTAssertEqual(1, [[obj.array minOfProperty:@"propA"] intValue]);
XCTAssertEqual(3, [[obj.array maxOfProperty:@"propA"] intValue]);
[realm transactionWithBlock:^{ [realm addObject:obj]; }];
XCTAssertEqual(6, [obj.array sumOfProperty:@"propA"].intValue);
XCTAssertEqual(2.0, [obj.array averageOfProperty:@"propA"].doubleValue);
XCTAssertEqual(1, [[obj.array minOfProperty:@"propA"] intValue]);
XCTAssertEqual(3, [[obj.array maxOfProperty:@"propA"] intValue]);
}
- (void)testValueForCollectionOperationKeyPath
{
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
EmployeeObject *e1 = [PrimaryEmployeeObject createInRealm:realm withValue:@{@"name": @"A", @"age": @20, @"hired": @YES}];
EmployeeObject *e2 = [PrimaryEmployeeObject createInRealm:realm withValue:@{@"name": @"B", @"age": @30, @"hired": @NO}];
EmployeeObject *e3 = [PrimaryEmployeeObject createInRealm:realm withValue:@{@"name": @"C", @"age": @40, @"hired": @YES}];
EmployeeObject *e4 = [PrimaryEmployeeObject createInRealm:realm withValue:@{@"name": @"D", @"age": @50, @"hired": @YES}];
PrimaryCompanyObject *c1 = [PrimaryCompanyObject createInRealm:realm withValue:@{@"name": @"ABC AG", @"employees": @[e1, e2, e3, e2]}];
PrimaryCompanyObject *c2 = [PrimaryCompanyObject createInRealm:realm withValue:@{@"name": @"ABC AG 2", @"employees": @[e1, e4]}];
ArrayOfPrimaryCompanies *companies = [ArrayOfPrimaryCompanies createInRealm:realm withValue:@[@[c1, c2]]];
[realm commitWriteTransaction];
// count operator
XCTAssertEqual([[c1.employees valueForKeyPath:@"@count"] integerValue], 4);
// numeric operators
XCTAssertEqual([[c1.employees valueForKeyPath:@"@min.age"] intValue], 20);
XCTAssertEqual([[c1.employees valueForKeyPath:@"@max.age"] intValue], 40);
XCTAssertEqual([[c1.employees valueForKeyPath:@"@sum.age"] integerValue], 120);
XCTAssertEqualWithAccuracy([[c1.employees valueForKeyPath:@"@avg.age"] doubleValue], 30, 0.1f);
// collection
XCTAssertEqualObjects([c1.employees valueForKeyPath:@"@unionOfObjects.name"],
(@[@"A", @"B", @"C", @"B"]));
XCTAssertEqualObjects([[c1.employees valueForKeyPath:@"@distinctUnionOfObjects.name"] sortedArrayUsingSelector:@selector(compare:)],
(@[@"A", @"B", @"C"]));
XCTAssertEqualObjects([companies.companies valueForKeyPath:@"@unionOfArrays.employees"],
(@[e1, e2, e3, e2, e1, e4]));
NSComparator cmp = ^NSComparisonResult(id obj1, id obj2) { return [[obj1 name] compare:[obj2 name]]; };
XCTAssertEqualObjects([[companies.companies valueForKeyPath:@"@distinctUnionOfArrays.employees"] sortedArrayUsingComparator:cmp],
(@[e1, e2, e3, e4]));
// invalid key paths
RLMAssertThrowsWithReasonMatching([c1.employees valueForKeyPath:@"@invalid.name"],
@"Unsupported KVC collection operator found in key path '@invalid.name'");
RLMAssertThrowsWithReasonMatching([c1.employees valueForKeyPath:@"@sum"],
@"Missing key path for KVC collection operator sum in key path '@sum'");
RLMAssertThrowsWithReasonMatching([c1.employees valueForKeyPath:@"@sum."],
@"Missing key path for KVC collection operator sum in key path '@sum.'");
RLMAssertThrowsWithReasonMatching([c1.employees valueForKeyPath:@"@[email protected]"],
@"Nested key paths.*not supported");
}
- (void)testCrossThreadAccess
{
CompanyObject *company = [[CompanyObject alloc] init];
company.name = @"name";
EmployeeObject *eo = [[EmployeeObject alloc] init];
eo.name = @"Joe";
eo.age = 40;
eo.hired = YES;
[company.employees addObject:eo];
RLMArray *employees = company.employees;
// Unmanaged object can be accessed from other threads
[self dispatchAsyncAndWait:^{
XCTAssertNoThrow(company.employees);
XCTAssertNoThrow([employees lastObject]);
}];
[RLMRealm.defaultRealm beginWriteTransaction];
[RLMRealm.defaultRealm addObject:company];
[RLMRealm.defaultRealm commitWriteTransaction];
employees = company.employees;
XCTAssertNoThrow(company.employees);
XCTAssertNoThrow([employees lastObject]);
[self dispatchAsyncAndWait:^{
XCTAssertThrows(company.employees);
XCTAssertThrows([employees lastObject]);
}];
}
- (void)testSortByNoColumns {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
DogObject *a2 = [DogObject createInDefaultRealmWithValue:@[@"a", @2]];
DogObject *b1 = [DogObject createInDefaultRealmWithValue:@[@"b", @1]];
DogObject *a1 = [DogObject createInDefaultRealmWithValue:@[@"a", @1]];
DogObject *b2 = [DogObject createInDefaultRealmWithValue:@[@"b", @2]];
RLMArray<DogObject *> *array = [DogArrayObject createInDefaultRealmWithValue:@[@[a2, b1, a1, b2]]].dogs;
[realm commitWriteTransaction];
RLMResults *notActuallySorted = [array sortedResultsUsingDescriptors:@[]];
XCTAssertTrue([array[0] isEqualToObject:notActuallySorted[0]]);
XCTAssertTrue([array[1] isEqualToObject:notActuallySorted[1]]);
XCTAssertTrue([array[2] isEqualToObject:notActuallySorted[2]]);
XCTAssertTrue([array[3] isEqualToObject:notActuallySorted[3]]);
}
- (void)testSortByMultipleColumns {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
DogObject *a1 = [DogObject createInDefaultRealmWithValue:@[@"a", @1]];
DogObject *a2 = [DogObject createInDefaultRealmWithValue:@[@"a", @2]];
DogObject *b1 = [DogObject createInDefaultRealmWithValue:@[@"b", @1]];
DogObject *b2 = [DogObject createInDefaultRealmWithValue:@[@"b", @2]];
DogArrayObject *array = [DogArrayObject createInDefaultRealmWithValue:@[@[a1, a2, b1, b2]]];
[realm commitWriteTransaction];
bool (^checkOrder)(NSArray *, NSArray *, NSArray *) = ^bool(NSArray *properties, NSArray *ascending, NSArray *dogs) {
NSArray *sort = @[[RLMSortDescriptor sortDescriptorWithKeyPath:properties[0] ascending:[ascending[0] boolValue]],
[RLMSortDescriptor sortDescriptorWithKeyPath:properties[1] ascending:[ascending[1] boolValue]]];
RLMResults *actual = [array.dogs sortedResultsUsingDescriptors:sort];
return [actual[0] isEqualToObject:dogs[0]]
&& [actual[1] isEqualToObject:dogs[1]]
&& [actual[2] isEqualToObject:dogs[2]]
&& [actual[3] isEqualToObject:dogs[3]];
};
// Check each valid sort
XCTAssertTrue(checkOrder(@[@"dogName", @"age"], @[@YES, @YES], @[a1, a2, b1, b2]));
XCTAssertTrue(checkOrder(@[@"dogName", @"age"], @[@YES, @NO], @[a2, a1, b2, b1]));
XCTAssertTrue(checkOrder(@[@"dogName", @"age"], @[@NO, @YES], @[b1, b2, a1, a2]));
XCTAssertTrue(checkOrder(@[@"dogName", @"age"], @[@NO, @NO], @[b2, b1, a2, a1]));
XCTAssertTrue(checkOrder(@[@"age", @"dogName"], @[@YES, @YES], @[a1, b1, a2, b2]));
XCTAssertTrue(checkOrder(@[@"age", @"dogName"], @[@YES, @NO], @[b1, a1, b2, a2]));
XCTAssertTrue(checkOrder(@[@"age", @"dogName"], @[@NO, @YES], @[a2, b2, a1, b1]));
XCTAssertTrue(checkOrder(@[@"age", @"dogName"], @[@NO, @NO], @[b2, a2, b1, a1]));
}
- (void)testSortByRenamedColumns {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
id value = @{@"array": @[@[@1, @"c"], @[@2, @"b"], @[@3, @"a"]]};
LinkToRenamedProperties1 *obj = [LinkToRenamedProperties1 createInRealm:realm withValue:value];
// FIXME: sorting has to use the column names because the parsing is done by
// the object store. This is not ideal.