Skip to content

Commit

Permalink
Add a test for setting properties to nil with createOrUpdate:
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed May 17, 2017
1 parent 77c86dc commit 71476e9
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Realm/Tests/ObjectCreationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions Realm/Tests/RLMTestObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ RLM_ARRAY_TYPE(AllTypesObject)
@property NSDate *date;
@end

@interface AllOptionalTypesPK : RLMObject
@property int pk;

@property NSNumber<RLMInt> *intObj;
@property NSNumber<RLMFloat> *floatObj;
@property NSNumber<RLMDouble> *doubleObj;
@property NSNumber<RLMBool> *boolObj;
@property NSString *string;
@property NSData *data;
@property NSDate *date;
@end


#pragma mark - Real Life Objects
#pragma mark -

Expand Down
9 changes: 9 additions & 0 deletions Realm/Tests/RLMTestObjects.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 -

Expand Down

0 comments on commit 71476e9

Please sign in to comment.