ObjectSchemaInitializationTests.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 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 RealmSwift
  20. import Realm.Private
  21. import Realm.Dynamic
  22. import Foundation
  23. class ObjectSchemaInitializationTests: TestCase {
  24. func testAllValidTypes() {
  25. let object = SwiftObject()
  26. let objectSchema = object.objectSchema
  27. let noSuchCol = objectSchema["noSuchCol"]
  28. XCTAssertNil(noSuchCol)
  29. let boolCol = objectSchema["boolCol"]
  30. XCTAssertNotNil(boolCol)
  31. XCTAssertEqual(boolCol!.name, "boolCol")
  32. XCTAssertEqual(boolCol!.type, PropertyType.bool)
  33. XCTAssertFalse(boolCol!.isIndexed)
  34. XCTAssertFalse(boolCol!.isOptional)
  35. XCTAssertNil(boolCol!.objectClassName)
  36. let intCol = objectSchema["intCol"]
  37. XCTAssertNotNil(intCol)
  38. XCTAssertEqual(intCol!.name, "intCol")
  39. XCTAssertEqual(intCol!.type, PropertyType.int)
  40. XCTAssertFalse(intCol!.isIndexed)
  41. XCTAssertFalse(intCol!.isOptional)
  42. XCTAssertNil(intCol!.objectClassName)
  43. let floatCol = objectSchema["floatCol"]
  44. XCTAssertNotNil(floatCol)
  45. XCTAssertEqual(floatCol!.name, "floatCol")
  46. XCTAssertEqual(floatCol!.type, PropertyType.float)
  47. XCTAssertFalse(floatCol!.isIndexed)
  48. XCTAssertFalse(floatCol!.isOptional)
  49. XCTAssertNil(floatCol!.objectClassName)
  50. let doubleCol = objectSchema["doubleCol"]
  51. XCTAssertNotNil(doubleCol)
  52. XCTAssertEqual(doubleCol!.name, "doubleCol")
  53. XCTAssertEqual(doubleCol!.type, PropertyType.double)
  54. XCTAssertFalse(doubleCol!.isIndexed)
  55. XCTAssertFalse(doubleCol!.isOptional)
  56. XCTAssertNil(doubleCol!.objectClassName)
  57. let stringCol = objectSchema["stringCol"]
  58. XCTAssertNotNil(stringCol)
  59. XCTAssertEqual(stringCol!.name, "stringCol")
  60. XCTAssertEqual(stringCol!.type, PropertyType.string)
  61. XCTAssertFalse(stringCol!.isIndexed)
  62. XCTAssertFalse(stringCol!.isOptional)
  63. XCTAssertNil(stringCol!.objectClassName)
  64. let binaryCol = objectSchema["binaryCol"]
  65. XCTAssertNotNil(binaryCol)
  66. XCTAssertEqual(binaryCol!.name, "binaryCol")
  67. XCTAssertEqual(binaryCol!.type, PropertyType.data)
  68. XCTAssertFalse(binaryCol!.isIndexed)
  69. XCTAssertFalse(binaryCol!.isOptional)
  70. XCTAssertNil(binaryCol!.objectClassName)
  71. let dateCol = objectSchema["dateCol"]
  72. XCTAssertNotNil(dateCol)
  73. XCTAssertEqual(dateCol!.name, "dateCol")
  74. XCTAssertEqual(dateCol!.type, PropertyType.date)
  75. XCTAssertFalse(dateCol!.isIndexed)
  76. XCTAssertFalse(dateCol!.isOptional)
  77. XCTAssertNil(dateCol!.objectClassName)
  78. let objectCol = objectSchema["objectCol"]
  79. XCTAssertNotNil(objectCol)
  80. XCTAssertEqual(objectCol!.name, "objectCol")
  81. XCTAssertEqual(objectCol!.type, PropertyType.object)
  82. XCTAssertFalse(objectCol!.isIndexed)
  83. XCTAssertTrue(objectCol!.isOptional)
  84. XCTAssertEqual(objectCol!.objectClassName!, "SwiftBoolObject")
  85. let arrayCol = objectSchema["arrayCol"]
  86. XCTAssertNotNil(arrayCol)
  87. XCTAssertEqual(arrayCol!.name, "arrayCol")
  88. XCTAssertEqual(arrayCol!.type, PropertyType.object)
  89. XCTAssertTrue(arrayCol!.isArray)
  90. XCTAssertFalse(arrayCol!.isIndexed)
  91. XCTAssertFalse(arrayCol!.isOptional)
  92. XCTAssertEqual(objectCol!.objectClassName!, "SwiftBoolObject")
  93. let dynamicArrayCol = SwiftCompanyObject().objectSchema["employees"]
  94. XCTAssertNotNil(dynamicArrayCol)
  95. XCTAssertEqual(dynamicArrayCol!.name, "employees")
  96. XCTAssertEqual(dynamicArrayCol!.type, PropertyType.object)
  97. XCTAssertTrue(dynamicArrayCol!.isArray)
  98. XCTAssertFalse(dynamicArrayCol!.isIndexed)
  99. XCTAssertFalse(arrayCol!.isOptional)
  100. XCTAssertEqual(dynamicArrayCol!.objectClassName!, "SwiftEmployeeObject")
  101. }
  102. func testInvalidObjects() {
  103. // Should be able to get a schema for a non-RLMObjectBase subclass
  104. let schema = RLMObjectSchema(forObjectClass: SwiftFakeObjectSubclass.self)
  105. XCTAssertEqual(schema.properties.count, 1)
  106. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithAnyObject.self),
  107. "Should throw when not ignoring a property of a type we can't persist")
  108. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithStringArray.self),
  109. "Should throw when not ignoring a property of a type we can't persist")
  110. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithOptionalStringArray.self),
  111. "Should throw when not ignoring a property of a type we can't persist")
  112. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithBadPropertyName.self),
  113. "Should throw when not ignoring a property with a name we don't support")
  114. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithManagedLazyProperty.self),
  115. "Should throw when not ignoring a lazy property")
  116. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithDynamicManagedLazyProperty.self),
  117. "Should throw when not ignoring a lazy property")
  118. // Shouldn't throw when not ignoring a property of a type we can't persist if it's not dynamic
  119. _ = RLMObjectSchema(forObjectClass: SwiftObjectWithEnum.self)
  120. // Shouldn't throw when not ignoring a property of a type we can't persist if it's not dynamic
  121. _ = RLMObjectSchema(forObjectClass: SwiftObjectWithStruct.self)
  122. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithDatePrimaryKey.self),
  123. "Should throw when setting a non int/string primary key")
  124. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithNSURL.self),
  125. "Should throw when not ignoring a property of a type we can't persist")
  126. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithNonOptionalLinkProperty.self),
  127. "Should throw when not marking a link property as optional")
  128. }
  129. func testPrimaryKey() {
  130. XCTAssertNil(SwiftObject().objectSchema.primaryKeyProperty,
  131. "Object should default to having no primary key property")
  132. XCTAssertEqual(SwiftPrimaryStringObject().objectSchema.primaryKeyProperty!.name, "stringCol")
  133. }
  134. func testIgnoredProperties() {
  135. let schema = SwiftIgnoredPropertiesObject().objectSchema
  136. XCTAssertNil(schema["runtimeProperty"], "The object schema shouldn't contain ignored properties")
  137. XCTAssertNil(schema["runtimeDefaultProperty"], "The object schema shouldn't contain ignored properties")
  138. XCTAssertNil(schema["readOnlyProperty"], "The object schema shouldn't contain read-only properties")
  139. }
  140. func testIndexedProperties() {
  141. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["stringCol"]!.isIndexed)
  142. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["intCol"]!.isIndexed)
  143. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["int8Col"]!.isIndexed)
  144. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["int16Col"]!.isIndexed)
  145. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["int32Col"]!.isIndexed)
  146. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["int64Col"]!.isIndexed)
  147. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["boolCol"]!.isIndexed)
  148. XCTAssertTrue(SwiftIndexedPropertiesObject().objectSchema["dateCol"]!.isIndexed)
  149. XCTAssertFalse(SwiftIndexedPropertiesObject().objectSchema["floatCol"]!.isIndexed)
  150. XCTAssertFalse(SwiftIndexedPropertiesObject().objectSchema["doubleCol"]!.isIndexed)
  151. XCTAssertFalse(SwiftIndexedPropertiesObject().objectSchema["dataCol"]!.isIndexed)
  152. }
  153. func testOptionalProperties() {
  154. let schema = RLMObjectSchema(forObjectClass: SwiftOptionalObject.self)
  155. for prop in schema.properties {
  156. XCTAssertTrue(prop.optional)
  157. }
  158. let types = Set(schema.properties.map { $0.type })
  159. XCTAssertEqual(types, Set([.string, .string, .data, .date, .object, .int, .float, .double, .bool]))
  160. }
  161. func testImplicitlyUnwrappedOptionalsAreParsedAsOptionals() {
  162. let schema = SwiftImplicitlyUnwrappedOptionalObject().objectSchema
  163. XCTAssertTrue(schema["optObjectCol"]!.isOptional)
  164. XCTAssertTrue(schema["optNSStringCol"]!.isOptional)
  165. XCTAssertTrue(schema["optStringCol"]!.isOptional)
  166. XCTAssertTrue(schema["optBinaryCol"]!.isOptional)
  167. XCTAssertTrue(schema["optDateCol"]!.isOptional)
  168. }
  169. func testNonRealmOptionalTypesDeclaredAsRealmOptional() {
  170. assertThrows(RLMObjectSchema(forObjectClass: SwiftObjectWithNonRealmOptionalType.self))
  171. }
  172. func testNotExplicitlyIgnoredComputedProperties() {
  173. let schema = SwiftComputedPropertyNotIgnoredObject().objectSchema
  174. // The two computed properties should not appear on the schema.
  175. XCTAssertEqual(schema.properties.count, 1)
  176. XCTAssertNotNil(schema["_urlBacking"])
  177. }
  178. }
  179. class SwiftFakeObject: Object {
  180. override class func _realmIgnoreClass() -> Bool { return true }
  181. }
  182. class SwiftObjectWithNSURL: SwiftFakeObject {
  183. @objc dynamic var url = NSURL(string: "http://realm.io")!
  184. }
  185. class SwiftObjectWithAnyObject: SwiftFakeObject {
  186. @objc dynamic var anyObject: AnyObject = NSObject()
  187. }
  188. class SwiftObjectWithStringArray: SwiftFakeObject {
  189. @objc dynamic var stringArray = [String]()
  190. }
  191. class SwiftObjectWithOptionalStringArray: SwiftFakeObject {
  192. @objc dynamic var stringArray: [String]?
  193. }
  194. enum SwiftEnum {
  195. case case1
  196. case case2
  197. }
  198. class SwiftObjectWithEnum: SwiftFakeObject {
  199. var swiftEnum = SwiftEnum.case1
  200. }
  201. class SwiftObjectWithStruct: SwiftFakeObject {
  202. var swiftStruct = SortDescriptor(keyPath: "prop")
  203. }
  204. class SwiftObjectWithDatePrimaryKey: SwiftFakeObject {
  205. @objc dynamic var date = Date()
  206. override class func primaryKey() -> String? {
  207. return "date"
  208. }
  209. }
  210. class SwiftFakeObjectSubclass: SwiftFakeObject {
  211. @objc dynamic var dateCol = Date()
  212. }
  213. // swiftlint:disable:next type_name
  214. class SwiftObjectWithNonNullableOptionalProperties: SwiftFakeObject {
  215. @objc dynamic var optDateCol: Date?
  216. }
  217. class SwiftObjectWithNonOptionalLinkProperty: SwiftFakeObject {
  218. @objc dynamic var objectCol = SwiftBoolObject()
  219. }
  220. extension Set: RealmOptionalType { }
  221. class SwiftObjectWithNonRealmOptionalType: SwiftFakeObject {
  222. let set = RealmOptional<Set<Int>>()
  223. }
  224. class SwiftObjectWithBadPropertyName: SwiftFakeObject {
  225. @objc dynamic var newValue = false
  226. }
  227. class SwiftObjectWithManagedLazyProperty: SwiftFakeObject {
  228. lazy var foobar: String = "foo"
  229. }
  230. // swiftlint:disable:next type_name
  231. class SwiftObjectWithDynamicManagedLazyProperty: SwiftFakeObject {
  232. @objc dynamic lazy var foobar: String = "foo"
  233. }