ObjectTests.m 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 "RLMTestCase.h"
  19. #import "RLMObjectSchema_Private.h"
  20. #import "RLMSchema_Private.h"
  21. #import <libkern/OSAtomic.h>
  22. #import <math.h>
  23. #import <objc/runtime.h>
  24. #import <stdalign.h>
  25. #pragma mark - Test Objects
  26. @interface DefaultObject : RLMObject
  27. @property int intCol;
  28. @property float floatCol;
  29. @property double doubleCol;
  30. @property BOOL boolCol;
  31. @property NSDate *dateCol;
  32. @property NSString *stringCol;
  33. @property NSData *binaryCol;
  34. @end
  35. @implementation DefaultObject
  36. + (NSDictionary *)defaultPropertyValues {
  37. NSString *binaryString = @"binary";
  38. NSData *binaryData = [binaryString dataUsingEncoding:NSUTF8StringEncoding];
  39. return @{@"intCol": @12,
  40. @"floatCol": @88.9f,
  41. @"doubleCol": @1002.892,
  42. @"boolCol": @YES,
  43. @"dateCol": [NSDate dateWithTimeIntervalSince1970:999999],
  44. @"stringCol": @"potato",
  45. @"binaryCol": binaryData};
  46. }
  47. @end
  48. @interface DynamicDefaultObject : RLMObject
  49. @property int intCol;
  50. @property float floatCol;
  51. @property double doubleCol;
  52. @property NSDate *dateCol;
  53. @property NSString *stringCol;
  54. @property NSData *binaryCol;
  55. @end
  56. @implementation DynamicDefaultObject
  57. + (BOOL)shouldIncludeInDefaultSchema {
  58. return NO;
  59. }
  60. + (NSDictionary *)defaultPropertyValues {
  61. static NSInteger dynamicDefaultSeed = 0;
  62. dynamicDefaultSeed++;
  63. return @{@"intCol": @(dynamicDefaultSeed),
  64. @"floatCol": @((float)dynamicDefaultSeed),
  65. @"doubleCol": @((double)dynamicDefaultSeed),
  66. @"dateCol": [NSDate dateWithTimeIntervalSince1970:dynamicDefaultSeed],
  67. @"stringCol": [[NSUUID UUID] UUIDString],
  68. @"binaryCol": [[[NSUUID UUID] UUIDString] dataUsingEncoding:NSUTF8StringEncoding]};
  69. }
  70. + (NSString *)primaryKey {
  71. return @"intCol";
  72. }
  73. @end
  74. @class CycleObject;
  75. RLM_ARRAY_TYPE(CycleObject)
  76. @interface CycleObject : RLMObject
  77. @property RLM_GENERIC_ARRAY(CycleObject) *objects;
  78. @end
  79. @implementation CycleObject
  80. @end
  81. @interface PrimaryStringObjectWrapper : RLMObject
  82. @property PrimaryStringObject *primaryStringObject;
  83. @end
  84. @implementation PrimaryStringObjectWrapper
  85. @end
  86. @interface PrimaryNestedObject : RLMObject
  87. @property int primaryCol;
  88. @property PrimaryStringObject *primaryStringObject;
  89. @property PrimaryStringObjectWrapper *primaryStringObjectWrapper;
  90. @property StringObject *stringObject;
  91. @property RLM_GENERIC_ARRAY(PrimaryIntObject) *primaryIntArray;
  92. @property NSString *stringCol;
  93. @end
  94. @implementation PrimaryNestedObject
  95. + (NSString *)primaryKey {
  96. return @"primaryCol";
  97. }
  98. + (NSDictionary *)defaultPropertyValues {
  99. return @{@"stringCol": @"default"};
  100. }
  101. @end
  102. @interface StringSubclassObject : StringObject
  103. @property NSString *stringCol2;
  104. @end
  105. @implementation StringSubclassObject
  106. @end
  107. @interface StringObjectNoThrow : StringObject
  108. @end
  109. @implementation StringObjectNoThrow
  110. - (id)valueForUndefinedKey:(__unused NSString *)key {
  111. return nil;
  112. }
  113. @end
  114. @interface StringSubclassObjectWithDefaults : StringObjectNoThrow
  115. @property NSString *stringCol2;
  116. @end
  117. @implementation StringSubclassObjectWithDefaults
  118. +(NSDictionary *)defaultPropertyValues {
  119. return @{@"stringCol2": @"default"};
  120. }
  121. @end
  122. @interface StringLinkObject : RLMObject
  123. @property StringObject *stringObjectCol;
  124. @property RLM_GENERIC_ARRAY(StringObject) *stringObjectArrayCol;
  125. @end
  126. @implementation StringLinkObject
  127. @end
  128. @interface ReadOnlyPropertyObject ()
  129. @property (readwrite) int readOnlyPropertyMadeReadWriteInClassExtension;
  130. @end
  131. @interface DataObject : RLMObject
  132. @property NSData *data1;
  133. @property NSData *data2;
  134. @end
  135. @implementation DataObject
  136. @end
  137. @interface DateObjectNoThrow : DateObject
  138. @property NSDate *date2;
  139. @end
  140. @implementation DateObjectNoThrow
  141. - (id)valueForUndefinedKey:(__unused NSString *)key {
  142. return nil;
  143. }
  144. @end
  145. @interface DateSubclassObject : DateObjectNoThrow
  146. @property NSDate *date3;
  147. @end
  148. @implementation DateSubclassObject
  149. @end
  150. @interface DateDefaultsObject : DateObjectNoThrow
  151. @property NSDate *date3;
  152. @end
  153. @implementation DateDefaultsObject
  154. + (NSDictionary *)defaultPropertyValues {
  155. return @{@"date3": [NSDate date]};
  156. }
  157. @end
  158. @interface SubclassDateObject : NSObject
  159. @property NSDate *dateCol;
  160. @property (getter=customGetter) NSDate *date2;
  161. @property (setter=customSetter:) NSDate *date3;
  162. @end
  163. @implementation SubclassDateObject
  164. @end
  165. #pragma mark - Tests
  166. @interface ObjectTests : RLMTestCase
  167. @end
  168. @implementation ObjectTests
  169. - (void)testKeyedSubscripting {
  170. EmployeeObject *objs = [[EmployeeObject alloc] initWithValue:@{@"name": @"Test0", @"age": @23, @"hired": @NO}];
  171. XCTAssertEqualObjects(objs[@"name"], @"Test0", @"Name should be Test0");
  172. XCTAssertEqualObjects(objs[@"age"], @23, @"age should be 23");
  173. XCTAssertEqualObjects(objs[@"hired"], @NO, @"hired should be NO");
  174. objs[@"name"] = @"Test1";
  175. XCTAssertEqualObjects(objs.name, @"Test1", @"Name should be Test1");
  176. RLMRealm *realm = [RLMRealm defaultRealm];
  177. [realm beginWriteTransaction];
  178. EmployeeObject *obj0 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Test1", @"age": @24, @"hired": @NO}];
  179. EmployeeObject *obj1 = [EmployeeObject createInRealm:realm withValue:@{@"name": @"Test2", @"age": @25, @"hired": @YES}];
  180. [realm commitWriteTransaction];
  181. XCTAssertEqualObjects(obj0[@"name"], @"Test1", @"Name should be Test1");
  182. XCTAssertEqualObjects(obj1[@"name"], @"Test2", @"Name should be Test1");
  183. [realm beginWriteTransaction];
  184. obj0[@"name"] = @"newName";
  185. [realm commitWriteTransaction];
  186. XCTAssertEqualObjects(obj0[@"name"], @"newName", @"Name should be newName");
  187. [realm beginWriteTransaction];
  188. obj0[@"name"] = nil;
  189. [realm commitWriteTransaction];
  190. XCTAssertNil(obj0[@"name"]);
  191. }
  192. - (void)testCannotUpdatePrimaryKey {
  193. PrimaryIntObject *intObj = [[PrimaryIntObject alloc] init];
  194. intObj.intCol = 1;
  195. XCTAssertNoThrow(intObj.intCol = 0);
  196. PrimaryStringObject *stringObj = [[PrimaryStringObject alloc] init];
  197. stringObj.stringCol = @"a";
  198. XCTAssertNoThrow(stringObj.stringCol = @"b");
  199. RLMRealm *realm = [RLMRealm defaultRealm];
  200. [realm beginWriteTransaction];
  201. [realm addObject:intObj];
  202. RLMAssertThrowsWithReason(intObj.intCol = 1, @"Primary key can't be changed");
  203. RLMAssertThrowsWithReason(intObj[@"intCol"] = @1, @"Primary key can't be changed");
  204. RLMAssertThrowsWithReason([intObj setValue:@1 forKey:@"intCol"], @"Primary key can't be changed");
  205. [realm addObject:stringObj];
  206. RLMAssertThrowsWithReason(stringObj.stringCol = @"a", @"Primary key can't be changed");
  207. RLMAssertThrowsWithReason(stringObj[@"stringCol"] = @"a", @"Primary key can't be changed");
  208. RLMAssertThrowsWithReason([stringObj setValue:@"a" forKey:@"stringCol"], @"Primary key can't be changed");
  209. [realm cancelWriteTransaction];
  210. }
  211. - (void)testDataTypes {
  212. RLMRealm *realm = [RLMRealm defaultRealm];
  213. [realm beginWriteTransaction];
  214. const char bin[4] = { 0, 1, 2, 3 };
  215. NSData *bin1 = [[NSData alloc] initWithBytes:bin length:sizeof bin / 2];
  216. NSData *bin2 = [[NSData alloc] initWithBytes:bin length:sizeof bin];
  217. NSDate *timeNow = [NSDate dateWithTimeIntervalSince1970:1000000];
  218. NSDate *timeZero = [NSDate dateWithTimeIntervalSince1970:0];
  219. AllTypesObject *c = [[AllTypesObject alloc] init];
  220. c.boolCol = NO;
  221. c.intCol = 54;
  222. c.floatCol = 0.7f;
  223. c.doubleCol = 0.8;
  224. c.stringCol = @"foo";
  225. c.binaryCol = bin1;
  226. c.dateCol = timeZero;
  227. c.cBoolCol = false;
  228. c.longCol = 99;
  229. c.objectCol = [[StringObject alloc] init];
  230. c.objectCol.stringCol = @"c";
  231. [realm addObject:c];
  232. [AllTypesObject createInRealm:realm withValue:@[@YES, @506, @7.7f, @8.8, @"banach", bin2,
  233. timeNow, @YES, @(-20), NSNull.null]];
  234. [realm commitWriteTransaction];
  235. AllTypesObject *row1 = [AllTypesObject allObjects][0];
  236. AllTypesObject *row2 = [AllTypesObject allObjects][1];
  237. XCTAssertEqual(row1.boolCol, NO, @"row1.BoolCol");
  238. XCTAssertEqual(row2.boolCol, YES, @"row2.BoolCol");
  239. XCTAssertEqual(row1.intCol, 54, @"row1.IntCol");
  240. XCTAssertEqual(row2.intCol, 506, @"row2.IntCol");
  241. XCTAssertEqual(row1.floatCol, 0.7f, @"row1.FloatCol");
  242. XCTAssertEqual(row2.floatCol, 7.7f, @"row2.FloatCol");
  243. XCTAssertEqual(row1.doubleCol, 0.8, @"row1.DoubleCol");
  244. XCTAssertEqual(row2.doubleCol, 8.8, @"row2.DoubleCol");
  245. XCTAssertTrue([row1.stringCol isEqual:@"foo"], @"row1.StringCol");
  246. XCTAssertTrue([row2.stringCol isEqual:@"banach"], @"row2.StringCol");
  247. XCTAssertTrue([row1.binaryCol isEqual:bin1], @"row1.BinaryCol");
  248. XCTAssertTrue([row2.binaryCol isEqual:bin2], @"row2.BinaryCol");
  249. XCTAssertTrue(([row1.dateCol isEqual:timeZero]), @"row1.DateCol");
  250. XCTAssertTrue(([row2.dateCol isEqual:timeNow]), @"row2.DateCol");
  251. XCTAssertEqual(row1.cBoolCol, false, @"row1.cBoolCol");
  252. XCTAssertEqual(row2.cBoolCol, true, @"row2.cBoolCol");
  253. XCTAssertEqual(row1.longCol, 99L, @"row1.IntCol");
  254. XCTAssertEqual(row2.longCol, -20L, @"row2.IntCol");
  255. XCTAssertTrue([row1.objectCol.stringCol isEqual:@"c"], @"row1.objectCol");
  256. XCTAssertNil(row2.objectCol, @"row2.objectCol");
  257. [realm transactionWithBlock:^{
  258. row1.boolCol = NO;
  259. row1.cBoolCol = false;
  260. row1.boolCol = (BOOL)6;
  261. row1.cBoolCol = (BOOL)6;
  262. }];
  263. XCTAssertEqual(row1.boolCol, true);
  264. XCTAssertEqual(row1.cBoolCol, true);
  265. AllTypesObject *o = [[AllTypesObject alloc] initWithValue:row1];
  266. o.floatCol = NAN;
  267. o.doubleCol = NAN;
  268. [realm transactionWithBlock:^{
  269. [realm addObject:o];
  270. }];
  271. XCTAssertTrue(isnan(o.floatCol));
  272. XCTAssertTrue(isnan(o.doubleCol));
  273. }
  274. - (void)testObjectSubclass {
  275. // test className methods
  276. XCTAssertEqualObjects(@"StringObject", [StringObject className]);
  277. XCTAssertEqualObjects(@"StringSubclassObject", [StringSubclassObject className]);
  278. RLMRealm *realm = [RLMRealm defaultRealm];
  279. [realm beginWriteTransaction];
  280. [StringObject createInDefaultRealmWithValue:@[@"string"]];
  281. StringSubclassObject *obj = [StringSubclassObject createInDefaultRealmWithValue:@[@"string", @"string2"]];
  282. // ensure property ordering
  283. XCTAssertEqualObjects([obj.objectSchema.properties[0] name], @"stringCol");
  284. XCTAssertEqualObjects([obj.objectSchema.properties[1] name], @"stringCol2");
  285. [realm commitWriteTransaction];
  286. // ensure creation in proper table
  287. RLMResults *results = StringSubclassObject.allObjects;
  288. XCTAssertEqual(1U, results.count);
  289. XCTAssertEqual(1U, StringObject.allObjects.count);
  290. // ensure exceptions on when using polymorphism
  291. [realm beginWriteTransaction];
  292. StringLinkObject *linkObject = [StringLinkObject createInDefaultRealmWithValue:@[NSNull.null, @[]]];
  293. RLMAssertThrowsWithReasonMatching(linkObject.stringObjectCol = obj,
  294. @"Can't .*StringSubclassObject.*StringObject");
  295. RLMAssertThrowsWithReasonMatching([linkObject.stringObjectArrayCol addObject:obj],
  296. @"Object of type .*StringSubclassObject.*does not match.*StringObject.*");
  297. [realm commitWriteTransaction];
  298. }
  299. - (void)testDateDistantFuture {
  300. RLMRealm *realm = [RLMRealm defaultRealm];
  301. [realm beginWriteTransaction];
  302. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[NSDate.distantFuture]];
  303. [realm commitWriteTransaction];
  304. XCTAssertEqualObjects(NSDate.distantFuture, dateObject.dateCol);
  305. }
  306. - (void)testDateDistantPast {
  307. RLMRealm *realm = [RLMRealm defaultRealm];
  308. [realm beginWriteTransaction];
  309. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[NSDate.distantPast]];
  310. [realm commitWriteTransaction];
  311. XCTAssertEqualObjects(NSDate.distantPast, dateObject.dateCol);
  312. }
  313. - (void)testDate50kYears {
  314. NSCalendarUnit units = (NSCalendarUnit)(NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay);
  315. NSDateComponents *components = [[NSCalendar currentCalendar] components:units fromDate:NSDate.date];
  316. components.calendar = [NSCalendar currentCalendar];
  317. components.year += 50000;
  318. RLMRealm *realm = [RLMRealm defaultRealm];
  319. [realm beginWriteTransaction];
  320. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[components.date]];
  321. [realm commitWriteTransaction];
  322. XCTAssertEqualObjects(components.date, dateObject.dateCol);
  323. }
  324. static void testDatesInRange(NSTimeInterval from, NSTimeInterval to, void (^check)(NSDate *, NSDate *)) {
  325. NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:from];
  326. RLMRealm *realm = [RLMRealm defaultRealm];
  327. [realm beginWriteTransaction];
  328. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[date]];
  329. while (from < to) @autoreleasepool {
  330. check(dateObject.dateCol, date);
  331. from = nextafter(from, DBL_MAX);
  332. date = [NSDate dateWithTimeIntervalSinceReferenceDate:from];
  333. dateObject.dateCol = date;
  334. }
  335. [realm commitWriteTransaction];
  336. }
  337. - (void)testExactRepresentationOfDatesAroundNow {
  338. NSDate *date = [NSDate date];
  339. NSTimeInterval time = date.timeIntervalSinceReferenceDate;
  340. testDatesInRange(time - .001, time + .001, ^(NSDate *d1, NSDate *d2) {
  341. XCTAssertEqualObjects(d1, d2);
  342. });
  343. }
  344. - (void)testExactRepresentationOfDatesAroundDistantFuture {
  345. NSDate *date = [NSDate distantFuture];
  346. NSTimeInterval time = date.timeIntervalSinceReferenceDate;
  347. testDatesInRange(time - .001, time + .001, ^(NSDate *d1, NSDate *d2) {
  348. XCTAssertEqualObjects(d1, d2);
  349. });
  350. }
  351. - (void)testExactRepresentationOfDatesAroundEpoch {
  352. NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
  353. NSTimeInterval time = date.timeIntervalSinceReferenceDate;
  354. testDatesInRange(time - .001, time + .001, ^(NSDate *d1, NSDate *d2) {
  355. XCTAssertEqualObjects(d1, d2);
  356. });
  357. }
  358. - (void)testExactRepresentationOfDatesAroundReferenceDate {
  359. RLMRealm *realm = [RLMRealm defaultRealm];
  360. [realm beginWriteTransaction];
  361. NSDate *zero = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
  362. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[zero]];
  363. XCTAssertEqualObjects(dateObject.dateCol, zero);
  364. // Just shy of 1ns should still be zero
  365. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:nextafter(1e-9, -DBL_MAX)];
  366. XCTAssertEqualObjects(dateObject.dateCol, zero);
  367. // Very slightly over 1ns (since 1e-9 can't be exactly represented by a double)
  368. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:1e-9];
  369. XCTAssertNotEqualObjects(dateObject.dateCol, zero);
  370. // Round toward zero, so -1ns + epsilon is zero
  371. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:nextafter(0, -DBL_MAX)];
  372. XCTAssertEqualObjects(dateObject.dateCol, zero);
  373. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:nextafter(-1e-9, DBL_MAX)];
  374. XCTAssertEqualObjects(dateObject.dateCol, zero);
  375. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:-1e-9];
  376. XCTAssertNotEqualObjects(dateObject.dateCol, zero);
  377. [realm commitWriteTransaction];
  378. }
  379. - (void)testDatesOutsideOfTimestampRange {
  380. NSDate *date = [NSDate date];
  381. NSDate *maxDate = [NSDate dateWithTimeIntervalSince1970:(double)(1ULL << 63) + .999999999];
  382. NSDate *minDate = [NSDate dateWithTimeIntervalSince1970:-(double)(1ULL << 63) - .999999999];
  383. NSDate *justOverMaxDate = [NSDate dateWithTimeIntervalSince1970:nextafter(maxDate.timeIntervalSince1970, DBL_MAX)];
  384. NSDate *justUnderMaxDate = [NSDate dateWithTimeIntervalSince1970:nextafter(maxDate.timeIntervalSince1970, -DBL_MAX)];
  385. NSDate *justOverMinDate = [NSDate dateWithTimeIntervalSince1970:nextafter(minDate.timeIntervalSince1970, DBL_MAX)];
  386. NSDate *justUnderMinDate = [NSDate dateWithTimeIntervalSince1970:nextafter(minDate.timeIntervalSince1970, -DBL_MAX)];
  387. RLMRealm *realm = [RLMRealm defaultRealm];
  388. [realm beginWriteTransaction];
  389. DateObject *dateObject = [DateObject createInRealm:realm withValue:@[date]];
  390. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:0.0/0.0];
  391. XCTAssertEqualObjects(dateObject.dateCol, [NSDate dateWithTimeIntervalSince1970:0]);
  392. dateObject.dateCol = maxDate;
  393. XCTAssertEqualObjects(dateObject.dateCol, maxDate);
  394. dateObject.dateCol = justOverMaxDate;
  395. XCTAssertEqualObjects(dateObject.dateCol, maxDate);
  396. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:DBL_MAX];
  397. XCTAssertEqualObjects(dateObject.dateCol, maxDate);
  398. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:1.0/0.0];
  399. XCTAssertEqualObjects(dateObject.dateCol, maxDate);
  400. dateObject.dateCol = minDate;
  401. XCTAssertEqualObjects(dateObject.dateCol, minDate);
  402. dateObject.dateCol = justUnderMinDate;
  403. XCTAssertEqualObjects(dateObject.dateCol, minDate);
  404. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:-DBL_MAX];
  405. XCTAssertEqualObjects(dateObject.dateCol, minDate);
  406. dateObject.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:-1.0/0.0];
  407. XCTAssertEqualObjects(dateObject.dateCol, minDate);
  408. dateObject.dateCol = justUnderMaxDate;
  409. XCTAssertEqualObjects(dateObject.dateCol, justUnderMaxDate);
  410. dateObject.dateCol = justOverMinDate;
  411. XCTAssertEqualObjects(dateObject.dateCol, justOverMinDate);
  412. [realm commitWriteTransaction];
  413. }
  414. - (void)testDataSizeLimits {
  415. RLMRealm *realm = [RLMRealm defaultRealm];
  416. // Allocation must be < 16 MB, with an 8-byte header and the allocation size
  417. // 8-byte aligned
  418. static const int maxSize = 0xFFFFFF - 15;
  419. // Multiple 16 MB blobs should be fine
  420. void *buffer = malloc(maxSize);
  421. strcpy((char *)buffer + maxSize - sizeof("hello") - 1, "hello");
  422. DataObject *obj = [[DataObject alloc] init];
  423. obj.data1 = obj.data2 = [NSData dataWithBytesNoCopy:buffer length:maxSize freeWhenDone:YES];
  424. [realm beginWriteTransaction];
  425. [realm addObject:obj];
  426. [realm commitWriteTransaction];
  427. XCTAssertEqual(maxSize, obj.data1.length);
  428. XCTAssertEqual(maxSize, obj.data2.length);
  429. XCTAssertTrue(strcmp((const char *)obj.data1.bytes + obj.data1.length - sizeof("hello") - 1, "hello") == 0);
  430. XCTAssertTrue(strcmp((const char *)obj.data2.bytes + obj.data2.length - sizeof("hello") - 1, "hello") == 0);
  431. // A blob over 16 MB should throw (and not crash)
  432. [realm beginWriteTransaction];
  433. RLMAssertThrowsWithReason(obj.data1 = [NSData dataWithBytesNoCopy:malloc(maxSize + 1)
  434. length:maxSize + 1 freeWhenDone:YES],
  435. @"Binary too big");
  436. [realm commitWriteTransaction];
  437. }
  438. - (void)testStringSizeLimits {
  439. RLMRealm *realm = [RLMRealm defaultRealm];
  440. // Allocation must be < 16 MB, with an 8-byte header, trailing NUL, and the
  441. // allocation size 8-byte aligned
  442. static const int maxSize = 0xFFFFFF - 16;
  443. void *buffer = calloc(maxSize, 1);
  444. strcpy((char *)buffer + maxSize - sizeof("hello") - 1, "hello");
  445. NSString *str = [[NSString alloc] initWithBytesNoCopy:buffer length:maxSize
  446. encoding:NSUTF8StringEncoding freeWhenDone:YES];
  447. StringObject *obj = [[StringObject alloc] init];
  448. obj.stringCol = str;
  449. [realm beginWriteTransaction];
  450. [realm addObject:obj];
  451. [realm commitWriteTransaction];
  452. XCTAssertEqualObjects(str, obj.stringCol);
  453. // A blob over 16 MB should throw (and not crash)
  454. [realm beginWriteTransaction];
  455. XCTAssertThrows(obj.stringCol = [[NSString alloc] initWithBytesNoCopy:calloc(maxSize + 1, 1)
  456. length:maxSize + 1
  457. encoding:NSUTF8StringEncoding
  458. freeWhenDone:YES]);
  459. [realm commitWriteTransaction];
  460. }
  461. - (void)testAddingObjectNotInSchemaThrows {
  462. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  463. configuration.objectClasses = @[StringObject.class];
  464. RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:nil];
  465. [realm beginWriteTransaction];
  466. RLMAssertThrowsWithReasonMatching([realm addObject:[[IntObject alloc] initWithValue:@[@1]]],
  467. @"Object type 'IntObject' is not managed by the Realm.*custom `objectClasses`");
  468. RLMAssertThrowsWithReasonMatching([IntObject createInRealm:realm withValue:@[@1]],
  469. @"Object type 'IntObject' is not managed by the Realm.*custom `objectClasses`");
  470. XCTAssertNoThrow([realm addObject:[[StringObject alloc] initWithValue:@[@"A"]]]);
  471. XCTAssertNoThrow([StringObject createInRealm:realm withValue:@[@"A"]]);
  472. [realm cancelWriteTransaction];
  473. }
  474. static void addProperty(Class cls, const char *name, const char *type, size_t size, size_t align, id getter) {
  475. objc_property_attribute_t objectColAttrs[] = {
  476. {"T", type},
  477. {"V", name},
  478. };
  479. class_addIvar(cls, name, size, align, type);
  480. class_addProperty(cls, name, objectColAttrs, sizeof(objectColAttrs) / sizeof(objc_property_attribute_t));
  481. char encoding[4] = " @:";
  482. encoding[0] = *type;
  483. class_addMethod(cls, sel_registerName(name), imp_implementationWithBlock(getter), encoding);
  484. }
  485. - (void)testObjectSubclassAddedAtRuntime {
  486. Class objectClass = objc_allocateClassPair(RLMObject.class, "RuntimeGeneratedObject", 0);
  487. addProperty(objectClass, "objectCol", "@\"RuntimeGeneratedObject\"", sizeof(id), alignof(id), ^(__unused id obj) { return nil; });
  488. addProperty(objectClass, "intCol", "i", sizeof(int), alignof(int), ^int(__unused id obj) { return 0; });
  489. objc_registerClassPair(objectClass);
  490. XCTAssertEqualObjects([objectClass className], @"RuntimeGeneratedObject");
  491. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  492. configuration.objectClasses = @[objectClass];
  493. XCTAssertEqualObjects([objectClass className], @"RuntimeGeneratedObject");
  494. RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:nil];
  495. [realm beginWriteTransaction];
  496. id object = [objectClass createInRealm:realm withValue:@{@"objectCol": [[objectClass alloc] init], @"intCol": @17}];
  497. RLMObjectSchema *schema = [object objectSchema];
  498. XCTAssertNotNil(schema[@"objectCol"]);
  499. XCTAssertNotNil(schema[@"intCol"]);
  500. XCTAssert([[object objectCol] isKindOfClass:objectClass]);
  501. XCTAssertEqual([object intCol], 17);
  502. [realm commitWriteTransaction];
  503. }
  504. #pragma mark - Default Property Values
  505. - (NSDictionary *)defaultValuesDictionary {
  506. return @{@"intCol" : @98,
  507. @"floatCol" : @231.0f,
  508. @"doubleCol": @123732.9231,
  509. @"boolCol" : @NO,
  510. @"dateCol" : [NSDate dateWithTimeIntervalSince1970:454321],
  511. @"stringCol": @"Westeros",
  512. @"binaryCol": [@"inputData" dataUsingEncoding:NSUTF8StringEncoding]};
  513. }
  514. - (void)testDefaultValuesFromNoValuePresent {
  515. RLMRealm *realm = [RLMRealm defaultRealm];
  516. [realm beginWriteTransaction];
  517. NSDictionary *inputValues = [self defaultValuesDictionary];
  518. NSArray *keys = [inputValues allKeys]; // To ensure iteration order is stable
  519. for (NSString *key in keys) {
  520. NSMutableDictionary *dict = [inputValues mutableCopy];
  521. [dict removeObjectForKey:key];
  522. [DefaultObject createInRealm:realm withValue:dict];
  523. }
  524. [realm commitWriteTransaction];
  525. // Test allObject for DefaultObject
  526. NSDictionary *defaultValues = [DefaultObject defaultPropertyValues];
  527. RLMResults *allObjects = [DefaultObject allObjectsInRealm:realm];
  528. for (NSUInteger i = 0; i < keys.count; ++i) {
  529. DefaultObject *object = allObjects[i];
  530. for (NSUInteger j = 0; j < keys.count; ++j) {
  531. NSString *key = keys[j];
  532. if (i == j) {
  533. XCTAssertEqualObjects(object[key], defaultValues[key]);
  534. }
  535. else {
  536. XCTAssertEqualObjects(object[key], inputValues[key]);
  537. }
  538. }
  539. }
  540. }
  541. - (void)testDefaultValuesFromNSNull {
  542. RLMRealm *realm = [RLMRealm defaultRealm];
  543. NSDictionary *defaultValues = [DefaultObject defaultPropertyValues];
  544. NSDictionary *inputValues = [self defaultValuesDictionary];
  545. NSArray *keys = [inputValues allKeys]; // To ensure iteration order is stable
  546. for (NSString *key in keys) {
  547. NSMutableDictionary *dict = [inputValues mutableCopy];
  548. dict[key] = NSNull.null;
  549. RLMProperty *prop = realm.schema[@"DefaultObject"][key];
  550. if (prop.optional) {
  551. [realm beginWriteTransaction];
  552. [DefaultObject createInRealm:realm withValue:dict];
  553. [realm commitWriteTransaction];
  554. DefaultObject *object = DefaultObject.allObjects.lastObject;
  555. for (NSUInteger j = 0; j < keys.count; ++j) {
  556. NSString *key2 = keys[j];
  557. if ([key isEqualToString:key2]) {
  558. XCTAssertEqualObjects(object[key2], prop.optional ? nil : defaultValues[key2]);
  559. }
  560. else {
  561. XCTAssertEqualObjects(object[key2], inputValues[key2]);
  562. }
  563. }
  564. }
  565. else {
  566. [realm beginWriteTransaction];
  567. RLMAssertThrowsWithReason([DefaultObject createInRealm:realm withValue:dict],
  568. @"Invalid value '<null>' of type 'NSNull' for ");
  569. [realm commitWriteTransaction];
  570. }
  571. }
  572. }
  573. - (void)testDefaultNSNumberPropertyValues {
  574. void (^assertDefaults)(NumberObject *) = ^(NumberObject *no) {
  575. XCTAssertEqualObjects(no.intObj, @1);
  576. XCTAssertEqualObjects(no.floatObj, @2.2f);
  577. XCTAssertEqualObjects(no.doubleObj, @3.3);
  578. XCTAssertEqualObjects(no.boolObj, @NO);
  579. };
  580. assertDefaults([[NumberDefaultsObject alloc] init]);
  581. RLMRealm *realm = [RLMRealm defaultRealm];
  582. [realm beginWriteTransaction];
  583. assertDefaults([NumberDefaultsObject createInRealm:realm withValue:@{}]);
  584. [realm cancelWriteTransaction];
  585. }
  586. - (void)testDynamicDefaultPropertyValues {
  587. void (^assertDifferentPropertyValues)(DynamicDefaultObject *, DynamicDefaultObject *) = ^(DynamicDefaultObject *obj1, DynamicDefaultObject *obj2) {
  588. XCTAssertNotEqual(obj1.intCol, obj2.intCol);
  589. XCTAssertNotEqual(obj1.floatCol, obj2.floatCol);
  590. XCTAssertNotEqual(obj1.doubleCol, obj2.doubleCol);
  591. XCTAssertNotEqualWithAccuracy(obj1.dateCol.timeIntervalSinceReferenceDate, obj2.dateCol.timeIntervalSinceReferenceDate, 0.01f);
  592. XCTAssertNotEqualObjects(obj1.stringCol, obj2.stringCol);
  593. XCTAssertNotEqualObjects(obj1.binaryCol, obj2.binaryCol);
  594. };
  595. assertDifferentPropertyValues([[DynamicDefaultObject alloc] init], [[DynamicDefaultObject alloc] init]);
  596. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  597. configuration.objectClasses = @[[DynamicDefaultObject class]];
  598. RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:nil];
  599. [realm beginWriteTransaction];
  600. assertDifferentPropertyValues([DynamicDefaultObject createInRealm:realm withValue:@{}], [DynamicDefaultObject createInRealm:realm withValue:@{}]);
  601. [realm cancelWriteTransaction];
  602. }
  603. #pragma mark - Ignored Properties
  604. - (void)testCanUseIgnoredProperty {
  605. NSURL *url = [NSURL URLWithString:@"http://realm.io"];
  606. RLMRealm *realm = [RLMRealm defaultRealm];
  607. [realm beginWriteTransaction];
  608. IgnoredURLObject *obj = [IgnoredURLObject new];
  609. obj.name = @"Realm";
  610. obj.url = url;
  611. [realm addObject:obj];
  612. XCTAssertEqual(obj.url, url, @"ignored properties should still be assignable and gettable inside a write block");
  613. [realm commitWriteTransaction];
  614. XCTAssertEqual(obj.url, url, @"ignored properties should still be assignable and gettable outside a write block");
  615. IgnoredURLObject *obj2 = [[IgnoredURLObject objectsWithPredicate:nil] firstObject];
  616. XCTAssertNotNil(obj2, @"object with ignored property should still be stored and accessible through the realm");
  617. XCTAssertEqualObjects(obj2.name, obj.name, @"managed property should be the same");
  618. XCTAssertNil(obj2.url, @"ignored property should be nil when getting from realm");
  619. }
  620. - (void)testCreateInRealmValidationForDictionary {
  621. RLMRealm *realm = [RLMRealm defaultRealm];
  622. const char bin[4] = { 0, 1, 2, 3 };
  623. NSData *bin1 = [[NSData alloc] initWithBytes:bin length:sizeof bin / 2];
  624. NSDate *timeNow = [NSDate dateWithTimeIntervalSince1970:1000000];
  625. NSDictionary * const dictValidAllTypes = @{@"boolCol" : @NO,
  626. @"intCol" : @54,
  627. @"floatCol" : @0.7f,
  628. @"doubleCol": @0.8,
  629. @"stringCol": @"foo",
  630. @"binaryCol": bin1,
  631. @"dateCol" : timeNow,
  632. @"cBoolCol" : @NO,
  633. @"longCol" : @(99),
  634. @"objectCol": NSNull.null};
  635. [realm beginWriteTransaction];
  636. // Test NSDictonary
  637. XCTAssertNoThrow(([AllTypesObject createInRealm:realm withValue:dictValidAllTypes]),
  638. @"Creating object with valid value types should not throw exception");
  639. for (NSString *keyToInvalidate in dictValidAllTypes.allKeys) {
  640. NSMutableDictionary *invalidInput = [dictValidAllTypes mutableCopy];
  641. id obj = @"invalid";
  642. if ([keyToInvalidate isEqualToString:@"stringCol"]) {
  643. obj = @1;
  644. }
  645. invalidInput[keyToInvalidate] = obj;
  646. RLMAssertThrowsWithReasonMatching([AllTypesObject createInRealm:realm withValue:invalidInput],
  647. @"Invalid value '.*'");
  648. }
  649. [realm commitWriteTransaction];
  650. }
  651. - (void)testCreateInRealmValidationForArray {
  652. RLMRealm *realm = [RLMRealm defaultRealm];
  653. // add test/link object to realm
  654. [realm beginWriteTransaction];
  655. StringObject *to = [StringObject createInRealm:realm withValue:@[@"c"]];
  656. [realm commitWriteTransaction];
  657. const char bin[4] = { 0, 1, 2, 3 };
  658. NSData *bin1 = [[NSData alloc] initWithBytes:bin length:sizeof bin / 2];
  659. NSDate *timeNow = [NSDate dateWithTimeIntervalSince1970:1000000];
  660. NSArray *const arrayValidAllTypes = @[@NO, @54, @0.7f, @0.8, @"foo", bin1, timeNow, @NO, @(99), to];
  661. [realm beginWriteTransaction];
  662. // Test NSArray
  663. XCTAssertNoThrow(([AllTypesObject createInRealm:realm withValue:arrayValidAllTypes]),
  664. @"Creating object with valid value types should not throw exception");
  665. const NSInteger stringColIndex = 4;
  666. for (NSUInteger i = 0; i < arrayValidAllTypes.count; i++) {
  667. NSMutableArray *invalidInput = [arrayValidAllTypes mutableCopy];
  668. id obj = @"invalid";
  669. if (i == stringColIndex) {
  670. obj = @1;
  671. }
  672. invalidInput[i] = obj;
  673. RLMAssertThrowsWithReasonMatching([AllTypesObject createInRealm:realm withValue:invalidInput],
  674. @"Invalid value '.*'");
  675. }
  676. [realm commitWriteTransaction];
  677. }
  678. - (void)testCreateInRealmReusesExistingObjects {
  679. RLMRealm *realm = [RLMRealm defaultRealm];
  680. [realm beginWriteTransaction];
  681. DogObject *dog = [DogObject createInDefaultRealmWithValue:@[@"Fido", @5]];
  682. OwnerObject *owner = [OwnerObject createInDefaultRealmWithValue:@[@"name", dog]];
  683. XCTAssertTrue([owner.dog isEqualToObject:dog]);
  684. XCTAssertEqual(1U, DogObject.allObjects.count);
  685. DogArrayObject *dogArray = [DogArrayObject createInDefaultRealmWithValue:@[@[dog]]];
  686. XCTAssertTrue([dogArray.dogs[0] isEqualToObject:dog]);
  687. XCTAssertEqual(1U, DogObject.allObjects.count);
  688. [realm commitWriteTransaction];
  689. }
  690. - (void)testCreateInRealmReusesExistingNestedObjectsByPrimaryKey {
  691. RLMRealm *realm = [RLMRealm defaultRealm];
  692. [realm beginWriteTransaction];
  693. PrimaryEmployeeObject *eo = [PrimaryEmployeeObject createInRealm:realm withValue:@[@"Samuel", @19, @NO]];
  694. PrimaryCompanyObject *co = [PrimaryCompanyObject createInRealm:realm withValue:@[@"Realm", @[eo], eo, @[eo]]];
  695. [realm commitWriteTransaction];
  696. [realm beginWriteTransaction];
  697. [PrimaryCompanyObject createOrUpdateInRealm:realm withValue:@{
  698. @"name": @"Realm",
  699. @"intern": @{@"name":@"Samuel", @"hired":@YES},
  700. }];
  701. [realm commitWriteTransaction];
  702. XCTAssertEqual(1U, co.employees.count);
  703. XCTAssertEqual(1U, [PrimaryEmployeeObject allObjectsInRealm:realm].count);
  704. XCTAssertEqualObjects(@"Samuel", eo.name);
  705. XCTAssertEqual(YES, eo.hired);
  706. XCTAssertEqual(19, eo.age);
  707. [realm beginWriteTransaction];
  708. [PrimaryCompanyObject createOrUpdateInRealm:realm withValue:@{
  709. @"name": @"Realm",
  710. @"employees": @[@{@"name":@"Samuel", @"hired":@NO}],
  711. @"intern": @{@"name":@"Samuel", @"age":@20},
  712. }];
  713. [realm commitWriteTransaction];
  714. XCTAssertEqual(1U, co.employees.count);
  715. XCTAssertEqual(1U, [PrimaryEmployeeObject allObjectsInRealm:realm].count);
  716. XCTAssertEqualObjects(@"Samuel", eo.name);
  717. XCTAssertEqual(NO, eo.hired);
  718. XCTAssertEqual(20, eo.age);
  719. [realm beginWriteTransaction];
  720. [PrimaryCompanyObject createOrUpdateInRealm:realm withValue:@{@"name": @"Realm",
  721. @"wrappedIntern": @[eo]}];
  722. [realm commitWriteTransaction];
  723. XCTAssertEqual(1U, [[PrimaryEmployeeObject allObjectsInRealm:realm] count]);
  724. }
  725. - (void)testCreateInRealmCopiesFromOtherRealm {
  726. RLMRealm *realm1 = [RLMRealm defaultRealm];
  727. RLMRealm *realm2 = [self realmWithTestPath];
  728. [realm1 beginWriteTransaction];
  729. [realm2 beginWriteTransaction];
  730. DogObject *dog = [DogObject createInDefaultRealmWithValue:@[@"Fido", @5]];
  731. OwnerObject *owner = [OwnerObject createInRealm:realm2 withValue:@[@"name", dog]];
  732. XCTAssertFalse([owner.dog isEqualToObject:dog]);
  733. XCTAssertEqual(1U, DogObject.allObjects.count);
  734. XCTAssertEqual(1U, [DogObject allObjectsInRealm:realm2].count);
  735. DogArrayObject *dogArray = [DogArrayObject createInRealm:realm2 withValue:@[@[dog]]];
  736. XCTAssertFalse([dogArray.dogs[0] isEqualToObject:dog]);
  737. XCTAssertEqual(1U, DogObject.allObjects.count);
  738. XCTAssertEqual(2U, [DogObject allObjectsInRealm:realm2].count);
  739. [realm1 commitWriteTransaction];
  740. [realm2 commitWriteTransaction];
  741. }
  742. - (void)testCreateInRealmWithOtherObjects {
  743. RLMRealm *realm = [RLMRealm defaultRealm];
  744. [realm beginWriteTransaction];
  745. DateObjectNoThrow *object = [DateObjectNoThrow createInDefaultRealmWithValue:@[NSDate.date, NSDate.date]];
  746. // create subclass with instance of base class with/without default objects
  747. XCTAssertNoThrow([DateSubclassObject createInDefaultRealmWithValue:object]);
  748. XCTAssertNoThrow([DateObjectNoThrow createInDefaultRealmWithValue:object]);
  749. // create using non-realm object with custom getter
  750. SubclassDateObject *obj = [SubclassDateObject new];
  751. obj.dateCol = [NSDate dateWithTimeIntervalSinceReferenceDate:1000];
  752. obj.date2 = [NSDate dateWithTimeIntervalSinceReferenceDate:2000];
  753. obj.date3 = [NSDate dateWithTimeIntervalSinceReferenceDate:3000];
  754. [DateDefaultsObject createInDefaultRealmWithValue:obj];
  755. XCTAssertEqual(2U, DateObjectNoThrow.allObjects.count);
  756. [realm commitWriteTransaction];
  757. }
  758. - (void)testObjectDescription {
  759. RLMRealm *realm = [RLMRealm defaultRealm];
  760. [realm beginWriteTransaction];
  761. // Init object before adding to realm
  762. EmployeeObject *soInit = [[EmployeeObject alloc] init];
  763. soInit.name = @"Peter";
  764. soInit.age = 30;
  765. soInit.hired = YES;
  766. [realm addObject:soInit];
  767. // description asserts block
  768. void (^descriptionAsserts)(NSString *) = ^(NSString *description) {
  769. XCTAssertTrue([description rangeOfString:@"name"].location != NSNotFound,
  770. @"column names should be displayed when calling \"description\" on RLMObject subclasses");
  771. XCTAssertTrue([description rangeOfString:@"Peter"].location != NSNotFound,
  772. @"column values should be displayed when calling \"description\" on RLMObject subclasses");
  773. XCTAssertTrue([description rangeOfString:@"age"].location != NSNotFound,
  774. @"column names should be displayed when calling \"description\" on RLMObject subclasses");
  775. XCTAssertTrue([description rangeOfString:[@30 description]].location != NSNotFound,
  776. @"column values should be displayed when calling \"description\" on RLMObject subclasses");
  777. XCTAssertTrue([description rangeOfString:@"hired"].location != NSNotFound,
  778. @"column names should be displayed when calling \"description\" on RLMObject subclasses");
  779. XCTAssertTrue([description rangeOfString:[@YES description]].location != NSNotFound,
  780. @"column values should be displayed when calling \"description\" on RLMObject subclasses");
  781. };
  782. // Test description in write block
  783. descriptionAsserts(soInit.description);
  784. [realm commitWriteTransaction];
  785. // Test description in read block
  786. NSString *objDescription = [[[EmployeeObject objectsWithPredicate:nil] firstObject] description];
  787. descriptionAsserts(objDescription);
  788. soInit = [[EmployeeObject alloc] init];
  789. soInit.age = 20;
  790. XCTAssert([soInit.description rangeOfString:@"(null)"].location != NSNotFound);
  791. }
  792. - (void)testObjectCycleDescription {
  793. CycleObject *obj = [[CycleObject alloc] init];
  794. [RLMRealm.defaultRealm transactionWithBlock:^{
  795. [RLMRealm.defaultRealm addObject:obj];
  796. [obj.objects addObject:obj];
  797. }];
  798. XCTAssertNoThrow(obj.description);
  799. }
  800. - (void)testDataObjectDescription {
  801. RLMRealm *realm = [RLMRealm defaultRealm];
  802. [realm beginWriteTransaction];
  803. char longData[200];
  804. [DataObject createInRealm:realm withValue:@[[NSData dataWithBytes:&longData length:200], [NSData dataWithBytes:&longData length:2]]];
  805. [realm commitWriteTransaction];
  806. DataObject *obj = [DataObject allObjectsInRealm:realm].firstObject;
  807. XCTAssertTrue([obj.description rangeOfString:@"200 total bytes"].location != NSNotFound);
  808. XCTAssertTrue([obj.description rangeOfString:@"2 total bytes"].location != NSNotFound);
  809. }
  810. - (void)testDeletedObjectDescription {
  811. RLMRealm *realm = [RLMRealm defaultRealm];
  812. [realm beginWriteTransaction];
  813. EmployeeObject *obj = [EmployeeObject createInRealm:realm withValue:@[@"Peter", @30, @YES]];
  814. [realm deleteObject:obj];
  815. [realm commitWriteTransaction];
  816. XCTAssertNoThrow(obj.description);
  817. }
  818. - (void)testManagedObjectUnknownKey {
  819. IntObject *obj = [[IntObject alloc] init];
  820. RLMRealm *realm = [RLMRealm defaultRealm];
  821. [realm beginWriteTransaction];
  822. [realm addObject:obj];
  823. [realm commitWriteTransaction];
  824. RLMAssertThrowsWithReason([obj objectForKeyedSubscript:@""],
  825. @"Invalid property name '' for class 'IntObject'");
  826. RLMAssertThrowsWithReason([obj setObject:@0 forKeyedSubscript:@""],
  827. @"Invalid property name '' for class 'IntObject'");
  828. }
  829. - (void)testUnmanagedRealmObjectUnknownKey {
  830. IntObject *obj = [[IntObject alloc] init];
  831. XCTAssertThrows([obj objectForKeyedSubscript:@""]);
  832. XCTAssertThrows([obj setObject:@0 forKeyedSubscript:@""]);
  833. }
  834. - (void)testEquality {
  835. IntObject *obj = [[IntObject alloc] init];
  836. IntObject *otherObj = [[IntObject alloc] init];
  837. RLMRealm *realm = [RLMRealm defaultRealm];
  838. RLMRealm *otherRealm = [self realmWithTestPath];
  839. XCTAssertFalse([obj isEqual:[NSObject new]], @"Comparing an RLMObject to a non-RLMObject should be false.");
  840. XCTAssertFalse([obj isEqualToObject:(RLMObject *)[NSObject new]], @"Comparing an RLMObject to a non-RLMObject should be false.");
  841. XCTAssertTrue([obj isEqual:obj], @"Same instance.");
  842. XCTAssertTrue([obj isEqualToObject:obj], @"Same instance.");
  843. XCTAssertFalse([obj isEqualToObject:otherObj], @"Comparison outside of realm.");
  844. [realm beginWriteTransaction];
  845. [realm addObject:obj];
  846. [realm commitWriteTransaction];
  847. XCTAssertFalse([obj isEqualToObject:otherObj], @"One in realm, the other is not.");
  848. XCTAssertTrue([obj isEqualToObject:[IntObject allObjects][0]], @"Same table and index.");
  849. [otherRealm beginWriteTransaction];
  850. [otherRealm addObject:otherObj];
  851. [otherRealm commitWriteTransaction];
  852. XCTAssertFalse([obj isEqualToObject:otherObj], @"Different realms.");
  853. [realm beginWriteTransaction];
  854. [realm addObject:[[IntObject alloc] init]];
  855. [realm addObject:[[BoolObject alloc] init]];
  856. [realm commitWriteTransaction];
  857. XCTAssertFalse([obj isEqualToObject:[IntObject allObjects][1]], @"Same table, different index.");
  858. XCTAssertFalse([obj isEqualToObject:[BoolObject allObjects][0]], @"Different tables.");
  859. }
  860. - (void)testCrossThreadAccess {
  861. IntObject *obj = [[IntObject alloc] init];
  862. // Unmanaged object can be accessed from other threads
  863. [self dispatchAsyncAndWait:^{ XCTAssertNoThrow(obj.intCol = 5); }];
  864. [RLMRealm.defaultRealm beginWriteTransaction];
  865. [RLMRealm.defaultRealm addObject:obj];
  866. [RLMRealm.defaultRealm commitWriteTransaction];
  867. [self dispatchAsyncAndWait:^{ RLMAssertThrowsWithReason(obj.intCol, @"incorrect thread"); }];
  868. }
  869. - (void)testIsDeleted {
  870. StringObject *obj1 = [[StringObject alloc] initWithValue:@[@"a"]];
  871. XCTAssertEqual(obj1.invalidated, NO);
  872. RLMRealm *realm = [self realmWithTestPath];
  873. [realm beginWriteTransaction];
  874. [realm addObject:obj1];
  875. StringObject *obj2 = [StringObject createInRealm:realm withValue:@[@"b"]];
  876. XCTAssertEqual([obj1 isInvalidated], NO);
  877. XCTAssertEqual(obj2.invalidated, NO);
  878. [realm commitWriteTransaction];
  879. // delete
  880. [realm beginWriteTransaction];
  881. // Delete directly
  882. [realm deleteObject:obj1];
  883. // Delete as result of query since then obj2's realm could point to a different instance
  884. [realm deleteObject:[[StringObject allObjectsInRealm:realm] firstObject]];
  885. XCTAssertEqual(obj1.invalidated, YES);
  886. XCTAssertEqual(obj2.invalidated, YES);
  887. RLMAssertThrowsWithReason([realm addObject:obj1], @"deleted or invalidated");
  888. NSArray *propObject = @[@"", @[obj2], @[]];
  889. RLMAssertThrowsWithReason([ArrayPropertyObject createInRealm:realm withValue:propObject],
  890. @"deleted or invalidated");
  891. [realm commitWriteTransaction];
  892. XCTAssertEqual(obj1.invalidated, YES);
  893. XCTAssertNil(obj1.realm, @"Realm should be nil after deletion");
  894. }
  895. - (void)testPrimaryKey {
  896. [[RLMRealm defaultRealm] beginWriteTransaction];
  897. [PrimaryStringObject createInDefaultRealmWithValue:(@[@"string", @1])];
  898. [PrimaryStringObject createInDefaultRealmWithValue:(@[@"string2", @1])];
  899. RLMAssertThrowsWithReason([PrimaryStringObject createInDefaultRealmWithValue:(@[@"string", @1])],
  900. @"existing primary key value");
  901. [PrimaryIntObject createInDefaultRealmWithValue:(@[@1])];
  902. [PrimaryIntObject createInDefaultRealmWithValue:(@{@"intCol": @2})];
  903. RLMAssertThrowsWithReason([PrimaryIntObject createInDefaultRealmWithValue:(@[@1])],
  904. @"existing primary key value");
  905. [PrimaryInt64Object createInDefaultRealmWithValue:(@[@(1LL << 40)])];
  906. [PrimaryInt64Object createInDefaultRealmWithValue:(@[@(1LL << 41)])];
  907. RLMAssertThrowsWithReason([PrimaryInt64Object createInDefaultRealmWithValue:(@[@(1LL << 40)])],
  908. @"existing primary key value");
  909. [PrimaryNullableIntObject createInDefaultRealmWithValue:@[@1]];
  910. [PrimaryNullableIntObject createInDefaultRealmWithValue:(@{@"optIntCol": @2, @"value": @0})];
  911. [PrimaryNullableIntObject createInDefaultRealmWithValue:@[NSNull.null]];
  912. RLMAssertThrowsWithReason([PrimaryNullableIntObject createInDefaultRealmWithValue:(@[@1, @0])],
  913. @"existing primary key value");
  914. RLMAssertThrowsWithReason([PrimaryNullableIntObject createInDefaultRealmWithValue:(@[NSNull.null, @0])],
  915. @"existing primary key value");
  916. [[RLMRealm defaultRealm] commitWriteTransaction];
  917. }
  918. - (void)testCreateOrUpdate {
  919. RLMRealm *realm = [RLMRealm defaultRealm];
  920. [realm beginWriteTransaction];
  921. [PrimaryNullableStringObject createOrUpdateInDefaultRealmWithValue:@[@"string", @1]];
  922. RLMResults *objects = [PrimaryNullableStringObject allObjects];
  923. XCTAssertEqual([objects count], 1U, @"Should have 1 object");
  924. XCTAssertEqual([(PrimaryNullableStringObject *)objects[0] intCol], 1, @"Value should be 1");
  925. [PrimaryNullableStringObject createOrUpdateInRealm:realm withValue:@{@"stringCol": @"string2", @"intCol": @2}];
  926. XCTAssertEqual([objects count], 2U, @"Should have 2 objects");
  927. [PrimaryNullableStringObject createOrUpdateInRealm:realm withValue:@{@"intCol": @5}];
  928. [PrimaryNullableStringObject createOrUpdateInRealm:realm withValue:@{@"intCol": @7}];
  929. XCTAssertEqual([PrimaryNullableStringObject objectInRealm:realm forPrimaryKey:NSNull.null].intCol, 7);
  930. [PrimaryNullableStringObject createOrUpdateInRealm:realm withValue:@{@"stringCol": NSNull.null, @"intCol": @11}];
  931. XCTAssertEqual([PrimaryNullableStringObject objectInRealm:realm forPrimaryKey:nil].intCol, 11);
  932. // upsert with new secondary property
  933. [PrimaryNullableStringObject createOrUpdateInDefaultRealmWithValue:@[@"string", @3]];
  934. XCTAssertEqual([objects count], 3U, @"Should have 3 objects");
  935. XCTAssertEqual([(PrimaryNullableStringObject *)objects[0] intCol], 3, @"Value should be 3");
  936. [realm commitWriteTransaction];
  937. }
  938. - (void)testCreateOrUpdateNestedObjects {
  939. RLMRealm *realm = [RLMRealm defaultRealm];
  940. [realm beginWriteTransaction];
  941. [PrimaryNestedObject createOrUpdateInDefaultRealmWithValue:@[@0, @[@"string", @1], @[@[@"string", @1]], @[@"string"], @[@[@1]], @""]];
  942. XCTAssertEqual([[PrimaryNestedObject allObjects] count], 1U, @"Should have 1 object");
  943. XCTAssertEqual([[PrimaryStringObject allObjects] count], 1U, @"Should have 1 object");
  944. XCTAssertEqual([[PrimaryIntObject allObjects] count], 1U, @"Should have 1 object");
  945. // update parent and nested object
  946. [PrimaryNestedObject createOrUpdateInDefaultRealmWithValue:@{@"primaryCol": @0,
  947. @"primaryStringObject": @[@"string", @2],
  948. @"primaryStringObjectWrapper": @[@[@"string", @2]],
  949. @"stringObject": @[@"string2"]}];
  950. XCTAssertEqual([[PrimaryNestedObject allObjects] count], 1U, @"Should have 1 object");
  951. XCTAssertEqual([[PrimaryStringObject allObjects] count], 1U, @"Should have 1 object");
  952. XCTAssertEqual([PrimaryStringObject.allObjects.lastObject intCol], 2, @"intCol should be 2");
  953. XCTAssertEqualObjects([PrimaryNestedObject.allObjects.lastObject stringCol], @"", @"stringCol should not have been updated");
  954. XCTAssertEqual(1U, [PrimaryNestedObject.allObjects.lastObject primaryIntArray].count, @"intArray should not have been overwritten");
  955. XCTAssertEqual([[StringObject allObjects] count], 2U, @"Should have 2 objects");
  956. // test partial update nulling out object/array properties
  957. [PrimaryNestedObject createOrUpdateInDefaultRealmWithValue:@{@"primaryCol": @0,
  958. @"stringCol": @"updated",
  959. @"stringObject": NSNull.null,
  960. @"primaryIntArray": NSNull.null}];
  961. PrimaryNestedObject *obj = PrimaryNestedObject.allObjects.lastObject;
  962. XCTAssertEqual(2, obj.primaryStringObject.intCol, @"primaryStringObject should not have changed");
  963. XCTAssertEqualObjects(obj.stringCol, @"updated", @"stringCol should have been updated");
  964. XCTAssertEqual(0U, obj.primaryIntArray.count, @"intArray should not have been emptied");
  965. XCTAssertNil(obj.stringObject, @"stringObject should be nil");
  966. // inserting new object should update nested
  967. obj = [PrimaryNestedObject createOrUpdateInDefaultRealmWithValue:@[@1, @[@"string", @3], @[@[@"string", @3]], @[@"string"], @[], @""]];
  968. XCTAssertEqual([[PrimaryNestedObject allObjects] count], 2U, @"Should have 2 objects");
  969. XCTAssertEqual([[PrimaryStringObject allObjects] count], 1U, @"Should have 1 object");
  970. XCTAssertEqual([(PrimaryStringObject *)[[PrimaryStringObject allObjects] lastObject] intCol], 3, @"intCol should be 3");
  971. // test addOrUpdateObject
  972. obj.primaryStringObject = [PrimaryStringObject createInDefaultRealmWithValue:@[@"string2", @1]];
  973. PrimaryNestedObject *obj1 = [[PrimaryNestedObject alloc] initWithValue:@[@1, @[@"string2", @4], @[@[@"string2", @4]], @[@"string"], @[@[@1], @[@2]], @""]];
  974. [realm addOrUpdateObject:obj1];
  975. XCTAssertEqual([[PrimaryNestedObject allObjects] count], 2U, @"Should have 2 objects");
  976. XCTAssertEqual([[PrimaryStringObject allObjects] count], 2U, @"Should have 2 objects");
  977. XCTAssertEqual([[PrimaryIntObject allObjects] count], 2U, @"Should have 2 objects");
  978. XCTAssertEqual([(PrimaryStringObject *)[[PrimaryStringObject allObjects] lastObject] intCol], 4, @"intCol should be 4");
  979. [realm commitWriteTransaction];
  980. }
  981. - (void)testCreateOrUpdateWithReorderedColumns {
  982. @autoreleasepool {
  983. // Create a Realm file with the properties in reverse order
  984. RLMObjectSchema *objectSchema = [RLMObjectSchema schemaForObjectClass:PrimaryStringObject.class];
  985. objectSchema.properties = @[objectSchema.properties[1], objectSchema.properties[0]];
  986. RLMSchema *schema = [RLMSchema new];
  987. schema.objectSchema = @[objectSchema];
  988. RLMRealm *realm = [self realmWithTestPathAndSchema:schema];
  989. [realm beginWriteTransaction];
  990. [PrimaryStringObject createOrUpdateInRealm:realm withValue:@[@5, @"a"]];
  991. [realm commitWriteTransaction];
  992. }
  993. RLMRealm *realm = [self realmWithTestPath];
  994. [realm beginWriteTransaction];
  995. XCTAssertEqual([PrimaryStringObject objectInRealm:realm forPrimaryKey:@"a"].intCol, 5);
  996. // Values in array are used in property declaration order, not table column order
  997. [PrimaryStringObject createOrUpdateInRealm:realm withValue:@[@"a", @6]];
  998. XCTAssertEqual([PrimaryStringObject objectInRealm:realm forPrimaryKey:@"a"].intCol, 6);
  999. [PrimaryStringObject createOrUpdateInRealm:realm withValue:@{@"stringCol": @"a", @"intCol": @7}];
  1000. XCTAssertEqual([PrimaryStringObject objectInRealm:realm forPrimaryKey:@"a"].intCol, 7);
  1001. [realm commitWriteTransaction];
  1002. }
  1003. - (void)testObjectInSet {
  1004. [[RLMRealm defaultRealm] beginWriteTransaction];
  1005. // set object with primary and non primary keys as they both override isEqual and hash
  1006. PrimaryStringObject *obj = [PrimaryStringObject createInDefaultRealmWithValue:(@[@"string2", @1])];
  1007. StringObject *strObj = [StringObject createInDefaultRealmWithValue:@[@"string"]];
  1008. NSMutableSet *dict = [NSMutableSet set];
  1009. [dict addObject:obj];
  1010. [dict addObject:strObj];
  1011. // primary key objects should match even with duplicate instances of the same object
  1012. XCTAssertTrue([dict containsObject:obj]);
  1013. XCTAssertTrue([dict containsObject:[[PrimaryStringObject allObjects] firstObject]]);
  1014. // non-primary key objects should only match when comparing identical instances
  1015. XCTAssertTrue([dict containsObject:strObj]);
  1016. XCTAssertFalse([dict containsObject:[[StringObject allObjects] firstObject]]);
  1017. [[RLMRealm defaultRealm] commitWriteTransaction];
  1018. }
  1019. - (void)testObjectForKey {
  1020. [RLMRealm.defaultRealm beginWriteTransaction];
  1021. PrimaryStringObject *strObj = [PrimaryStringObject createInDefaultRealmWithValue:@[@"key", @0]];
  1022. PrimaryNullableStringObject *nullStrObj = [PrimaryNullableStringObject createInDefaultRealmWithValue:@[NSNull.null, @0]];
  1023. PrimaryIntObject *intObj = [PrimaryIntObject createInDefaultRealmWithValue:@[@0]];
  1024. PrimaryNullableIntObject *nonNullIntObj = [PrimaryNullableIntObject createInDefaultRealmWithValue:@[@0]];
  1025. PrimaryNullableIntObject *nullIntObj = [PrimaryNullableIntObject createInDefaultRealmWithValue:@[NSNull.null]];
  1026. [RLMRealm.defaultRealm commitWriteTransaction];
  1027. // no PK
  1028. RLMAssertThrowsWithReason([StringObject objectForPrimaryKey:@""],
  1029. @"does not have a primary key");
  1030. RLMAssertThrowsWithReason([IntObject objectForPrimaryKey:@0],
  1031. @"does not have a primary key");
  1032. RLMAssertThrowsWithReason([StringObject objectForPrimaryKey:NSNull.null],
  1033. @"does not have a primary key");
  1034. RLMAssertThrowsWithReason([StringObject objectForPrimaryKey:nil],
  1035. @"does not have a primary key");
  1036. RLMAssertThrowsWithReason([IntObject objectForPrimaryKey:nil],
  1037. @"does not have a primary key");
  1038. // wrong PK type
  1039. RLMAssertThrowsWithReasonMatching([PrimaryStringObject objectForPrimaryKey:@0],
  1040. @"Invalid value '0' of type '.*Number.*' for 'string' property 'PrimaryStringObject.stringCol'.");
  1041. RLMAssertThrowsWithReasonMatching([PrimaryStringObject objectForPrimaryKey:@[]],
  1042. @"of type '.*Array.*' for 'string' property 'PrimaryStringObject.stringCol'.");
  1043. RLMAssertThrowsWithReasonMatching([PrimaryIntObject objectForPrimaryKey:@""],
  1044. @"Invalid value '' of type '.*String.*' for 'int' property 'PrimaryIntObject.intCol'.");
  1045. RLMAssertThrowsWithReason([PrimaryIntObject objectForPrimaryKey:NSNull.null],
  1046. @"Invalid value '<null>' of type 'NSNull' for 'int' property 'PrimaryIntObject.intCol'.");
  1047. RLMAssertThrowsWithReason([PrimaryIntObject objectForPrimaryKey:nil],
  1048. @"Invalid value '(null)' of type '(null)' for 'int' property 'PrimaryIntObject.intCol'.");
  1049. RLMAssertThrowsWithReason([PrimaryStringObject objectForPrimaryKey:NSNull.null],
  1050. @"Invalid value '<null>' of type 'NSNull' for 'string' property 'PrimaryStringObject.stringCol'.");
  1051. RLMAssertThrowsWithReason([PrimaryStringObject objectForPrimaryKey:nil],
  1052. @"Invalid value '(null)' of type '(null)' for 'string' property 'PrimaryStringObject.stringCol'.");
  1053. // no object with key
  1054. XCTAssertNil([PrimaryStringObject objectForPrimaryKey:@"bad key"]);
  1055. XCTAssertNil([PrimaryIntObject objectForPrimaryKey:@1]);
  1056. // object with key exists
  1057. XCTAssertEqualObjects(strObj, [PrimaryStringObject objectForPrimaryKey:@"key"]);
  1058. XCTAssertEqualObjects(nullStrObj, [PrimaryNullableStringObject objectForPrimaryKey:NSNull.null]);
  1059. XCTAssertEqualObjects(nullStrObj, [PrimaryNullableStringObject objectForPrimaryKey:nil]);
  1060. XCTAssertEqualObjects(intObj, [PrimaryIntObject objectForPrimaryKey:@0]);
  1061. XCTAssertEqualObjects(nonNullIntObj, [PrimaryNullableIntObject objectForPrimaryKey:@0]);
  1062. XCTAssertEqualObjects(nullIntObj, [PrimaryNullableIntObject objectForPrimaryKey:NSNull.null]);
  1063. XCTAssertEqualObjects(nullIntObj, [PrimaryNullableIntObject objectForPrimaryKey:nil]);
  1064. // nil realm throws
  1065. RLMAssertThrowsWithReason([PrimaryIntObject objectInRealm:self.nonLiteralNil forPrimaryKey:@0],
  1066. @"Realm must not be nil");
  1067. }
  1068. - (void)testClassExtension {
  1069. RLMRealm *realm = [RLMRealm defaultRealm];
  1070. [realm beginWriteTransaction];
  1071. BaseClassStringObject *bObject = [[BaseClassStringObject alloc ] init];
  1072. bObject.intCol = 1;
  1073. bObject.stringCol = @"stringVal";
  1074. [realm addObject:bObject];
  1075. [realm commitWriteTransaction];
  1076. BaseClassStringObject *objectFromRealm = [BaseClassStringObject allObjects][0];
  1077. XCTAssertEqual(1, objectFromRealm.intCol);
  1078. XCTAssertEqualObjects(@"stringVal", objectFromRealm.stringCol);
  1079. }
  1080. @end