SchemaTests.mm 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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/XCTest.h>
  19. #import "RLMMultiProcessTestCase.h"
  20. #import "RLMAccessor.h"
  21. #import "RLMObjectSchema_Private.hpp"
  22. #import "RLMObject_Private.h"
  23. #import "RLMProperty_Private.h"
  24. #import "RLMRealmConfiguration_Private.hpp"
  25. #import "RLMRealm_Dynamic.h"
  26. #import "RLMRealm_Private.hpp"
  27. #import "RLMSchema_Private.hpp"
  28. #import "RLMUtil.hpp"
  29. #import "schema.hpp"
  30. #import <realm/table.hpp>
  31. #import <algorithm>
  32. #import <objc/runtime.h>
  33. @interface SchemaTestClassBase : RLMObject
  34. @property IntObject *baseCol;
  35. @end
  36. @implementation SchemaTestClassBase
  37. @end
  38. @interface SchemaTestClassFirstChild : SchemaTestClassBase
  39. @property IntObject *firstChildCol;
  40. @end
  41. @implementation SchemaTestClassFirstChild
  42. @end
  43. @interface SchemaTestClassSecondChild : SchemaTestClassBase
  44. @property IntObject *secondChildCol;
  45. @end
  46. @implementation SchemaTestClassSecondChild
  47. @end
  48. RLM_ARRAY_TYPE(SchemaTestClassBase)
  49. RLM_ARRAY_TYPE(SchemaTestClassFirstChild)
  50. RLM_ARRAY_TYPE(SchemaTestClassSecondChild)
  51. @interface SchemaTestClassLink : RLMObject
  52. @property SchemaTestClassBase *base;
  53. @property SchemaTestClassFirstChild *child;
  54. @property SchemaTestClassSecondChild *secondChild;
  55. @property RLM_GENERIC_ARRAY(SchemaTestClassBase) *baseArray;
  56. @property RLM_GENERIC_ARRAY(SchemaTestClassFirstChild) *childArray;
  57. @property RLM_GENERIC_ARRAY(SchemaTestClassSecondChild) *secondChildArray;
  58. @end
  59. @implementation SchemaTestClassLink
  60. @end
  61. @interface NonDefaultObject : RLMObject
  62. @property int intCol;
  63. @end
  64. @implementation NonDefaultObject
  65. + (BOOL)shouldIncludeInDefaultSchema {
  66. return NO;
  67. }
  68. @end
  69. RLM_ARRAY_TYPE(NonDefaultObject);
  70. @interface NonDefaultArrayObject : RLMObject
  71. @property RLM_GENERIC_ARRAY(NonDefaultObject) *array;
  72. @end
  73. @implementation NonDefaultArrayObject
  74. + (BOOL)shouldIncludeInDefaultSchema {
  75. return NO;
  76. }
  77. @end
  78. @class MutualLink2Object;
  79. @interface MutualLink1Object : RLMObject
  80. @property (nullable) MutualLink2Object *object;
  81. @end
  82. @implementation MutualLink1Object
  83. + (BOOL)shouldIncludeInDefaultSchema {
  84. return NO;
  85. }
  86. @end
  87. @interface MutualLink2Object : RLMObject
  88. @property (nullable) MutualLink1Object *object;
  89. @end
  90. @implementation MutualLink2Object
  91. + (BOOL)shouldIncludeInDefaultSchema {
  92. return NO;
  93. }
  94. @end
  95. @interface SchemaTestClassWithSingleDuplicatePropertyBase : FakeObject
  96. @property NSString *string;
  97. @end
  98. @implementation SchemaTestClassWithSingleDuplicatePropertyBase
  99. @end
  100. @interface SchemaTestClassWithSingleDuplicateProperty : SchemaTestClassWithSingleDuplicatePropertyBase
  101. @property NSString *string;
  102. @end
  103. @implementation SchemaTestClassWithSingleDuplicateProperty
  104. @dynamic string;
  105. @end
  106. @interface SchemaTestClassWithMultipleDuplicatePropertiesBase : FakeObject
  107. @property NSString *string;
  108. @property int integer;
  109. @end
  110. @implementation SchemaTestClassWithMultipleDuplicatePropertiesBase
  111. @end
  112. @interface SchemaTestClassWithMultipleDuplicateProperties : SchemaTestClassWithMultipleDuplicatePropertiesBase
  113. @property NSString *string;
  114. @property int integer;
  115. @end
  116. @implementation SchemaTestClassWithMultipleDuplicateProperties
  117. @dynamic string;
  118. @dynamic integer;
  119. @end
  120. @interface UnindexableProperty : FakeObject
  121. @property double unindexable;
  122. @end
  123. @implementation UnindexableProperty
  124. + (NSArray *)indexedProperties {
  125. return @[@"unindexable"];
  126. }
  127. @end
  128. @interface InvalidPrimaryKeyType : FakeObject
  129. @property double primaryKey;
  130. @end
  131. @implementation InvalidPrimaryKeyType
  132. + (NSString *)primaryKey {
  133. return @"primaryKey";
  134. }
  135. @end
  136. @interface RequiredLinkProperty : FakeObject
  137. @property BoolObject *object;
  138. @end
  139. @implementation RequiredLinkProperty
  140. + (NSArray *)requiredProperties {
  141. return @[@"object"];
  142. }
  143. @end
  144. @interface InvalidNSNumberProtocolObject : FakeObject
  145. @property NSNumber<NSFastEnumeration> *number;
  146. @end
  147. @implementation InvalidNSNumberProtocolObject
  148. @end
  149. @interface InvalidNSNumberNoProtocolObject : FakeObject
  150. @property NSNumber *number;
  151. @end
  152. @implementation InvalidNSNumberNoProtocolObject
  153. @end
  154. @class InvalidReadWriteLinkingObjectsProperty;
  155. @class InvalidLinkingObjectsPropertiesMethod;
  156. @class ValidLinkingObjectsPropertyWithProtocol;
  157. @class InvalidLinkingObjectsPropertyProtocol;
  158. @interface SchemaTestsLinkSource : FakeObject
  159. @property InvalidReadWriteLinkingObjectsProperty *irwlop;
  160. @property InvalidLinkingObjectsPropertiesMethod *iloprm;
  161. @property ValidLinkingObjectsPropertyWithProtocol *vlopwp;
  162. @property InvalidLinkingObjectsPropertyProtocol *ilopp;
  163. @end
  164. @implementation SchemaTestsLinkSource
  165. @end
  166. @interface MixedProperty : FakeObject
  167. @property id mixed;
  168. @end
  169. @implementation MixedProperty
  170. @end
  171. RLM_ARRAY_TYPE(SchemaTestsLinkSource)
  172. @interface InvalidReadWriteLinkingObjectsProperty : FakeObject
  173. @property RLMLinkingObjects *linkingObjects;
  174. @end
  175. @implementation InvalidReadWriteLinkingObjectsProperty
  176. + (NSDictionary *)linkingObjectsProperties {
  177. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:SchemaTestsLinkSource.class propertyName:@"irwlop"] };
  178. }
  179. @end
  180. @interface InvalidLinkingObjectsPropertiesMethod : FakeObject
  181. @property (readonly) RLMLinkingObjects *linkingObjects;
  182. @end
  183. @implementation InvalidLinkingObjectsPropertiesMethod
  184. @end
  185. @interface ValidLinkingObjectsPropertyWithProtocol : FakeObject
  186. @property (readonly) RLMLinkingObjects<SchemaTestsLinkSource> *linkingObjects;
  187. @end
  188. @implementation ValidLinkingObjectsPropertyWithProtocol
  189. + (NSDictionary *)linkingObjectsProperties {
  190. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:SchemaTestsLinkSource.class propertyName:@"vlopwp"] };
  191. }
  192. @end
  193. RLM_ARRAY_TYPE(NotARealClass)
  194. @interface InvalidLinkingObjectsPropertyProtocol : FakeObject
  195. @property (readonly) RLMLinkingObjects<NotARealClass> *linkingObjects;
  196. @end
  197. @implementation InvalidLinkingObjectsPropertyProtocol
  198. + (NSDictionary *)linkingObjectsProperties {
  199. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:SchemaTestsLinkSource.class propertyName:@"ilopp"] };
  200. }
  201. @end
  202. @interface InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink : FakeObject
  203. @property (readonly) RLMLinkingObjects *linkingObjects;
  204. @property InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink *link;
  205. @end
  206. @implementation InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink
  207. + (NSDictionary *)linkingObjectsProperties {
  208. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink.class
  209. propertyName:@"nosuchproperty"] };
  210. }
  211. @end
  212. @interface InvalidLinkingObjectsPropertySourcePropertyNotALink : FakeObject
  213. @property (readonly) RLMLinkingObjects *linkingObjects;
  214. @property int64_t integer;
  215. @end
  216. @implementation InvalidLinkingObjectsPropertySourcePropertyNotALink
  217. + (NSDictionary *)linkingObjectsProperties {
  218. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:InvalidLinkingObjectsPropertySourcePropertyNotALink.class
  219. propertyName:@"integer"] };
  220. }
  221. @end
  222. @interface InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere : FakeObject
  223. @property (readonly) RLMLinkingObjects *linkingObjects;
  224. @property IntObject *link;
  225. @end
  226. @implementation InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere
  227. + (NSDictionary *)linkingObjectsProperties {
  228. return @{ @"linkingObjects": [RLMPropertyDescriptor descriptorWithClass:InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere.class
  229. propertyName:@"link"] };
  230. }
  231. @end
  232. @interface SchemaTests : RLMMultiProcessTestCase
  233. @end
  234. @implementation SchemaTests
  235. - (void)tearDown {
  236. RLMSetTreatFakeObjectAsRLMObject(NO);
  237. [super tearDown];
  238. }
  239. - (void)testNoSchemaForUnmanagedObjectClasses {
  240. RLMSchema *schema = [RLMSchema sharedSchema];
  241. XCTAssertNil([schema schemaForClassName:@"RLMObject"]);
  242. XCTAssertNil([schema schemaForClassName:@"RLMObjectBase"]);
  243. XCTAssertNil([schema schemaForClassName:@"RLMDynamicObject"]);
  244. }
  245. - (void)testSchemaWithObjectClasses {
  246. RLMSchema *schema = [RLMSchema schemaWithObjectClasses:@[RLMDynamicObject.class, StringObject.class]];
  247. XCTAssertEqualObjects((@[@"RLMDynamicObject", @"StringObject"]),
  248. [[schema.objectSchema valueForKey:@"className"] sortedArrayUsingSelector:@selector(compare:)]);
  249. XCTAssertNil([RLMSchema.sharedSchema schemaForClassName:@"RLMDynamicObject"]);
  250. }
  251. - (void)testInheritanceInitialization
  252. {
  253. Class testClasses[] = {
  254. [SchemaTestClassBase class],
  255. [SchemaTestClassFirstChild class],
  256. [SchemaTestClassSecondChild class],
  257. [SchemaTestClassLink class],
  258. [IntObject class]
  259. };
  260. auto pred = ^(Class lft, Class rgt) {
  261. return (uintptr_t)lft < (uintptr_t)rgt;
  262. };
  263. auto checkSchema = ^(RLMSchema *schema, NSString *className, NSDictionary *properties) {
  264. RLMObjectSchema *objectSchema = schema[className];
  265. XCTAssertEqualObjects(className, objectSchema.className);
  266. XCTAssertEqualObjects(className, [objectSchema.unmanagedClass className]);
  267. XCTAssertEqualObjects(className, [objectSchema.accessorClass className]);
  268. XCTAssertEqual(objectSchema.properties.count, properties.count);
  269. for (NSString *propName in properties) {
  270. XCTAssertEqualObjects(properties[propName], [objectSchema[propName] objectClassName]);
  271. }
  272. };
  273. // Test each permutation of loading orders and verify that all properties
  274. // are initialized correctly
  275. std::sort(testClasses, std::end(testClasses), pred);
  276. unsigned long iteration = 0;
  277. do @autoreleasepool {
  278. // Clean up any existing overridden things
  279. for (Class cls : testClasses) {
  280. // Ensure that the className method isn't used during schema init
  281. // as it may not be overriden yet
  282. Class metaClass = object_getClass(cls);
  283. IMP imp = imp_implementationWithBlock(^{ return nil; });
  284. class_replaceMethod(metaClass, @selector(className), imp, "@:");
  285. }
  286. NSMutableArray *objectSchemas = [NSMutableArray arrayWithCapacity:4U];
  287. for (Class cls : testClasses) {
  288. [objectSchemas addObject:[RLMObjectSchema schemaForObjectClass:cls]];
  289. }
  290. RLMSchema *schema = [[RLMSchema alloc] init];
  291. schema.objectSchema = objectSchemas;
  292. for (RLMObjectSchema *objectSchema in objectSchemas) {
  293. NSString *name = [NSString stringWithFormat:@"RLMTestAccessor_%lu_%@", iteration, objectSchema.className];
  294. objectSchema.accessorClass = RLMManagedAccessorClassForObjectClass(objectSchema.objectClass, objectSchema, name.UTF8String);
  295. objectSchema.unmanagedClass = RLMUnmanagedAccessorClassForObjectClass(objectSchema.objectClass, objectSchema);
  296. }
  297. ++iteration;
  298. for (Class cls : testClasses) {
  299. NSString *className = NSStringFromClass(cls);
  300. // Restore the className method
  301. Class metaClass = object_getClass(cls);
  302. IMP imp = imp_implementationWithBlock(^{ return className; });
  303. class_replaceMethod(metaClass, @selector(className), imp, "@:");
  304. }
  305. // Verify that each class has the correct properties and className
  306. // for generated subclasses
  307. checkSchema(schema, @"SchemaTestClassBase", @{@"baseCol": @"IntObject"});
  308. checkSchema(schema, @"SchemaTestClassFirstChild", @{@"baseCol": @"IntObject",
  309. @"firstChildCol": @"IntObject"});
  310. checkSchema(schema, @"SchemaTestClassSecondChild", @{@"baseCol": @"IntObject",
  311. @"secondChildCol": @"IntObject"});
  312. checkSchema(schema, @"SchemaTestClassLink", @{@"base": @"SchemaTestClassBase",
  313. @"baseArray": @"SchemaTestClassBase",
  314. @"child": @"SchemaTestClassFirstChild",
  315. @"childArray": @"SchemaTestClassFirstChild",
  316. @"secondChild": @"SchemaTestClassSecondChild",
  317. @"secondChildArray": @"SchemaTestClassSecondChild"});
  318. // Test creating objects of each class
  319. [self deleteFiles];
  320. RLMRealm *realm = [self realmWithTestPathAndSchema:schema];
  321. [realm beginWriteTransaction];
  322. [realm createObject:@"SchemaTestClassBase" withValue:@{@"baseCol": @[@0]}];
  323. [realm createObject:@"SchemaTestClassFirstChild" withValue:@{@"baseCol": @[@0], @"firstChildCol": @[@0]}];
  324. [realm createObject:@"SchemaTestClassSecondChild" withValue:@{@"baseCol": @[@0], @"secondChildCol": @[@0]}];
  325. [realm commitWriteTransaction];
  326. } while (std::next_permutation(testClasses, std::end(testClasses), pred));
  327. }
  328. - (void)testObjectSchema
  329. {
  330. // Setting up some test data
  331. // Due to the automatic initialization of a new realm with all visible classes inheriting from
  332. // RLMObject, it's difficult to define test cases that verify the absolute correctness of a
  333. // realm's current type catalogue unless its expected configuration is known at compile time
  334. // (requires that the set of expected types is always up-to-date). Instead, only a partial
  335. // verification is performed, which only requires the availability of a well-defined subset of
  336. // types and ignores any other types that may be included in the realm's type catalogue.
  337. // If a more fine-grained control with the realm's type inclusion mechanism is introduced later
  338. // on, these tests should be altered to verify all types.
  339. NSArray *expectedTypes = @[@"AllTypesObject",
  340. @"LinkToAllTypesObject",
  341. @"StringObject",
  342. @"IntObject"];
  343. NSString *unexpectedType = @"__$ThisTypeShouldNotOccur$__";
  344. // Getting the test realm
  345. NSMutableArray *objectSchema = [NSMutableArray array];
  346. for (NSString *className in expectedTypes) {
  347. [objectSchema addObject:[RLMObjectSchema schemaForObjectClass:NSClassFromString(className)]];
  348. }
  349. RLMSchema *schema = [[RLMSchema alloc] init];
  350. schema.objectSchema = objectSchema;
  351. // create realm with schema
  352. @autoreleasepool { [self realmWithTestPathAndSchema:schema]; }
  353. // get dynamic realm and extract schema
  354. RLMRealm *realm = [self realmWithTestPathAndSchema:nil];
  355. schema = realm.schema;
  356. // Test 1: Does the objectSchema return the right number of object schemas?
  357. NSArray *objectSchemas = schema.objectSchema;
  358. XCTAssertTrue(objectSchemas.count >= expectedTypes.count, @"Expecting %lu object schemas in database found %lu", (unsigned long)expectedTypes.count, (unsigned long)objectSchemas.count);
  359. // Test 2: Does the object schema array contain the expected schemas?
  360. NSUInteger identifiedTypesCount = 0;
  361. for (NSString *expectedType in expectedTypes) {
  362. NSUInteger occurrenceCount = 0;
  363. for (RLMObjectSchema *objectSchema in objectSchemas) {
  364. if ([objectSchema.className isEqualToString:expectedType]) {
  365. occurrenceCount++;
  366. }
  367. }
  368. XCTAssertEqual(occurrenceCount, (NSUInteger)1, @"Expecting single occurrence of object schema for type %@ found %lu", expectedType, (unsigned long)occurrenceCount);
  369. if (occurrenceCount > 0) {
  370. identifiedTypesCount++;
  371. }
  372. }
  373. // Test 3: Does the object schema array contains at least the expected classes
  374. XCTAssertTrue(identifiedTypesCount >= expectedTypes.count, @"Unexpected object schemas in database. Found %lu out of %lu expected", (unsigned long)identifiedTypesCount, (unsigned long)expectedTypes.count);
  375. // Test 4: Test querying object schemas using schemaForClassName: for expected types
  376. for (NSString *expectedType in expectedTypes) {
  377. XCTAssertNotNil([schema schemaForClassName:expectedType], @"Expecting to find object schema for type %@ in realm using query, found none", expectedType);
  378. }
  379. // Test 5: Test querying object schemas using schemaForClassName: for unexpected types
  380. XCTAssertNil([schema schemaForClassName:unexpectedType], @"Expecting not to find object schema for type %@ in realm using query, did find", unexpectedType);
  381. // Test 6: Test querying object schemas using subscription for unexpected types
  382. for (NSString *expectedType in expectedTypes) {
  383. XCTAssertNotNil(schema[expectedType], @"Expecting to find object schema for type %@ in realm using subscription, found none", expectedType);
  384. }
  385. // Test 7: Test querying object schemas using subscription for unexpected types
  386. XCTAssertThrows(schema[unexpectedType], @"Expecting asking schema for type %@ in realm using subscription to throw", unexpectedType);
  387. // Test 8: RLMObject should not appear in the shared object schema
  388. XCTAssertThrows(RLMSchema.sharedSchema[@"RLMObject"]);
  389. }
  390. - (void)testDescription {
  391. NSArray *expectedTypes = @[@"AllTypesObject",
  392. @"StringObject",
  393. @"IntObject"];
  394. NSMutableArray *objectSchema = [NSMutableArray array];
  395. for (NSString *className in expectedTypes) {
  396. [objectSchema addObject:[RLMObjectSchema schemaForObjectClass:NSClassFromString(className)]];
  397. }
  398. RLMSchema *schema = [[RLMSchema alloc] init];
  399. schema.objectSchema = objectSchema;
  400. XCTAssertEqualObjects(schema.description, @"Schema {\n"
  401. @"\tAllTypesObject {\n"
  402. @"\t\tboolCol {\n"
  403. @"\t\t\ttype = bool;\n"
  404. @"\t\t\tobjectClassName = (null);\n"
  405. @"\t\t\tlinkOriginPropertyName = (null);\n"
  406. @"\t\t\tindexed = NO;\n"
  407. @"\t\t\tisPrimary = NO;\n"
  408. @"\t\t\tarray = NO;\n"
  409. @"\t\t\toptional = NO;\n"
  410. @"\t\t}\n"
  411. @"\t\tintCol {\n"
  412. @"\t\t\ttype = int;\n"
  413. @"\t\t\tobjectClassName = (null);\n"
  414. @"\t\t\tlinkOriginPropertyName = (null);\n"
  415. @"\t\t\tindexed = NO;\n"
  416. @"\t\t\tisPrimary = NO;\n"
  417. @"\t\t\tarray = NO;\n"
  418. @"\t\t\toptional = NO;\n"
  419. @"\t\t}\n"
  420. @"\t\tfloatCol {\n"
  421. @"\t\t\ttype = float;\n"
  422. @"\t\t\tobjectClassName = (null);\n"
  423. @"\t\t\tlinkOriginPropertyName = (null);\n"
  424. @"\t\t\tindexed = NO;\n"
  425. @"\t\t\tisPrimary = NO;\n"
  426. @"\t\t\tarray = NO;\n"
  427. @"\t\t\toptional = NO;\n"
  428. @"\t\t}\n"
  429. @"\t\tdoubleCol {\n"
  430. @"\t\t\ttype = double;\n"
  431. @"\t\t\tobjectClassName = (null);\n"
  432. @"\t\t\tlinkOriginPropertyName = (null);\n"
  433. @"\t\t\tindexed = NO;\n"
  434. @"\t\t\tisPrimary = NO;\n"
  435. @"\t\t\tarray = NO;\n"
  436. @"\t\t\toptional = NO;\n"
  437. @"\t\t}\n"
  438. @"\t\tstringCol {\n"
  439. @"\t\t\ttype = string;\n"
  440. @"\t\t\tobjectClassName = (null);\n"
  441. @"\t\t\tlinkOriginPropertyName = (null);\n"
  442. @"\t\t\tindexed = NO;\n"
  443. @"\t\t\tisPrimary = NO;\n"
  444. @"\t\t\tarray = NO;\n"
  445. @"\t\t\toptional = NO;\n"
  446. @"\t\t}\n"
  447. @"\t\tbinaryCol {\n"
  448. @"\t\t\ttype = data;\n"
  449. @"\t\t\tobjectClassName = (null);\n"
  450. @"\t\t\tlinkOriginPropertyName = (null);\n"
  451. @"\t\t\tindexed = NO;\n"
  452. @"\t\t\tisPrimary = NO;\n"
  453. @"\t\t\tarray = NO;\n"
  454. @"\t\t\toptional = NO;\n"
  455. @"\t\t}\n"
  456. @"\t\tdateCol {\n"
  457. @"\t\t\ttype = date;\n"
  458. @"\t\t\tobjectClassName = (null);\n"
  459. @"\t\t\tlinkOriginPropertyName = (null);\n"
  460. @"\t\t\tindexed = NO;\n"
  461. @"\t\t\tisPrimary = NO;\n"
  462. @"\t\t\tarray = NO;\n"
  463. @"\t\t\toptional = NO;\n"
  464. @"\t\t}\n"
  465. @"\t\tcBoolCol {\n"
  466. @"\t\t\ttype = bool;\n"
  467. @"\t\t\tobjectClassName = (null);\n"
  468. @"\t\t\tlinkOriginPropertyName = (null);\n"
  469. @"\t\t\tindexed = NO;\n"
  470. @"\t\t\tisPrimary = NO;\n"
  471. @"\t\t\tarray = NO;\n"
  472. @"\t\t\toptional = NO;\n"
  473. @"\t\t}\n"
  474. @"\t\tlongCol {\n"
  475. @"\t\t\ttype = int;\n"
  476. @"\t\t\tobjectClassName = (null);\n"
  477. @"\t\t\tlinkOriginPropertyName = (null);\n"
  478. @"\t\t\tindexed = NO;\n"
  479. @"\t\t\tisPrimary = NO;\n"
  480. @"\t\t\tarray = NO;\n"
  481. @"\t\t\toptional = NO;\n"
  482. @"\t\t}\n"
  483. @"\t\tobjectCol {\n"
  484. @"\t\t\ttype = object;\n"
  485. @"\t\t\tobjectClassName = StringObject;\n"
  486. @"\t\t\tlinkOriginPropertyName = (null);\n"
  487. @"\t\t\tindexed = NO;\n"
  488. @"\t\t\tisPrimary = NO;\n"
  489. @"\t\t\tarray = NO;\n"
  490. @"\t\t\toptional = YES;\n"
  491. @"\t\t}\n"
  492. @"\t\tlinkingObjectsCol {\n"
  493. @"\t\t\ttype = linking objects;\n"
  494. @"\t\t\tobjectClassName = LinkToAllTypesObject;\n"
  495. @"\t\t\tlinkOriginPropertyName = allTypesCol;\n"
  496. @"\t\t\tindexed = NO;\n"
  497. @"\t\t\tisPrimary = NO;\n"
  498. @"\t\t\tarray = YES;\n"
  499. @"\t\t\toptional = NO;\n"
  500. @"\t\t}\n"
  501. @"\t}\n"
  502. @"\tIntObject {\n"
  503. @"\t\tintCol {\n"
  504. @"\t\t\ttype = int;\n"
  505. @"\t\t\tobjectClassName = (null);\n"
  506. @"\t\t\tlinkOriginPropertyName = (null);\n"
  507. @"\t\t\tindexed = NO;\n"
  508. @"\t\t\tisPrimary = NO;\n"
  509. @"\t\t\tarray = NO;\n"
  510. @"\t\t\toptional = NO;\n"
  511. @"\t\t}\n"
  512. @"\t}\n"
  513. @"\tStringObject {\n"
  514. @"\t\tstringCol {\n"
  515. @"\t\t\ttype = string;\n"
  516. @"\t\t\tobjectClassName = (null);\n"
  517. @"\t\t\tlinkOriginPropertyName = (null);\n"
  518. @"\t\t\tindexed = NO;\n"
  519. @"\t\t\tisPrimary = NO;\n"
  520. @"\t\t\tarray = NO;\n"
  521. @"\t\t\toptional = YES;\n"
  522. @"\t\t}\n"
  523. @"\t}\n"
  524. @"}");
  525. }
  526. - (void)testClassWithDuplicateProperties
  527. {
  528. // If a property is overriden in a child class it should not be picked up more than once.
  529. RLMObjectSchema *firstSchema = [RLMObjectSchema schemaForObjectClass:SchemaTestClassWithSingleDuplicateProperty.class];
  530. XCTAssertEqual((int)firstSchema.properties.count, 1);
  531. RLMObjectSchema *secondSchema = [RLMObjectSchema schemaForObjectClass:SchemaTestClassWithMultipleDuplicateProperties.class];
  532. XCTAssertEqual((int)secondSchema.properties.count, 2);
  533. }
  534. - (void)testClassWithInvalidPrimaryKey {
  535. XCTAssertThrows([RLMObjectSchema schemaForObjectClass:InvalidPrimaryKeyType.class]);
  536. }
  537. - (void)testClassWithUnindexableProperty {
  538. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:UnindexableProperty.class];
  539. RLMSchema *schema = [[RLMSchema alloc] init];
  540. schema.objectSchema = @[objectSchema];
  541. RLMAssertThrowsWithReasonMatching([self realmWithTestPathAndSchema:schema],
  542. @"Property 'UnindexableProperty.unindexable' of type 'double' cannot be indexed");
  543. }
  544. - (void)testClassWithRequiredNullableProperties {
  545. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:RequiredPropertiesObject.class];
  546. XCTAssertFalse([objectSchema[@"stringCol"] optional]);
  547. XCTAssertFalse([objectSchema[@"binaryCol"] optional]);
  548. }
  549. - (void)testClassWithRequiredPrimitiveArrayProperties {
  550. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:AllPrimitiveArrays.class];
  551. XCTAssertFalse(objectSchema[@"intObj"].optional);
  552. XCTAssertFalse(objectSchema[@"boolObj"].optional);
  553. XCTAssertFalse(objectSchema[@"floatObj"].optional);
  554. XCTAssertFalse(objectSchema[@"doubleObj"].optional);
  555. XCTAssertFalse(objectSchema[@"stringObj"].optional);
  556. XCTAssertFalse(objectSchema[@"dateObj"].optional);
  557. XCTAssertFalse(objectSchema[@"dataObj"].optional);
  558. }
  559. - (void)testClassWithOptionalPrimitiveArrayProperties {
  560. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:AllOptionalPrimitiveArrays.class];
  561. XCTAssertTrue(objectSchema[@"intObj"].optional);
  562. XCTAssertTrue(objectSchema[@"boolObj"].optional);
  563. XCTAssertTrue(objectSchema[@"floatObj"].optional);
  564. XCTAssertTrue(objectSchema[@"doubleObj"].optional);
  565. XCTAssertTrue(objectSchema[@"stringObj"].optional);
  566. XCTAssertTrue(objectSchema[@"dateObj"].optional);
  567. XCTAssertTrue(objectSchema[@"dataObj"].optional);
  568. }
  569. - (void)testClassWithRequiredLinkProperty {
  570. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:RequiredLinkProperty.class], @"cannot be made required.*'object'");
  571. }
  572. - (void)testClassWithInvalidNSNumberProtocolProperty {
  573. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:InvalidNSNumberProtocolObject.class],
  574. @"Property 'number' is of type \"NSNumber<NSFastEnumeration>\" which is not a supported NSNumber object type.");
  575. }
  576. - (void)testClassWithInvalidNSNumberNoProtocolProperty {
  577. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:InvalidNSNumberNoProtocolObject.class], @"Property 'number' requires a protocol defining the contained type");
  578. }
  579. - (void)testClassWithReadWriteLinkingObjectsProperty {
  580. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:InvalidReadWriteLinkingObjectsProperty.class],
  581. @"Property 'linkingObjects' must be declared as readonly .* linking objects");
  582. }
  583. - (void)testClassWithInvalidLinkingObjectsPropertiesMethod {
  584. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:InvalidLinkingObjectsPropertiesMethod.class],
  585. @"Property 'linkingObjects' .* but \\+linkingObjectsProperties .* class or property");
  586. }
  587. - (void)testClassWithLinkingObjectsPropertyWithProtocol {
  588. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:ValidLinkingObjectsPropertyWithProtocol.class];
  589. XCTAssertEqual(objectSchema[@"linkingObjects"].type, RLMPropertyTypeLinkingObjects);
  590. }
  591. - (void)testClassWithInvalidLinkingObjectsPropertyProtocol {
  592. RLMAssertThrowsWithReasonMatching([RLMObjectSchema schemaForObjectClass:InvalidLinkingObjectsPropertyProtocol.class],
  593. @"Property 'linkingObjects' .* type RLMLinkingObjects<NotARealClass>.*conflicting class name.*'SchemaTestsLinkSource'");
  594. }
  595. - (void)testClassWithInvalidLinkingObjectsPropertyMissingSourcePropertyOfLink {
  596. RLMSetTreatFakeObjectAsRLMObject(YES);
  597. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  598. config.customSchema = [RLMSchema schemaWithObjectClasses:@[ InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink.class ]];
  599. RLMAssertThrowsWithReasonMatching([RLMRealm realmWithConfiguration:config error:nil],
  600. @"Property 'InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink.nosuchproperty' declared as origin of linking objects property 'InvalidLinkingObjectsPropertyMissingSourcePropertyOfLink.linkingObjects' does not exist");
  601. }
  602. - (void)testClassWithInvalidLinkingObjectsPropertySourcePropertyNotALink {
  603. RLMSetTreatFakeObjectAsRLMObject(YES);
  604. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  605. config.customSchema = [RLMSchema schemaWithObjectClasses:@[ InvalidLinkingObjectsPropertySourcePropertyNotALink.class ]];
  606. RLMAssertThrowsWithReasonMatching([RLMRealm realmWithConfiguration:config error:nil],
  607. @"Property 'InvalidLinkingObjectsPropertySourcePropertyNotALink.integer' declared as origin of linking objects property 'InvalidLinkingObjectsPropertySourcePropertyNotALink.linkingObjects' is not a link");
  608. }
  609. - (void)testClassWithInvalidLinkingObjectsPropertySourcePropertysLinkElsewhere {
  610. RLMSetTreatFakeObjectAsRLMObject(YES);
  611. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  612. config.customSchema = [RLMSchema schemaWithObjectClasses:@[ InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere.class, IntObject.class ]];
  613. RLMAssertThrowsWithReasonMatching([RLMRealm realmWithConfiguration:config error:nil],
  614. @"Property 'InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere.link' declared as origin of linking objects property 'InvalidLinkingObjectsPropertySourcePropertyLinksElsewhere.linkingObjects' links to type 'IntObject'");
  615. }
  616. - (void)testMixedIsRejected {
  617. RLMSetTreatFakeObjectAsRLMObject(YES);
  618. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  619. RLMAssertThrowsWithReasonMatching(config.objectClasses = @[[MixedProperty class]],
  620. @"Property 'mixed' is declared as 'id'.*");
  621. }
  622. // Can't spawn child processes on iOS
  623. #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR
  624. - (void)testPartialSharedSchemaInit {
  625. if (self.isParent) {
  626. RLMRunChildAndWait();
  627. return;
  628. }
  629. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  630. // Verify that opening with class subsets without the shared schema being
  631. // initialized works
  632. config.objectClasses = @[IntObject.class];
  633. @autoreleasepool {
  634. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  635. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  636. XCTAssertNoThrow(realm.schema[@"IntObject"]);
  637. }
  638. config.objectClasses = @[IntObject.class, StringObject.class];
  639. @autoreleasepool {
  640. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  641. XCTAssertEqual(2U, realm.schema.objectSchema.count);
  642. XCTAssertNoThrow(realm.schema[@"IntObject"]);
  643. XCTAssertNoThrow(realm.schema[@"StringObject"]);
  644. }
  645. // Verify that the shared schema generated afterwards is valid
  646. config.objectClasses = nil;
  647. @autoreleasepool {
  648. RLMRealm *realm = [RLMRealm defaultRealm];
  649. // Shared schema shouldn't have accessor classes
  650. for (RLMObjectSchema *objectSchema in realm.schema.objectSchema) {
  651. const char *actualClassName = class_getName(objectSchema.objectClass);
  652. XCTAssertEqual(nullptr, strstr(actualClassName, "RLMAccessor"));
  653. XCTAssertEqual(nullptr, strstr(actualClassName, "RLMUnmanaged"));
  654. }
  655. // Shared schema shouldn't have duplicate entries
  656. XCTAssertEqual(realm.schema.objectSchema.count,
  657. [NSSet setWithArray:[realm.schema.objectSchema valueForKey:@"className"]].count);
  658. // Shared schema should have the ones that were used in the subsets
  659. XCTAssertNoThrow(realm.schema[@"IntObject"]);
  660. XCTAssertNoThrow(realm.schema[@"StringObject"]);
  661. }
  662. }
  663. - (void)testPartialSharedSchemaInitInheritance {
  664. if (self.isParent) {
  665. RLMRunChildAndWait();
  666. return;
  667. }
  668. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  669. config.objectClasses = @[NumberObject.class];
  670. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  671. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  672. XCTAssertEqualObjects(@"NumberObject", [[[[NumberObject alloc] init] objectSchema] className]);
  673. // Verify that child class doesn't use the parent class's schema
  674. XCTAssertEqualObjects(@"NumberDefaultsObject", [[[[NumberDefaultsObject alloc] init] objectSchema] className]);
  675. }
  676. - (void)testCreateUnmanagedObjectWithUninitializedSchema {
  677. if (self.isParent) {
  678. RLMRunChildAndWait();
  679. return;
  680. }
  681. XCTAssertTrue(RLMSchema.partialSharedSchema.objectSchema.count == 0);
  682. XCTAssertNoThrow([[IntObject alloc] initWithValue:@[@0]]);
  683. XCTAssertNoThrow([[NonDefaultObject alloc] initWithValue:@[@0]]);
  684. }
  685. - (void)testCreateUnmanagedObjectWithNestedObjectWithUninitializedSchema {
  686. if (self.isParent) {
  687. RLMRunChildAndWait();
  688. return;
  689. }
  690. XCTAssertTrue(RLMSchema.partialSharedSchema.objectSchema.count == 0);
  691. XCTAssertNoThrow([[IntegerArrayPropertyObject alloc] initWithValue:(@[@0, @[@[@0]]])]);
  692. XCTAssertNoThrow([[NonDefaultArrayObject alloc] initWithValue:@[@[@[@0]]]]);
  693. XCTAssertNoThrow([[MutualLink1Object alloc] initWithValue:@[@[@{}]]]);
  694. }
  695. - (void)testKVOSubclassesDoNotAppearInSchema {
  696. if (self.isParent) {
  697. RLMRunChildAndWait();
  698. return;
  699. }
  700. auto obj = [IntObject new];
  701. [obj addObserver:self forKeyPath:@"intCol" options:0 context:0];
  702. for (RLMObjectSchema *objectSchema in RLMRealm.defaultRealm.schema.objectSchema) {
  703. XCTAssertEqual([objectSchema.className rangeOfString:@"RLM:"].location, NSNotFound);
  704. }
  705. [obj removeObserver:self forKeyPath:@"intCol" context:0];
  706. }
  707. #if !DEBUG
  708. - (void)testMultipleProcessesTryingToInitializeSchema {
  709. RLMRealm *syncRealm = [self realmWithTestPath];
  710. if (!self.isParent) {
  711. RLMSchema *schema = [RLMSchema schemaWithObjectClasses:@[IntObject.class]];
  712. RLMProperty *prop = ((NSArray *)[schema.objectSchema[0] properties])[0];
  713. prop.type = RLMPropertyTypeFloat;
  714. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  715. config.customSchema = schema;
  716. config.schemaVersion = 1;
  717. [syncRealm transactionWithBlock:^{
  718. [StringObject createInRealm:syncRealm withValue:@[@""]];
  719. }];
  720. [RLMRealm realmWithConfiguration:config error:nil];
  721. return;
  722. }
  723. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  724. config.objectClasses = @[IntObject.class];
  725. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  726. // Hold a write transaction to prevent the child processes from performing
  727. // the migration immediately
  728. [realm beginWriteTransaction];
  729. // Spawn a bunch of child processes which will all try to perform the migration
  730. dispatch_group_t group = dispatch_group_create();
  731. for (int i = 0; i < 5; ++i) {
  732. dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
  733. RLMRunChildAndWait();
  734. });
  735. }
  736. // Wait for all five to be immediately before the point where they will try
  737. // to perform the migration. There's inherently a race condition here in
  738. // as in theory all but one process could be suspended immediately after
  739. // committing the signalling commit and then not get woken up until after
  740. // the migration is complete, but in practice it won't happen and we can't
  741. // wait for someone to be waiting on a mutex.
  742. XCTestExpectation *notificationFired = [self expectationWithDescription:@"notification fired"];
  743. RLMNotificationToken *token = [syncRealm addNotificationBlock:^(NSString *, RLMRealm *) {
  744. if ([StringObject allObjectsInRealm:syncRealm].count == 5) {
  745. [notificationFired fulfill];
  746. }
  747. }];
  748. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  749. [token invalidate];
  750. // Release the write transaction and let them run
  751. [realm cancelWriteTransaction];
  752. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  753. }
  754. #endif
  755. - (void)testOpeningFileWithDifferentClassSubsetsInDifferentProcesses {
  756. if (!self.isParent) {
  757. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  758. config.objectClasses = @[StringObject.class];
  759. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  760. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  761. // Verify that the StringObject table actually exists
  762. [realm beginWriteTransaction];
  763. [StringObject createInRealm:realm withValue:@[@""]];
  764. [realm commitWriteTransaction];
  765. return;
  766. }
  767. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  768. config.objectClasses = @[IntObject.class];
  769. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  770. realm.autorefresh = false;
  771. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  772. [realm beginWriteTransaction];
  773. [IntObject createInRealm:realm withValue:@[@1]];
  774. [realm commitWriteTransaction];
  775. RLMRunChildAndWait();
  776. // Should be able to advance over the transaction creating a new table and
  777. // inserting a row into it
  778. XCTAssertNoThrow([realm refresh]);
  779. // Verify that the IntObject table didn't break
  780. XCTAssertEqual(1, [[IntObject allObjectsInRealm:realm].firstObject intCol]);
  781. [realm beginWriteTransaction];
  782. [IntObject createInRealm:realm withValue:@[@2]];
  783. // StringObject still isn't usable in this process since it isn't in the
  784. // class subset
  785. XCTAssertThrows([StringObject createInRealm:realm withValue:@[@""]]);
  786. [realm commitWriteTransaction];
  787. }
  788. - (void)testAddingIndexToExistingColumnInBackgroundProcess {
  789. if (!self.isParent) {
  790. RLMSchema *schema = [RLMSchema schemaWithObjectClasses:@[IntObject.class]];
  791. RLMObjectSchema *objectSchema = schema.objectSchema[0];
  792. RLMProperty *prop = objectSchema.properties[0];
  793. prop.indexed = YES;
  794. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  795. config.customSchema = schema;
  796. [RLMRealm realmWithConfiguration:config error:nil];
  797. return;
  798. }
  799. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  800. config.objectClasses = @[IntObject.class];
  801. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  802. realm.autorefresh = false;
  803. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  804. // Insert a value to ensure stuff actually happens when the index is added/removed
  805. [realm beginWriteTransaction];
  806. [IntObject createInRealm:realm withValue:@[@1]];
  807. [realm commitWriteTransaction];
  808. RLMRunChildAndWait();
  809. // Should accept the index change
  810. XCTAssertNoThrow([realm refresh]);
  811. }
  812. - (void)testRemovingIndexFromExistingColumnInBackgroundProcess {
  813. if (!self.isParent) {
  814. RLMSchema *schema = [RLMSchema schemaWithObjectClasses:@[IndexedStringObject.class]];
  815. RLMObjectSchema *objectSchema = schema.objectSchema[0];
  816. RLMProperty *prop = objectSchema.properties[0];
  817. prop.indexed = NO;
  818. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  819. config.customSchema = schema;
  820. [RLMRealm realmWithConfiguration:config error:nil];
  821. return;
  822. }
  823. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  824. config.objectClasses = @[IndexedStringObject.class];
  825. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  826. realm.autorefresh = false;
  827. XCTAssertEqual(1U, realm.schema.objectSchema.count);
  828. // Insert a value to ensure stuff actually happens when the index is added/removed
  829. [realm beginWriteTransaction];
  830. [IndexedStringObject createInRealm:realm withValue:@[@"1"]];
  831. [realm commitWriteTransaction];
  832. RLMRunChildAndWait();
  833. // Should accept the index change
  834. XCTAssertNoThrow([realm refresh]);
  835. }
  836. - (void)testMigratingToLaterVersionInBackgroundProcess {
  837. if (!self.isParent) {
  838. RLMSchema *schema = [RLMSchema schemaWithObjectClasses:@[IntObject.class]];
  839. RLMObjectSchema *objectSchema = schema.objectSchema[0];
  840. RLMProperty *prop = objectSchema.properties[0];
  841. prop.type = RLMPropertyTypeFloat;
  842. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  843. config.customSchema = schema;
  844. config.schemaVersion = 1;
  845. [RLMRealm realmWithConfiguration:config error:nil];
  846. return;
  847. }
  848. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  849. config.objectClasses = @[IntObject.class];
  850. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  851. realm.autorefresh = false;
  852. [realm beginWriteTransaction];
  853. [IntObject createInRealm:realm withValue:@[@1]];
  854. [realm commitWriteTransaction];
  855. RLMRunChildAndWait();
  856. // Should fail to refresh since we can't use later versions of the file due
  857. // to the schema change
  858. XCTAssertThrows([realm refresh]);
  859. XCTAssertThrows([realm beginWriteTransaction]);
  860. // Should have been left in a sensible state after the errors
  861. XCTAssertEqual(1, [[IntObject allObjectsInRealm:realm].firstObject intCol]);
  862. }
  863. - (void)testInsertingColumnsInBackgroundProcess {
  864. RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
  865. config.schemaMode = realm::SchemaMode::Additive;
  866. if (!self.isParent) {
  867. config.dynamic = true;
  868. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  869. [realm beginWriteTransaction];
  870. realm->_info[@"IntObject"].table()->insert_column(0, realm::type_String, "col");
  871. [realm commitWriteTransaction];
  872. return;
  873. }
  874. RLMRealm *realm = [RLMRealm realmWithConfiguration:config error:nil];
  875. [realm transactionWithBlock:^{
  876. [IntObject createInRealm:realm withValue:@[@5]];
  877. }];
  878. RLMRunChildAndWait();
  879. XCTAssertEqual(5, [[IntObject allObjectsInRealm:realm].firstObject intCol]);
  880. XCTAssertEqual(1U, [IntObject objectsInRealm:realm where:@"intCol = 5"].count);
  881. __block IntObject *io = [IntObject new];
  882. io.intCol = 6;
  883. [realm transactionWithBlock:^{ [realm addObject:io]; }];
  884. XCTAssertEqual(io.intCol, 6);
  885. XCTAssertEqualObjects(io[@"intCol"], @6);
  886. [realm transactionWithBlock:^{ io = [IntObject createInRealm:realm withValue:@[@7]]; }];
  887. XCTAssertEqual(io.intCol, 7);
  888. [realm transactionWithBlock:^{ io = [IntObject createInRealm:realm withValue:@{@"intCol": @8}]; }];
  889. XCTAssertEqual(io.intCol, 8);
  890. [realm transactionWithBlock:^{ io.intCol = 9; }];
  891. XCTAssertEqual(io.intCol, 9);
  892. [realm transactionWithBlock:^{ io[@"intCol"] = @10; }];
  893. XCTAssertEqual(io.intCol, 10);
  894. // Create query, add column, run query
  895. RLMResults *query = [IntObject objectsInRealm:realm where:@"intCol > 5"];
  896. RLMRunChildAndWait();
  897. XCTAssertEqual(query.count, 3U);
  898. // Create query, create TV, add column, reevaluate query
  899. query = [IntObject objectsInRealm:realm where:@"intCol > 5"];
  900. (void)[query lastObject];
  901. RLMRunChildAndWait();
  902. XCTAssertEqual(query.count, 3U);
  903. }
  904. #endif
  905. @end