ObjectSchemaTests.swift 5.6 KB

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