SchemaTests.mm 46 KB

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