ObjectSchemaTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class ObjectSchemaTests: TestCase {
  21. var objectSchema: ObjectSchema!
  22. var swiftObjectSchema: ObjectSchema {
  23. return try! Realm().schema["SwiftObject"]!
  24. }
  25. func testProperties() {
  26. let objectSchema = swiftObjectSchema
  27. let propertyNames = objectSchema.properties.map { $0.name }
  28. XCTAssertEqual(propertyNames,
  29. ["boolCol", "intCol", "intEnumCol", "floatCol", "doubleCol",
  30. "stringCol", "binaryCol", "dateCol", "objectCol", "arrayCol"]
  31. )
  32. }
  33. // Cannot name testClassName() because it interferes with the method on XCTest
  34. func testClassNameProperty() {
  35. let objectSchema = swiftObjectSchema
  36. XCTAssertEqual(objectSchema.className, "SwiftObject")
  37. }
  38. func testObjectClass() {
  39. let objectSchema = swiftObjectSchema
  40. XCTAssertTrue(objectSchema.objectClass === SwiftObject.self)
  41. }
  42. func testPrimaryKeyProperty() {
  43. let objectSchema = swiftObjectSchema
  44. XCTAssertNil(objectSchema.primaryKeyProperty)
  45. XCTAssertEqual(try! Realm().schema["SwiftPrimaryStringObject"]!.primaryKeyProperty!.name, "stringCol")
  46. }
  47. func testDescription() {
  48. let objectSchema = swiftObjectSchema
  49. let expected =
  50. "SwiftObject {\n" +
  51. " boolCol {\n" +
  52. " type = bool;\n" +
  53. " objectClassName = (null);\n" +
  54. " linkOriginPropertyName = (null);\n" +
  55. " indexed = NO;\n" +
  56. " isPrimary = NO;\n" +
  57. " array = NO;\n" +
  58. " optional = NO;\n" +
  59. " }\n" +
  60. " intCol {\n" +
  61. " type = int;\n" +
  62. " objectClassName = (null);\n" +
  63. " linkOriginPropertyName = (null);\n" +
  64. " indexed = NO;\n" +
  65. " isPrimary = NO;\n" +
  66. " array = NO;\n" +
  67. " optional = NO;\n" +
  68. " }\n" +
  69. " intEnumCol {\n" +
  70. " type = int;\n" +
  71. " objectClassName = (null);\n" +
  72. " linkOriginPropertyName = (null);\n" +
  73. " indexed = NO;\n" +
  74. " isPrimary = NO;\n" +
  75. " array = NO;\n" +
  76. " optional = NO;\n" +
  77. " }\n" +
  78. " floatCol {\n" +
  79. " type = float;\n" +
  80. " objectClassName = (null);\n" +
  81. " linkOriginPropertyName = (null);\n" +
  82. " indexed = NO;\n" +
  83. " isPrimary = NO;\n" +
  84. " array = NO;\n" +
  85. " optional = NO;\n" +
  86. " }\n" +
  87. " doubleCol {\n" +
  88. " type = double;\n" +
  89. " objectClassName = (null);\n" +
  90. " linkOriginPropertyName = (null);\n" +
  91. " indexed = NO;\n" +
  92. " isPrimary = NO;\n" +
  93. " array = NO;\n" +
  94. " optional = NO;\n" +
  95. " }\n" +
  96. " stringCol {\n" +
  97. " type = string;\n" +
  98. " objectClassName = (null);\n" +
  99. " linkOriginPropertyName = (null);\n" +
  100. " indexed = NO;\n" +
  101. " isPrimary = NO;\n" +
  102. " array = NO;\n" +
  103. " optional = NO;\n" +
  104. " }\n" +
  105. " binaryCol {\n" +
  106. " type = data;\n" +
  107. " objectClassName = (null);\n" +
  108. " linkOriginPropertyName = (null);\n" +
  109. " indexed = NO;\n" +
  110. " isPrimary = NO;\n" +
  111. " array = NO;\n" +
  112. " optional = NO;\n" +
  113. " }\n" +
  114. " dateCol {\n" +
  115. " type = date;\n" +
  116. " objectClassName = (null);\n" +
  117. " linkOriginPropertyName = (null);\n" +
  118. " indexed = NO;\n" +
  119. " isPrimary = NO;\n" +
  120. " array = NO;\n" +
  121. " optional = NO;\n" +
  122. " }\n" +
  123. " objectCol {\n" +
  124. " type = object;\n" +
  125. " objectClassName = SwiftBoolObject;\n" +
  126. " linkOriginPropertyName = (null);\n" +
  127. " indexed = NO;\n" +
  128. " isPrimary = NO;\n" +
  129. " array = NO;\n" +
  130. " optional = YES;\n" +
  131. " }\n" +
  132. " arrayCol {\n" +
  133. " type = object;\n" +
  134. " objectClassName = SwiftBoolObject;\n" +
  135. " linkOriginPropertyName = (null);\n" +
  136. " indexed = NO;\n" +
  137. " isPrimary = NO;\n" +
  138. " array = YES;\n" +
  139. " optional = NO;\n" +
  140. " }\n" +
  141. "}"
  142. XCTAssertEqual(objectSchema.description, expected.replacingOccurrences(of: " ", with: "\t"))
  143. }
  144. func testSubscript() {
  145. let objectSchema = swiftObjectSchema
  146. XCTAssertNil(objectSchema["noSuchProperty"])
  147. XCTAssertEqual(objectSchema["boolCol"]!.name, "boolCol")
  148. }
  149. func testEquals() {
  150. let objectSchema = swiftObjectSchema
  151. XCTAssert(try! objectSchema == Realm().schema["SwiftObject"]!)
  152. XCTAssert(try! objectSchema != Realm().schema["SwiftStringObject"]!)
  153. }
  154. }