mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-29 05:05:34 +08:00
Add delegate methods to RKObjectMappingOperationDelegate for tracking mappable content that is not
set due to an unchanged value. Update RKMapperTest to correctly validate mappings that do not result in a value change (typically nil => nil). closes #72, fixes #634
This commit is contained in:
@@ -1131,6 +1131,44 @@
|
||||
[mockUser verify];
|
||||
}
|
||||
|
||||
- (void)testDelegateIsInformedWhenANilValueIsMappedForNSNullWithExistingValue {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKObjectAttributeMapping* nameMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"name" toKeyPath:@"name"];
|
||||
[mapping addAttributeMapping:nameMapping];
|
||||
|
||||
NSDictionary* dictionary = [[RKTestFixture parsedObjectWithContentsOfFixture:@"user.json"] mutableCopy];
|
||||
[dictionary setValue:[NSNull null] forKey:@"name"];
|
||||
RKTestUser* user = [RKTestUser user];
|
||||
user.name = @"Blake Watters";
|
||||
id mockDelegate = [OCMockObject mockForProtocol:@protocol(RKObjectMappingOperationDelegate)];
|
||||
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user mapping:mapping];
|
||||
operation.delegate = mockDelegate;
|
||||
NSError* error = nil;
|
||||
[[mockDelegate expect] objectMappingOperation:operation didFindMapping:nameMapping forKeyPath:@"name"];
|
||||
[[mockDelegate expect] objectMappingOperation:operation didSetValue:nil forKeyPath:@"name" usingMapping:nameMapping];
|
||||
[operation performMapping:&error];
|
||||
[mockDelegate verify];
|
||||
}
|
||||
|
||||
- (void)testDelegateIsInformedWhenUnchangedValueIsSkipped {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKObjectAttributeMapping* nameMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"name" toKeyPath:@"name"];
|
||||
[mapping addAttributeMapping:nameMapping];
|
||||
|
||||
NSDictionary* dictionary = [[RKTestFixture parsedObjectWithContentsOfFixture:@"user.json"] mutableCopy];
|
||||
[dictionary setValue:@"Blake Watters" forKey:@"name"];
|
||||
RKTestUser* user = [RKTestUser user];
|
||||
user.name = @"Blake Watters";
|
||||
id mockDelegate = [OCMockObject mockForProtocol:@protocol(RKObjectMappingOperationDelegate)];
|
||||
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user mapping:mapping];
|
||||
operation.delegate = mockDelegate;
|
||||
NSError* error = nil;
|
||||
[[mockDelegate expect] objectMappingOperation:operation didFindMapping:nameMapping forKeyPath:@"name"];
|
||||
[[mockDelegate expect] objectMappingOperation:operation didNotSetUnchangedValue:@"Blake Watters" forKeyPath:@"name" usingMapping:nameMapping];
|
||||
[operation performMapping:&error];
|
||||
[mockDelegate verify];
|
||||
}
|
||||
|
||||
- (void)testShouldOptionallySetDefaultValueForAMissingKeyPath {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKObjectAttributeMapping* nameMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"name" toKeyPath:@"name"];
|
||||
@@ -1327,6 +1365,33 @@
|
||||
[mapper release];
|
||||
}
|
||||
|
||||
- (void)testSkippingOfIdenticalObjectsInformsDelegate {
|
||||
RKTestUser* user = [RKTestUser user];
|
||||
RKTestAddress* address = [RKTestAddress address];
|
||||
address.addressID = [NSNumber numberWithInt:1234];
|
||||
user.address = address;
|
||||
id mockUser = [OCMockObject partialMockForObject:user];
|
||||
[[mockUser reject] setAddress:OCMOCK_ANY];
|
||||
|
||||
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKObjectAttributeMapping* nameMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"name" toKeyPath:@"name"];
|
||||
[userMapping addAttributeMapping:nameMapping];
|
||||
RKObjectMapping* addressMapping = [RKObjectMapping mappingForClass:[RKTestAddress class]];
|
||||
RKObjectAttributeMapping* idMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"id" toKeyPath:@"addressID"];
|
||||
[addressMapping addAttributeMapping:idMapping];
|
||||
|
||||
RKObjectRelationshipMapping* hasOneMapping = [RKObjectRelationshipMapping mappingFromKeyPath:@"address" toKeyPath:@"address" withMapping:addressMapping];
|
||||
[userMapping addRelationshipMapping:hasOneMapping];
|
||||
|
||||
id userInfo = [RKTestFixture parsedObjectWithContentsOfFixture:@"user.json"];
|
||||
RKObjectMappingOperation *operation = [RKObjectMappingOperation mappingOperationFromObject:userInfo toObject:user withMapping:userMapping];
|
||||
id mockDelegate = [OCMockObject niceMockForProtocol:@protocol(RKObjectMappingOperationDelegate)];
|
||||
[[mockDelegate expect] objectMappingOperation:operation didNotSetUnchangedValue:address forKeyPath:@"address" usingMapping:hasOneMapping];
|
||||
operation.delegate = mockDelegate;
|
||||
[operation performMapping:nil];
|
||||
[mockDelegate verify];
|
||||
}
|
||||
|
||||
- (void)testShouldNotSetThePropertyWhenTheNestedObjectCollectionIsIdentical {
|
||||
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
|
||||
RKObjectAttributeMapping* idMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"id" toKeyPath:@"userID"];
|
||||
|
||||
Reference in New Issue
Block a user