Permalink
Browse files

Ensure MOC isn't save when error is mapped

  • Loading branch information...
segiddins committed Jan 4, 2014
1 parent 02168bb commit e45a6a8b47b61e27feb638b369803c632115a5aa
@@ -619,17 +619,17 @@ - (void)performMappingOnResponseWithCompletionBlock:(void(^)(RKMappingResult *ma
if (! success || [weakSelf isCancelled]) {
return completionBlock(nil, error);
}
- }
-
- // Persist our mapped objects
- success = [weakSelf obtainPermanentObjectIDsForInsertedObjects:&error];
- if (! success || [weakSelf isCancelled]) {
- return completionBlock(nil, error);
- }
- success = [weakSelf saveContext:&error];
- if (! success || [weakSelf isCancelled]) {
- return completionBlock(nil, error);
+ // Persist our mapped objects
+ success = [weakSelf obtainPermanentObjectIDsForInsertedObjects:&error];
+ if (! success || [weakSelf isCancelled]) {
+ return completionBlock(nil, error);
+ }
+
+ success = [weakSelf saveContext:&error];
+ if (! success || [weakSelf isCancelled]) {
+ return completionBlock(nil, error);
+ }
}
// Refetch all managed objects nested at key paths within the results dictionary before returning
View
@@ -55,7 +55,7 @@ SPEC CHECKSUMS:
ISO8601DateFormatterValueTransformer: d0af1f50a9df42db72d7418db31938b8473e0af8
OCHamcrest: 782c52ae8c673e5d6060a65005cfd26d20d519ba
OCMock: 80e4cfbc71a0e1bd28caf10a9fda27da7c4c8402
- RestKit: dc107a0aa4f962575ad0385bac5b3c5e4f591f4e
+ RestKit: 8aedc01890f52f090ff30f60c709eab072dd14c3
RKCLLocationValueTransformer: e4a127582a78df98035f75b611e48c0545b8884d
RKValueTransformers: 1562d458b9c9d59ce777bfc5982f2e1aef2e9182
SOCKit: 2f3bc4d07910de12dcc202815e07db68a3802581
@@ -1275,6 +1275,44 @@ - (void)testThatSuccessfulCompletionSavesManagedObjectIfTargetObjectIsUnsavedEve
}];
}
+- (void)testThatManagedObjectContextIsNotSavedWhenOperationErrorIsMapped
+{
+ RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
+ RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Human" inManagedObjectStore:managedObjectStore];
+ [entityMapping addAttributeMappingsFromDictionary:@{ @"name": @"name" }];
+ RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"human" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
+
+ RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
+ [errorMapping addAttributeMappingsFromDictionary:@{ @"self": @"errorMessage" }];
+ NSMutableIndexSet *errorCodes = [[NSMutableIndexSet alloc] init];
+ [errorCodes addIndexes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
+ [errorCodes addIndexes:RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError)];
+ RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"error" statusCodes:errorCodes];
+
+ NSManagedObjectContext *scratchContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
+ id mockContext = [OCMockObject partialMockForObject:scratchContext];
+ [[mockContext reject] save:((NSError __autoreleasing **)[OCMArg anyPointer])];
+ scratchContext.parentContext = [managedObjectStore mainQueueManagedObjectContext];
+
+ RKHuman *human = [NSEntityDescription insertNewObjectForEntityForName:@"Human" inManagedObjectContext:mockContext];
+ human.name = @"Blake";
+
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/422" relativeToURL:[RKTestFactory baseURL]]];
+ request.HTTPMethod = @"POST";
+ RKManagedObjectRequestOperation *managedObjectRequestOperation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor, errorDescriptor ]];
+ managedObjectRequestOperation.managedObjectContext = mockContext;
+ managedObjectRequestOperation.targetObject = human;
+ [managedObjectRequestOperation start];
+ [managedObjectRequestOperation waitUntilFinished];
+
+ expect(scratchContext.hasChanges).to.beTruthy();
+ expect([managedObjectStore.mainQueueManagedObjectContext existingObjectWithID:human.objectID error:nil]).to.beNil();
+ expect(managedObjectRequestOperation.error).toNot.beNil();
+ expect(managedObjectRequestOperation.mappingResult.dictionary[@"human"]).to.beNil();
+ expect(scratchContext.insertedObjects).to.haveCountOf(1);
+ [mockContext verify];
+}
+
- (void)testThatMapperOperationDelegateIsPassedThroughToUnderlyingMapperOperation
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
View
@@ -296,6 +296,12 @@ def render_fixture(path, options = {})
''
end
+ post '/422' do
+ status 422
+ content_type 'application/json'
+ { :error => "Unprocessable Entity." }.to_json
+ end
+
delete '/humans/1234/whitespace' do
content_type 'application/json'
status 200

0 comments on commit e45a6a8

Please sign in to comment.