-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathObjectCreationTests.swift
810 lines (706 loc) · 33.9 KB
/
ObjectCreationTests.swift
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
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 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 XCTest
import Realm.Private
import RealmSwift
import Foundation
class ObjectCreationTests: TestCase {
// MARK: Init tests
func testInitWithDefaults() {
// test all properties are defaults
let object = SwiftObject()
XCTAssertNil(object.realm)
// test defaults values
verifySwiftObjectWithDictionaryLiteral(object, dictionary: SwiftObject.defaultValues(), boolObjectValue: false,
boolObjectListValues: [])
// test realm properties are nil for standalone
XCTAssertNil(object.realm)
XCTAssertNil(object.objectCol!.realm)
XCTAssertNil(object.arrayCol.realm)
}
func testInitWithOptionalWithoutDefaults() {
let object = SwiftOptionalObject()
for prop in object.objectSchema.properties {
let value = object[prop.name]
if let value = value as? RLMOptionalBase {
XCTAssertNil(value.underlyingValue)
} else {
XCTAssertNil(value)
}
}
}
func testInitWithOptionalDefaults() {
let object = SwiftOptionalDefaultValuesObject()
verifySwiftOptionalObjectWithDictionaryLiteral(object, dictionary:
SwiftOptionalDefaultValuesObject.defaultValues(), boolObjectValue: true)
}
func testInitWithDictionary() {
// dictionary with all values specified
let baselineValues: [String: Any] =
["boolCol": true,
"intCol": 1,
"floatCol": 1.1 as Float,
"doubleCol": 11.1,
"stringCol": "b",
"binaryCol": "b".data(using: String.Encoding.utf8)!,
"dateCol": Date(timeIntervalSince1970: 2),
"objectCol": SwiftBoolObject(value: [true]),
"arrayCol": [SwiftBoolObject(value: [true]), SwiftBoolObject()]
]
// test with valid dictionary literals
let props = try! Realm().schema["SwiftObject"]!.properties
for propNum in 0..<props.count {
for validValue in validValuesForSwiftObjectType(props[propNum].type) {
// update dict with valid value and init
var values = baselineValues
values[props[propNum].name] = validValue
let object = SwiftObject(value: values)
verifySwiftObjectWithDictionaryLiteral(object, dictionary: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
}
// test with invalid dictionary literals
for propNum in 0..<props.count {
for invalidValue in invalidValuesForSwiftObjectType(props[propNum].type) {
// update dict with invalid value and init
var values = baselineValues
values[props[propNum].name] = invalidValue
assertThrows(SwiftObject(value: values), "Invalid property value")
}
}
}
func testInitWithDefaultsAndDictionary() {
// test with dictionary with mix of default and one specified value
let object = SwiftObject(value: ["intCol": 200])
let valueDict = defaultSwiftObjectValuesWithReplacements(["intCol": 200])
verifySwiftObjectWithDictionaryLiteral(object, dictionary: valueDict, boolObjectValue: false,
boolObjectListValues: [])
}
func testInitWithArray() {
// array with all values specified
let baselineValues: [Any] = [true, 1, 1.1 as Float, 11.1, "b", "b".data(using: String.Encoding.utf8)!,
Date(timeIntervalSince1970: 2), ["boolCol": true], [[true], [false]]]
// test with valid dictionary literals
let props = try! Realm().schema["SwiftObject"]!.properties
for propNum in 0..<props.count {
for validValue in validValuesForSwiftObjectType(props[propNum].type) {
// update dict with valid value and init
var values = baselineValues
values[propNum] = validValue
let object = SwiftObject(value: values)
verifySwiftObjectWithArrayLiteral(object, array: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
}
// test with invalid dictionary literals
for propNum in 0..<props.count {
for invalidValue in invalidValuesForSwiftObjectType(props[propNum].type) {
// update dict with invalid value and init
var values = baselineValues
values[propNum] = invalidValue
assertThrows(SwiftObject(value: values), "Invalid property value")
}
}
}
func testInitWithKVCObject() {
// test with kvc object
let objectWithInt = SwiftObject(value: ["intCol": 200])
let objectWithKVCObject = SwiftObject(value: objectWithInt)
let valueDict = defaultSwiftObjectValuesWithReplacements(["intCol": 200])
verifySwiftObjectWithDictionaryLiteral(objectWithKVCObject, dictionary: valueDict, boolObjectValue: false,
boolObjectListValues: [])
}
func testGenericInit() {
func createObject<T: Object>() -> T {
return T()
}
let obj1: SwiftBoolObject = createObject()
let obj2 = SwiftBoolObject()
XCTAssertEqual(obj1.boolCol, obj2.boolCol,
"object created via generic initializer should equal object created by calling initializer directly")
}
func testInitWithObjcName() {
// Test that init doesn't crash going into non-swift init logic for renamed Swift classes.
_ = SwiftObjcRenamedObject()
_ = SwiftObjcArbitrarilyRenamedObject()
}
// MARK: Creation tests
func testCreateWithDefaults() {
let realm = try! Realm()
assertThrows(realm.create(SwiftObject.self), "Must be in write transaction")
var object: SwiftObject!
let objects = realm.objects(SwiftObject.self)
XCTAssertEqual(0, objects.count)
try! realm.write {
// test create with all defaults
object = realm.create(SwiftObject.self)
return
}
verifySwiftObjectWithDictionaryLiteral(object, dictionary: SwiftObject.defaultValues(), boolObjectValue: false,
boolObjectListValues: [])
// test realm properties are populated correctly
XCTAssertEqual(object.realm!, realm)
XCTAssertEqual(object.objectCol!.realm!, realm)
XCTAssertEqual(object.arrayCol.realm!, realm)
}
func testCreateWithOptionalWithoutDefaults() {
let realm = try! Realm()
try! realm.write {
let object = realm.create(SwiftOptionalObject.self)
for prop in object.objectSchema.properties {
XCTAssertNil(object[prop.name])
}
}
}
func testCreateWithOptionalDefaults() {
let realm = try! Realm()
try! realm.write {
let object = realm.create(SwiftOptionalDefaultValuesObject.self)
self.verifySwiftOptionalObjectWithDictionaryLiteral(object,
dictionary: SwiftOptionalDefaultValuesObject.defaultValues(), boolObjectValue: true)
}
}
func testCreateWithOptionalIgnoredProperties() {
let realm = try! Realm()
try! realm.write {
let object = realm.create(SwiftOptionalIgnoredPropertiesObject.self)
let properties = object.objectSchema.properties
XCTAssertEqual(properties.count, 1)
XCTAssertEqual(properties[0].name, "id")
}
}
func testCreateWithDictionary() {
// dictionary with all values specified
let baselineValues: [String: Any] = [
"boolCol": true,
"intCol": 1,
"floatCol": 1.1 as Float,
"doubleCol": 11.1,
"stringCol": "b",
"binaryCol": "b".data(using: String.Encoding.utf8)!,
"dateCol": Date(timeIntervalSince1970: 2),
"objectCol": SwiftBoolObject(value: [true]),
"arrayCol": [SwiftBoolObject(value: [true]), SwiftBoolObject()]
]
// test with valid dictionary literals
let props = try! Realm().schema["SwiftObject"]!.properties
for propNum in 0..<props.count {
for validValue in validValuesForSwiftObjectType(props[propNum].type) {
// update dict with valid value and init
var values = baselineValues
values[props[propNum].name] = validValue
try! Realm().beginWrite()
let object = try! Realm().create(SwiftObject.self, value: values)
verifySwiftObjectWithDictionaryLiteral(object, dictionary: values, boolObjectValue: true,
boolObjectListValues: [true, false])
try! Realm().commitWrite()
verifySwiftObjectWithDictionaryLiteral(object, dictionary: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
}
// test with invalid dictionary literals
for propNum in 0..<props.count {
for invalidValue in invalidValuesForSwiftObjectType(props[propNum].type) {
// update dict with invalid value and init
var values = baselineValues
values[props[propNum].name] = invalidValue
try! Realm().beginWrite()
assertThrows(try! Realm().create(SwiftObject.self, value: values), "Invalid property value")
try! Realm().cancelWrite()
}
}
}
func testCreateWithDefaultsAndDictionary() {
// test with dictionary with mix of default and one specified value
let realm = try! Realm()
realm.beginWrite()
let objectWithInt = realm.create(SwiftObject.self, value: ["intCol": 200])
try! realm.commitWrite()
let valueDict = defaultSwiftObjectValuesWithReplacements(["intCol": 200])
verifySwiftObjectWithDictionaryLiteral(objectWithInt, dictionary: valueDict, boolObjectValue: false,
boolObjectListValues: [])
}
func testCreateWithArray() {
// array with all values specified
let baselineValues: [Any] = [true, 1, 1.1 as Float, 11.1, "b", "b".data(using: String.Encoding.utf8)!,
Date(timeIntervalSince1970: 2), ["boolCol": true], [[true], [false]]]
// test with valid dictionary literals
let props = try! Realm().schema["SwiftObject"]!.properties
for propNum in 0..<props.count {
for validValue in validValuesForSwiftObjectType(props[propNum].type) {
// update dict with valid value and init
var values = baselineValues
values[propNum] = validValue
try! Realm().beginWrite()
let object = try! Realm().create(SwiftObject.self, value: values)
verifySwiftObjectWithArrayLiteral(object, array: values, boolObjectValue: true,
boolObjectListValues: [true, false])
try! Realm().commitWrite()
verifySwiftObjectWithArrayLiteral(object, array: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
}
// test with invalid array literals
for propNum in 0..<props.count {
for invalidValue in invalidValuesForSwiftObjectType(props[propNum].type) {
// update dict with invalid value and init
var values = baselineValues
values[propNum] = invalidValue
try! Realm().beginWrite()
assertThrows(try! Realm().create(SwiftObject.self, value: values),
"Invalid property value '\(invalidValue)' for property number \(propNum)")
try! Realm().cancelWrite()
}
}
}
func testCreateWithKVCObject() {
// test with kvc object
try! Realm().beginWrite()
let objectWithInt = try! Realm().create(SwiftObject.self, value: ["intCol": 200])
let objectWithKVCObject = try! Realm().create(SwiftObject.self, value: objectWithInt)
let valueDict = defaultSwiftObjectValuesWithReplacements(["intCol": 200])
try! Realm().commitWrite()
verifySwiftObjectWithDictionaryLiteral(objectWithKVCObject, dictionary: valueDict, boolObjectValue: false,
boolObjectListValues: [])
XCTAssertEqual(try! Realm().objects(SwiftObject.self).count, 2, "Object should have been copied")
}
func testCreateWithNestedObjects() {
let standalone = SwiftPrimaryStringObject(value: ["p0", 11])
try! Realm().beginWrite()
let objectWithNestedObjects = try! Realm().create(SwiftLinkToPrimaryStringObject.self, value: ["p1", ["p1", 11],
[standalone]])
try! Realm().commitWrite()
let stringObjects = try! Realm().objects(SwiftPrimaryStringObject.self)
XCTAssertEqual(stringObjects.count, 2)
let persistedObject = stringObjects.first!
// standalone object should be copied into the realm, not added directly
XCTAssertNotEqual(standalone, persistedObject)
XCTAssertEqual(objectWithNestedObjects.object!, persistedObject)
XCTAssertEqual(objectWithNestedObjects.objects.first!, stringObjects.last!)
let standalone1 = SwiftPrimaryStringObject(value: ["p3", 11])
try! Realm().beginWrite()
assertThrows(try! Realm().create(SwiftLinkToPrimaryStringObject.self, value: ["p3", ["p3", 11], [standalone1]]),
"Should throw with duplicate primary key")
try! Realm().commitWrite()
}
func testUpdateWithNestedObjects() {
let standalone = SwiftPrimaryStringObject(value: ["primary", 11])
try! Realm().beginWrite()
let object = try! Realm().create(SwiftLinkToPrimaryStringObject.self, value: ["otherPrimary", ["primary", 12],
[["primary", 12]]], update: true)
try! Realm().commitWrite()
let stringObjects = try! Realm().objects(SwiftPrimaryStringObject.self)
XCTAssertEqual(stringObjects.count, 1)
let persistedObject = object.object!
XCTAssertEqual(persistedObject.intCol, 12)
XCTAssertNil(standalone.realm) // the standalone object should be copied, rather than added, to the realm
XCTAssertEqual(object.object!, persistedObject)
XCTAssertEqual(object.objects.first!, persistedObject)
}
func testCreateWithObjectsFromAnotherRealm() {
let values: [String: Any] = [
"boolCol": true,
"intCol": 1,
"floatCol": 1.1 as Float,
"doubleCol": 11.1,
"stringCol": "b",
"binaryCol": "b".data(using: String.Encoding.utf8)!,
"dateCol": Date(timeIntervalSince1970: 2),
"objectCol": SwiftBoolObject(value: [true]),
"arrayCol": [SwiftBoolObject(value: [true]), SwiftBoolObject()]
]
realmWithTestPath().beginWrite()
let otherRealmObject = realmWithTestPath().create(SwiftObject.self, value: values)
try! realmWithTestPath().commitWrite()
try! Realm().beginWrite()
let object = try! Realm().create(SwiftObject.self, value: otherRealmObject)
try! Realm().commitWrite()
XCTAssertNotEqual(otherRealmObject, object)
verifySwiftObjectWithDictionaryLiteral(object, dictionary: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
func testCreateWithDeeplyNestedObjectsFromAnotherRealm() {
let values: [String: Any] = [
"boolCol": true,
"intCol": 1,
"floatCol": 1.1 as Float,
"doubleCol": 11.1,
"stringCol": "b",
"binaryCol": "b".data(using: String.Encoding.utf8)!,
"dateCol": Date(timeIntervalSince1970: 2),
"objectCol": SwiftBoolObject(value: [true]),
"arrayCol": [SwiftBoolObject(value: [true]), SwiftBoolObject()]
]
let realmA = realmWithTestPath()
let realmB = try! Realm()
var realmAObject: SwiftListOfSwiftObject!
try! realmA.write {
let array = [SwiftObject(value: values), SwiftObject(value: values)]
realmAObject = realmA.create(SwiftListOfSwiftObject.self, value: ["array": array])
}
var realmBObject: SwiftListOfSwiftObject!
try! realmB.write {
realmBObject = realmB.create(SwiftListOfSwiftObject.self, value: realmAObject)
}
XCTAssertNotEqual(realmAObject, realmBObject)
XCTAssertEqual(realmBObject.array.count, 2)
for swiftObject in realmBObject.array {
verifySwiftObjectWithDictionaryLiteral(swiftObject, dictionary: values, boolObjectValue: true,
boolObjectListValues: [true, false])
}
}
func testUpdateWithObjectsFromAnotherRealm() {
realmWithTestPath().beginWrite()
let otherRealmObject = realmWithTestPath().create(SwiftLinkToPrimaryStringObject.self,
value: ["primary", NSNull(), [["2", 2], ["4", 4]]])
try! realmWithTestPath().commitWrite()
try! Realm().beginWrite()
try! Realm().create(SwiftLinkToPrimaryStringObject.self, value: ["primary", ["10", 10], [["11", 11]]])
let object = try! Realm().create(SwiftLinkToPrimaryStringObject.self, value: otherRealmObject, update: true)
try! Realm().commitWrite()
XCTAssertNotEqual(otherRealmObject, object) // the object from the other realm should be copied into this realm
XCTAssertEqual(try! Realm().objects(SwiftLinkToPrimaryStringObject.self).count, 1)
XCTAssertEqual(try! Realm().objects(SwiftPrimaryStringObject.self).count, 4)
}
func testCreateWithNSNullLinks() {
let values: [String: Any] = [
"boolCol": true,
"intCol": 1,
"floatCol": 1.1,
"doubleCol": 11.1,
"stringCol": "b",
"binaryCol": "b".data(using: String.Encoding.utf8)!,
"dateCol": Date(timeIntervalSince1970: 2),
"objectCol": NSNull(),
"arrayCol": NSNull()
]
realmWithTestPath().beginWrite()
let object = realmWithTestPath().create(SwiftObject.self, value: values)
try! realmWithTestPath().commitWrite()
XCTAssert(object.objectCol == nil) // XCTAssertNil caused a NULL deref inside _swift_getClass
XCTAssertEqual(object.arrayCol.count, 0)
}
func testCreateWithObjcName() {
let realm = try! Realm()
try! realm.write {
let object = realm.create(SwiftObjcRenamedObject.self)
object.stringCol = "string"
}
XCTAssertEqual(realm.objects(SwiftObjcRenamedObject.self).count, 1)
try! realm.write {
realm.delete(realm.objects(SwiftObjcRenamedObject.self))
}
}
func testCreateWithDifferentObjcName() {
let realm = try! Realm()
try! realm.write {
let object = realm.create(SwiftObjcArbitrarilyRenamedObject.self)
object.boolCol = true
}
XCTAssertEqual(realm.objects(SwiftObjcArbitrarilyRenamedObject.self).count, 1)
try! realm.write {
realm.delete(realm.objects(SwiftObjcArbitrarilyRenamedObject.self))
}
}
func testCreateOrUpdateNil() {
let realm = try! Realm()
realm.beginWrite()
// Create with all fields nil
let object = realm.create(SwiftOptionalPrimaryObject.self, value: SwiftOptionalPrimaryObject(), update: true)
XCTAssertNil(object.id.value)
XCTAssertNil(object.optIntCol.value)
XCTAssertNil(object.optInt8Col.value)
XCTAssertNil(object.optInt16Col.value)
XCTAssertNil(object.optInt32Col.value)
XCTAssertNil(object.optInt64Col.value)
XCTAssertNil(object.optBoolCol.value)
XCTAssertNil(object.optFloatCol.value)
XCTAssertNil(object.optDoubleCol.value)
XCTAssertNil(object.optDateCol)
XCTAssertNil(object.optStringCol)
XCTAssertNil(object.optNSStringCol)
XCTAssertNil(object.optBinaryCol)
XCTAssertNil(object.optObjectCol)
// Try to switch to non-nil
let object2 = SwiftOptionalPrimaryObject()
object2.optIntCol.value = 1
object2.optInt8Col.value = 1
object2.optInt16Col.value = 1
object2.optInt32Col.value = 1
object2.optInt64Col.value = 1
object2.optFloatCol.value = 1
object2.optDoubleCol.value = 1
object2.optBoolCol.value = true
object2.optDateCol = Date()
object2.optStringCol = ""
object2.optNSStringCol = ""
object2.optBinaryCol = Data()
object2.optObjectCol = SwiftBoolObject()
realm.create(SwiftOptionalPrimaryObject.self, value: object2, update: true)
XCTAssertNil(object.id.value)
XCTAssertNotNil(object.optIntCol.value)
XCTAssertNotNil(object.optInt8Col.value)
XCTAssertNotNil(object.optInt16Col.value)
XCTAssertNotNil(object.optInt32Col.value)
XCTAssertNotNil(object.optInt64Col.value)
XCTAssertNotNil(object.optBoolCol.value)
XCTAssertNotNil(object.optFloatCol.value)
XCTAssertNotNil(object.optDoubleCol.value)
XCTAssertNotNil(object.optDateCol)
XCTAssertNotNil(object.optStringCol)
XCTAssertNotNil(object.optNSStringCol)
XCTAssertNotNil(object.optBinaryCol)
XCTAssertNotNil(object.optObjectCol)
// Try to switch back to nil
realm.create(SwiftOptionalPrimaryObject.self, value: SwiftOptionalPrimaryObject(), update: true)
XCTAssertNil(object.id.value)
// FIXME: these should all be nil but that's a breaking change
XCTAssertNotNil(object.optIntCol.value)
XCTAssertNotNil(object.optInt8Col.value)
XCTAssertNotNil(object.optInt16Col.value)
XCTAssertNotNil(object.optInt32Col.value)
XCTAssertNotNil(object.optInt64Col.value)
XCTAssertNotNil(object.optBoolCol.value)
XCTAssertNotNil(object.optFloatCol.value)
XCTAssertNotNil(object.optDoubleCol.value)
XCTAssertNotNil(object.optDateCol)
XCTAssertNotNil(object.optStringCol)
XCTAssertNotNil(object.optNSStringCol)
XCTAssertNotNil(object.optBinaryCol)
XCTAssertNotNil(object.optObjectCol)
realm.cancelWrite()
}
// test null object
// test null list
// MARK: Add tests
func testAddWithExisingNestedObjects() {
try! Realm().beginWrite()
let existingObject = try! Realm().create(SwiftBoolObject.self)
try! Realm().commitWrite()
try! Realm().beginWrite()
let object = SwiftObject(value: ["objectCol": existingObject])
try! Realm().add(object)
try! Realm().commitWrite()
XCTAssertNotNil(object.realm)
XCTAssertEqual(object.objectCol, existingObject)
}
func testAddAndUpdateWithExisingNestedObjects() {
try! Realm().beginWrite()
let existingObject = try! Realm().create(SwiftPrimaryStringObject.self, value: ["primary", 1])
try! Realm().commitWrite()
try! Realm().beginWrite()
let object = SwiftLinkToPrimaryStringObject(value: ["primary", ["primary", 2], []])
try! Realm().add(object, update: true)
try! Realm().commitWrite()
XCTAssertNotNil(object.realm)
XCTAssertEqual(object.object!, existingObject) // the existing object should be updated
XCTAssertEqual(existingObject.intCol, 2)
}
func testAddObjectCycle() {
weak var weakObj1: SwiftCircleObject? = nil, weakObj2: SwiftCircleObject? = nil
autoreleasepool {
let obj1 = SwiftCircleObject(value: [])
let obj2 = SwiftCircleObject(value: [obj1, [obj1]])
obj1.obj = obj2
obj1.array.append(obj2)
weakObj1 = obj1
weakObj2 = obj2
let realm = try! Realm()
try! realm.write {
realm.add(obj1)
}
XCTAssertEqual(obj1.realm, realm)
XCTAssertEqual(obj2.realm, realm)
}
XCTAssertNil(weakObj1)
XCTAssertNil(weakObj2)
}
func testAddOrUpdateNil() {
let realm = try! Realm()
realm.beginWrite()
// Create with all fields nil
let object = SwiftOptionalPrimaryObject()
realm.add(object)
XCTAssertNil(object.id.value)
XCTAssertNil(object.optIntCol.value)
XCTAssertNil(object.optInt8Col.value)
XCTAssertNil(object.optInt16Col.value)
XCTAssertNil(object.optInt32Col.value)
XCTAssertNil(object.optInt64Col.value)
XCTAssertNil(object.optBoolCol.value)
XCTAssertNil(object.optFloatCol.value)
XCTAssertNil(object.optDoubleCol.value)
XCTAssertNil(object.optDateCol)
XCTAssertNil(object.optStringCol)
XCTAssertNil(object.optNSStringCol)
XCTAssertNil(object.optBinaryCol)
XCTAssertNil(object.optObjectCol)
// Try to switch to non-nil
let object2 = SwiftOptionalPrimaryObject()
object2.optIntCol.value = 1
object2.optInt8Col.value = 1
object2.optInt16Col.value = 1
object2.optInt32Col.value = 1
object2.optInt64Col.value = 1
object2.optFloatCol.value = 1
object2.optDoubleCol.value = 1
object2.optBoolCol.value = true
object2.optDateCol = Date()
object2.optStringCol = ""
object2.optNSStringCol = ""
object2.optBinaryCol = Data()
object2.optObjectCol = SwiftBoolObject()
realm.add(object2, update: true)
XCTAssertNil(object.id.value)
XCTAssertNotNil(object.optIntCol.value)
XCTAssertNotNil(object.optInt8Col.value)
XCTAssertNotNil(object.optInt16Col.value)
XCTAssertNotNil(object.optInt32Col.value)
XCTAssertNotNil(object.optInt64Col.value)
XCTAssertNotNil(object.optBoolCol.value)
XCTAssertNotNil(object.optFloatCol.value)
XCTAssertNotNil(object.optDoubleCol.value)
XCTAssertNotNil(object.optDateCol)
XCTAssertNotNil(object.optStringCol)
XCTAssertNotNil(object.optNSStringCol)
XCTAssertNotNil(object.optBinaryCol)
XCTAssertNotNil(object.optObjectCol)
// Try to switch back to nil
let object3 = SwiftOptionalPrimaryObject()
realm.add(object3, update: true)
XCTAssertNil(object.id.value)
XCTAssertNil(object.optIntCol.value)
XCTAssertNil(object.optInt8Col.value)
XCTAssertNil(object.optInt16Col.value)
XCTAssertNil(object.optInt32Col.value)
XCTAssertNil(object.optInt64Col.value)
XCTAssertNil(object.optBoolCol.value)
XCTAssertNil(object.optFloatCol.value)
XCTAssertNil(object.optDoubleCol.value)
XCTAssertNil(object.optDateCol)
XCTAssertNil(object.optStringCol)
XCTAssertNil(object.optNSStringCol)
XCTAssertNil(object.optBinaryCol)
XCTAssertNil(object.optObjectCol)
realm.cancelWrite()
}
// MARK: Private utilities
private func verifySwiftObjectWithArrayLiteral(_ object: SwiftObject, array: [Any], boolObjectValue: Bool,
boolObjectListValues: [Bool]) {
XCTAssertEqual(object.boolCol, (array[0] as! Bool))
XCTAssertEqual(object.intCol, (array[1] as! Int))
//XCTAssertEqual(object.floatCol, (array[2] as! Float)) // FIXME: crashes with swift 3.2
XCTAssertEqual(object.doubleCol, (array[3] as! Double))
XCTAssertEqual(object.stringCol, (array[4] as! String))
XCTAssertEqual(object.binaryCol, (array[5] as! Data))
XCTAssertEqual(object.dateCol, (array[6] as! Date))
XCTAssertEqual(object.objectCol!.boolCol, boolObjectValue)
XCTAssertEqual(object.arrayCol.count, boolObjectListValues.count)
for i in 0..<boolObjectListValues.count {
XCTAssertEqual(object.arrayCol[i].boolCol, boolObjectListValues[i])
}
}
private func verifySwiftObjectWithDictionaryLiteral(_ object: SwiftObject, dictionary: [String: Any],
boolObjectValue: Bool, boolObjectListValues: [Bool]) {
XCTAssertEqual(object.boolCol, (dictionary["boolCol"] as! Bool))
XCTAssertEqual(object.intCol, (dictionary["intCol"] as! Int))
//XCTAssertEqual(object.floatCol, (dictionary["floatCol"] as! Float)) // FIXME: crashes with swift 3.2
XCTAssertEqual(object.doubleCol, (dictionary["doubleCol"] as! Double))
XCTAssertEqual(object.stringCol, (dictionary["stringCol"] as! String))
XCTAssertEqual(object.binaryCol, (dictionary["binaryCol"] as! Data))
XCTAssertEqual(object.dateCol, (dictionary["dateCol"] as! Date))
XCTAssertEqual(object.objectCol!.boolCol, boolObjectValue)
XCTAssertEqual(object.arrayCol.count, boolObjectListValues.count)
for i in 0..<boolObjectListValues.count {
XCTAssertEqual(object.arrayCol[i].boolCol, boolObjectListValues[i])
}
}
private func verifySwiftOptionalObjectWithDictionaryLiteral(_ object: SwiftOptionalDefaultValuesObject,
dictionary: [String: Any],
boolObjectValue: Bool?) {
XCTAssertEqual(object.optBoolCol.value, (dictionary["optBoolCol"] as! Bool?))
XCTAssertEqual(object.optIntCol.value, (dictionary["optIntCol"] as! Int?))
XCTAssertEqual(object.optInt8Col.value,
((dictionary["optInt8Col"] as! NSNumber?)?.int8Value).map({Int8($0)}))
XCTAssertEqual(object.optInt16Col.value,
((dictionary["optInt16Col"] as! NSNumber?)?.int16Value).map({Int16($0)}))
XCTAssertEqual(object.optInt32Col.value,
((dictionary["optInt32Col"] as! NSNumber?)?.int32Value).map({Int32($0)}))
XCTAssertEqual(object.optInt64Col.value, (dictionary["optInt64Col"] as! NSNumber?)?.int64Value)
XCTAssertEqual(object.optFloatCol.value, (dictionary["optFloatCol"] as! Float?))
XCTAssertEqual(object.optDoubleCol.value, (dictionary["optDoubleCol"] as! Double?))
XCTAssertEqual(object.optStringCol, (dictionary["optStringCol"] as! String?))
XCTAssertEqual(object.optNSStringCol, (dictionary["optNSStringCol"] as! NSString))
XCTAssertEqual(object.optBinaryCol, (dictionary["optBinaryCol"] as! Data?))
XCTAssertEqual(object.optDateCol, (dictionary["optDateCol"] as! Date?))
XCTAssertEqual(object.optObjectCol?.boolCol, boolObjectValue)
}
private func defaultSwiftObjectValuesWithReplacements(_ replace: [String: Any]) -> [String: Any] {
var valueDict = SwiftObject.defaultValues()
for (key, value) in replace {
valueDict[key] = value
}
return valueDict
}
// return an array of valid values that can be used to initialize each type
// swiftlint:disable:next cyclomatic_complexity
private func validValuesForSwiftObjectType(_ type: PropertyType) -> [Any] {
try! Realm().beginWrite()
let persistedObject = try! Realm().create(SwiftBoolObject.self, value: [true])
try! Realm().commitWrite()
switch type {
case .bool: return [true, NSNumber(value: 0 as Int), NSNumber(value: 1 as Int)]
case .int: return [NSNumber(value: 1 as Int)]
case .float: return [NSNumber(value: 1 as Int), NSNumber(value: 1.1 as Float), NSNumber(value: 11.1 as Double)]
case .double: return [NSNumber(value: 1 as Int), NSNumber(value: 1.1 as Float), NSNumber(value: 11.1 as Double)]
case .string: return ["b"]
case .data: return ["b".data(using: String.Encoding.utf8, allowLossyConversion: false)!]
case .date: return [Date(timeIntervalSince1970: 2)]
case .object: return [[true], ["boolCol": true], SwiftBoolObject(value: [true]), persistedObject]
case .array: return [
[[true], [false]],
[["boolCol": true], ["boolCol": false]],
[SwiftBoolObject(value: [true]), SwiftBoolObject(value: [false])],
[persistedObject, [false]]
]
case .any: XCTFail("not supported")
case .linkingObjects: XCTFail("not supported")
}
return []
}
// swiftlint:disable:next cyclomatic_complexity
private func invalidValuesForSwiftObjectType(_ type: PropertyType) -> [Any] {
try! Realm().beginWrite()
let persistedObject = try! Realm().create(SwiftIntObject.self)
try! Realm().commitWrite()
switch type {
case .bool: return ["invalid", NSNumber(value: 2 as Int), NSNumber(value: 1.1 as Float), NSNumber(value: 11.1 as Double)]
case .int: return ["invalid", NSNumber(value: 1.1 as Float), NSNumber(value: 11.1 as Double)]
case .float: return ["invalid", true, false]
case .double: return ["invalid", true, false]
case .string: return [0x197A71D, true, false]
case .data: return ["invalid"]
case .date: return ["invalid"]
case .object: return ["invalid", ["a"], ["boolCol": "a"], SwiftIntObject()]
case .array: return ["invalid", [["a"]], [["boolCol": "a"]], [[SwiftIntObject()]], [[persistedObject]]]
case .any: XCTFail("not supported")
case .linkingObjects: XCTFail("not supported")
}
return []
}
}