From 71476e955b39e724e1f41ecc8251a2c538fe568d Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Mon, 8 May 2017 11:36:14 -0700 Subject: [PATCH] Add a test for setting properties to nil with createOrUpdate: --- Realm/Tests/ObjectCreationTests.mm | 62 ++++++++++++++++++++++++++++++ Realm/Tests/RLMTestObjects.h | 13 +++++++ Realm/Tests/RLMTestObjects.m | 9 +++++ 3 files changed, 84 insertions(+) diff --git a/Realm/Tests/ObjectCreationTests.mm b/Realm/Tests/ObjectCreationTests.mm index a08acdd08b..e9c9039ec4 100644 --- a/Realm/Tests/ObjectCreationTests.mm +++ b/Realm/Tests/ObjectCreationTests.mm @@ -902,6 +902,68 @@ - (void)testCreateOrUpdateOnManagedObjectInDifferentRealmDeepCopies { [realm2 cancelWriteTransaction]; } +- (void)testCreateOrUpdateWithNilValues { + auto now = [NSDate date]; + auto bytes = [NSData dataWithBytes:"a" length:1]; + auto nonnull = [[AllOptionalTypesPK alloc] initWithValue:@[@0, @1, @2.2f, @3.3, @YES, @"a", bytes, now]]; + auto null = [[AllOptionalTypesPK alloc] initWithValue:@[@0]]; + + auto realm = [RLMRealm defaultRealm]; + [realm beginWriteTransaction]; + + auto obj = [AllOptionalTypesPK createInRealm:realm withValue:nonnull]; + [AllOptionalTypesPK createOrUpdateInRealm:realm withValue:null]; + + // FIXME: these should actually all be nil + XCTAssertNotNil(obj.intObj); + XCTAssertNotNil(obj.floatObj); + XCTAssertNotNil(obj.doubleObj); + XCTAssertNotNil(obj.boolObj); + XCTAssertNotNil(obj.string); + XCTAssertNotNil(obj.data); + XCTAssertNotNil(obj.date); + + [AllOptionalTypesPK createOrUpdateInRealm:realm withValue:nonnull]; + [AllOptionalTypesPK createOrUpdateInRealm:realm withValue:@[@0]]; + + // No values specified, so old values should remain + XCTAssertNotNil(obj.intObj); + XCTAssertNotNil(obj.floatObj); + XCTAssertNotNil(obj.doubleObj); + XCTAssertNotNil(obj.boolObj); + XCTAssertNotNil(obj.string); + XCTAssertNotNil(obj.data); + XCTAssertNotNil(obj.date); + + [AllOptionalTypesPK createOrUpdateInRealm:realm withValue:@{@"pk": @0}]; + XCTAssertNotNil(obj.intObj); + XCTAssertNotNil(obj.floatObj); + XCTAssertNotNil(obj.doubleObj); + XCTAssertNotNil(obj.boolObj); + XCTAssertNotNil(obj.string); + XCTAssertNotNil(obj.data); + XCTAssertNotNil(obj.date); + + [AllOptionalTypesPK createOrUpdateInRealm:realm withValue:@{@"pk": @0, + @"intObj": NSNull.null, + @"floatObj": NSNull.null, + @"doubleObj": NSNull.null, + @"boolObj": NSNull.null, + @"string": NSNull.null, + @"data": NSNull.null, + @"date": NSNull.null, + }]; + XCTAssertNil(obj.intObj); + XCTAssertNil(obj.floatObj); + XCTAssertNil(obj.doubleObj); + XCTAssertNil(obj.boolObj); + XCTAssertNil(obj.string); + XCTAssertNil(obj.data); + XCTAssertNil(obj.date); + + [realm cancelWriteTransaction]; +} + #pragma mark - Add - (void)testAddInvalidated { diff --git a/Realm/Tests/RLMTestObjects.h b/Realm/Tests/RLMTestObjects.h index 2f574280d6..440e7f0bb5 100644 --- a/Realm/Tests/RLMTestObjects.h +++ b/Realm/Tests/RLMTestObjects.h @@ -148,6 +148,19 @@ RLM_ARRAY_TYPE(AllTypesObject) @property NSDate *date; @end +@interface AllOptionalTypesPK : RLMObject +@property int pk; + +@property NSNumber *intObj; +@property NSNumber *floatObj; +@property NSNumber *doubleObj; +@property NSNumber *boolObj; +@property NSString *string; +@property NSData *data; +@property NSDate *date; +@end + + #pragma mark - Real Life Objects #pragma mark - diff --git a/Realm/Tests/RLMTestObjects.m b/Realm/Tests/RLMTestObjects.m index a7f585851c..93e7ee70ed 100644 --- a/Realm/Tests/RLMTestObjects.m +++ b/Realm/Tests/RLMTestObjects.m @@ -97,6 +97,15 @@ @implementation LinkToAllTypesObject @implementation AllOptionalTypes @end +@implementation AllOptionalTypesPK ++ (NSString *)primaryKey { + return @"pk"; +} ++ (NSDictionary *)defaultPropertyValues { + return @{@"pk": NSUUID.UUID.UUIDString}; +} +@end + #pragma mark - Real Life Objects #pragma mark -