SwiftDynamicTests.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2014 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. import XCTest
  19. import Foundation
  20. import Realm.Private
  21. import Realm.Dynamic
  22. class SwiftDynamicTests: RLMTestCase {
  23. // Swift models
  24. func testDynamicRealmExists() {
  25. autoreleasepool {
  26. // open realm in autoreleasepool to create tables and then dispose
  27. let realm = RLMRealm(url: RLMTestRealmURL())
  28. realm.beginWriteTransaction()
  29. _ = SwiftDynamicObject.create(in: realm, withValue: ["column1", 1])
  30. _ = SwiftDynamicObject.create(in: realm, withValue: ["column2", 2])
  31. try! realm.commitWriteTransaction()
  32. }
  33. let dyrealm = realm(withTestPathAndSchema: nil)
  34. XCTAssertNotNil(dyrealm, "realm should not be nil")
  35. // verify schema
  36. let dynSchema = dyrealm.schema[SwiftDynamicObject.className()]
  37. XCTAssertNotNil(dynSchema, "Should be able to get object schema dynamically")
  38. XCTAssertEqual(dynSchema.properties.count, Int(2))
  39. XCTAssertEqual(dynSchema.properties[0].name, "stringCol")
  40. XCTAssertEqual(dynSchema.properties[1].type, RLMPropertyType.int)
  41. // verify object type
  42. let array = SwiftDynamicObject.allObjects(in: dyrealm)
  43. XCTAssertEqual(array.count, UInt(2))
  44. XCTAssertEqual(array.objectClassName, SwiftDynamicObject.className())
  45. }
  46. func testDynamicProperties() {
  47. autoreleasepool {
  48. // open realm in autoreleasepool to create tables and then dispose
  49. let realm = RLMRealm(url: RLMTestRealmURL())
  50. realm.beginWriteTransaction()
  51. _ = SwiftDynamicObject.create(in: realm, withValue: ["column1", 1])
  52. _ = SwiftDynamicObject.create(in: realm, withValue: ["column2", 2])
  53. try! realm.commitWriteTransaction()
  54. }
  55. // verify properties
  56. let dyrealm = realm(withTestPathAndSchema: nil)
  57. let array = dyrealm.allObjects("SwiftDynamicObject")
  58. XCTAssertTrue(array[0]["intCol"] as! NSNumber == 1)
  59. XCTAssertTrue(array[1]["stringCol"] as! String == "column2")
  60. }
  61. // Objective-C models
  62. func testDynamicRealmExists_objc() {
  63. autoreleasepool {
  64. // open realm in autoreleasepool to create tables and then dispose
  65. let realm = RLMRealm(url: RLMTestRealmURL())
  66. realm.beginWriteTransaction()
  67. _ = DynamicObject.create(in: realm, withValue: ["column1", 1])
  68. _ = DynamicObject.create(in: realm, withValue: ["column2", 2])
  69. try! realm.commitWriteTransaction()
  70. }
  71. let dyrealm = realm(withTestPathAndSchema: nil)
  72. XCTAssertNotNil(dyrealm, "realm should not be nil")
  73. // verify schema
  74. let dynSchema = dyrealm.schema[DynamicObject.className()]
  75. XCTAssertNotNil(dynSchema, "Should be able to get object schema dynamically")
  76. XCTAssertTrue(dynSchema.properties.count == 2)
  77. XCTAssertTrue(dynSchema.properties[0].name == "stringCol")
  78. XCTAssertTrue(dynSchema.properties[1].type == RLMPropertyType.int)
  79. // verify object type
  80. let array = DynamicObject.allObjects(in: dyrealm)
  81. XCTAssertEqual(array.count, UInt(2))
  82. XCTAssertEqual(array.objectClassName, DynamicObject.className())
  83. }
  84. func testDynamicProperties_objc() {
  85. autoreleasepool {
  86. // open realm in autoreleasepool to create tables and then dispose
  87. let realm = RLMRealm(url: RLMTestRealmURL())
  88. realm.beginWriteTransaction()
  89. _ = DynamicObject.create(in: realm, withValue: ["column1", 1])
  90. _ = DynamicObject.create(in: realm, withValue: ["column2", 2])
  91. try! realm.commitWriteTransaction()
  92. }
  93. // verify properties
  94. let dyrealm = realm(withTestPathAndSchema: nil)
  95. let array = dyrealm.allObjects("DynamicObject")
  96. XCTAssertTrue(array[0]["intCol"] as! NSNumber == 1)
  97. XCTAssertTrue(array[1]["stringCol"] as! String == "column2")
  98. }
  99. func testDynamicTypes_objc() {
  100. let date = Date(timeIntervalSince1970: 100000)
  101. let data = "a".data(using: String.Encoding.utf8)!
  102. let obj1: [Any] = [true, 1, 1.1 as Float, 1.11, "string",
  103. data, date, true, 11, NSNull()]
  104. let obj = StringObject()
  105. obj.stringCol = "string"
  106. let data2 = "b".data(using: String.Encoding.utf8)!
  107. let obj2: [Any] = [false, 2, 2.2 as Float, 2.22, "string2",
  108. data2, date, false, 22, obj]
  109. autoreleasepool {
  110. // open realm in autoreleasepool to create tables and then dispose
  111. let realm = self.realmWithTestPath()
  112. realm.beginWriteTransaction()
  113. _ = AllTypesObject.create(in: realm, withValue: obj1)
  114. _ = AllTypesObject.create(in: realm, withValue: obj2)
  115. try! realm.commitWriteTransaction()
  116. }
  117. // verify properties
  118. let dyrealm = realm(withTestPathAndSchema: nil)
  119. let results = dyrealm.allObjects(AllTypesObject.className())
  120. XCTAssertEqual(results.count, UInt(2))
  121. let robj1 = results[0]
  122. let robj2 = results[1]
  123. let schema = dyrealm.schema[AllTypesObject.className()]
  124. for idx in 0..<obj1.count - 1 {
  125. let prop = schema.properties[idx]
  126. XCTAssertTrue((obj1[idx] as AnyObject).isEqual(robj1[prop.name]))
  127. XCTAssertTrue((obj2[idx] as AnyObject).isEqual(robj2[prop.name]))
  128. }
  129. // check sub object type
  130. XCTAssertTrue(schema.properties[9].objectClassName! == "StringObject")
  131. // check object equality
  132. XCTAssertNil(robj1["objectCol"], "object should be nil")
  133. XCTAssertTrue((robj2["objectCol"] as! RLMObject)["stringCol"] as! String == "string")
  134. }
  135. }