ObjectSchemaTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "RLMTestCase.h"
  19. #import "RLMObjectSchema_Private.h"
  20. #pragma mark - Test Objects
  21. @interface IndexedObject : RLMObject
  22. @property NSString *stringCol;
  23. @property NSInteger integerCol;
  24. @property int intCol;
  25. @property long longCol;
  26. @property long long longlongCol;
  27. @property BOOL boolCol;
  28. @property NSDate *dateCol;
  29. @property NSNumber<RLMInt> *optionalIntCol;
  30. @property NSNumber<RLMBool> *optionalBoolCol;
  31. @property float floatCol;
  32. @property double doubleCol;
  33. @property NSData *dataCol;
  34. @property NSNumber<RLMFloat> *optionalFloatCol;
  35. @property NSNumber<RLMDouble> *optionalDoubleCol;
  36. @end
  37. @implementation IndexedObject
  38. + (NSArray *)indexedProperties {
  39. return @[@"stringCol", @"integerCol", @"intCol", @"longCol", @"longlongCol",
  40. @"boolCol", @"dateCol", @"optionalIntCol", @"optionalBoolCol"];
  41. }
  42. @end
  43. #pragma mark - Tests
  44. @interface ObjectSchemaTests : RLMTestCase
  45. @end
  46. @implementation ObjectSchemaTests
  47. - (void)testDescription {
  48. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[PrimaryStringObject class]];
  49. XCTAssertEqualObjects(objectSchema.description, @"PrimaryStringObject {\n"
  50. @"\tstringCol {\n"
  51. @"\t\ttype = string;\n"
  52. @"\t\tobjectClassName = (null);\n"
  53. @"\t\tlinkOriginPropertyName = (null);\n"
  54. @"\t\tindexed = YES;\n"
  55. @"\t\tisPrimary = YES;\n"
  56. @"\t\tarray = NO;\n"
  57. @"\t\toptional = NO;\n"
  58. @"\t}\n"
  59. @"\tintCol {\n"
  60. @"\t\ttype = int;\n"
  61. @"\t\tobjectClassName = (null);\n"
  62. @"\t\tlinkOriginPropertyName = (null);\n"
  63. @"\t\tindexed = NO;\n"
  64. @"\t\tisPrimary = NO;\n"
  65. @"\t\tarray = NO;\n"
  66. @"\t\toptional = NO;\n"
  67. @"\t}\n"
  68. @"}");
  69. }
  70. - (void)testObjectForKeyedSubscript {
  71. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[PrimaryStringObject class]];
  72. XCTAssertEqualObjects(objectSchema[@"stringCol"].name, @"stringCol");
  73. XCTAssertEqualObjects(objectSchema[@"intCol"].name, @"intCol");
  74. XCTAssertNil(objectSchema[@"missing"]);
  75. }
  76. - (void)testPrimaryKeyProperty {
  77. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[PrimaryStringObject class]];
  78. XCTAssertEqualObjects(objectSchema.primaryKeyProperty.name, @"stringCol");
  79. objectSchema = [RLMObjectSchema schemaForObjectClass:[StringObject class]];
  80. XCTAssertNil(objectSchema.primaryKeyProperty);
  81. }
  82. #pragma mark - Schema Discovery
  83. - (void)testIgnoredUnsupportedProperty {
  84. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[IgnoredURLObject class]];
  85. XCTAssertEqual(1U, objectSchema.properties.count);
  86. XCTAssertEqualObjects(objectSchema.properties[0].name, @"name");
  87. }
  88. - (void)testReadOnlyPropertiesImplicitlyIgnored {
  89. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:[ReadOnlyPropertyObject class]];
  90. XCTAssertEqual(1U, objectSchema.properties.count);
  91. XCTAssertEqualObjects(objectSchema.properties[0].name, @"readOnlyPropertyMadeReadWriteInClassExtension");
  92. }
  93. - (void)testIndex {
  94. RLMObjectSchema *schema = [RLMObjectSchema schemaForObjectClass:[IndexedObject class]];
  95. XCTAssertTrue(schema[@"stringCol"].indexed);
  96. XCTAssertTrue(schema[@"integerCol"].indexed);
  97. XCTAssertTrue(schema[@"intCol"].indexed);
  98. XCTAssertTrue(schema[@"longCol"].indexed);
  99. XCTAssertTrue(schema[@"longlongCol"].indexed);
  100. XCTAssertTrue(schema[@"boolCol"].indexed);
  101. XCTAssertTrue(schema[@"dateCol"].indexed);
  102. XCTAssertTrue(schema[@"optionalIntCol"].indexed);
  103. XCTAssertTrue(schema[@"optionalBoolCol"].indexed);
  104. XCTAssertFalse(schema[@"floatCol"].indexed);
  105. XCTAssertFalse(schema[@"doubleCol"].indexed);
  106. XCTAssertFalse(schema[@"dataCol"].indexed);
  107. XCTAssertFalse(schema[@"optionalFloatCol"].indexed);
  108. XCTAssertFalse(schema[@"optionalDoubleCol"].indexed);
  109. }
  110. @end