// // RKMappingTestTest.m // RestKit // // Created by Blake Watters on 1/3/13. // Copyright (c) 2013 RestKit. All rights reserved. // #import "RKTestEnvironment.h" #import "RKMappingTest.h" #import "RKTestUser.h" #import "RKHuman.h" #import "RKCat.h" @interface RKMappingTestTest : SenTestCase @property (nonatomic, strong) id objectRepresentation; @property (nonatomic, strong) RKMappingTest *mappingTest; @end @implementation RKMappingTestTest - (void)setUp { self.objectRepresentation = [RKTestFixture parsedObjectWithContentsOfFixture:@"user.json"]; RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[RKTestUser class]]; [mapping addAttributeMappingsFromDictionary:@{ @"id": @"userID", @"name": @"name", @"birthdate": @"birthDate", @"created_at": @"createdAt" }]; RKObjectMapping *addressMapping = [RKObjectMapping mappingForClass:[RKTestAddress class]]; [addressMapping addAttributeMappingsFromDictionary:@{ @"id": @"addressID"}]; [addressMapping addAttributeMappingsFromArray:@[ @"city", @"state", @"country" ]]; [mapping addRelationshipMappingWithSourceKeyPath:@"address" mapping:addressMapping]; RKObjectMapping *coordinateMapping = [RKObjectMapping mappingForClass:[RKTestCoordinate class]]; [coordinateMapping addAttributeMappingsFromArray:@[ @"latitude", @"longitude" ]]; [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"coordinate" withMapping:coordinateMapping]]; self.mappingTest = [[RKMappingTest alloc] initWithMapping:mapping sourceObject:self.objectRepresentation destinationObject:nil]; } - (void)testMappingTestForAttribute { [self.mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"name" destinationKeyPath:@"name" value:@"Blake Watters"]]; expect([self.mappingTest evaluate]).to.equal(YES); } - (void)testMappingTestFailureForAttribute { [self.mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"name" destinationKeyPath:@"name" value:@"Invalid"]]; expect([self.mappingTest evaluate]).to.equal(NO); } - (void)testMappingTestForAttributeWithBlock { [self.mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"name" destinationKeyPath:@"name" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) { return [mappedValue isEqualToString:@"Blake Watters"]; }]]; expect([self.mappingTest evaluate]).to.equal(YES); } - (void)testMappingTestForRelationship { RKTestCoordinate *coordinate = [RKTestCoordinate new]; coordinate.latitude = 12345; coordinate.longitude = 56789; [self.mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:nil destinationKeyPath:@"coordinate" value:coordinate]]; expect([self.mappingTest evaluate]).to.equal(NO); } - (void)testMappingTestForRelationshipWithBlock { [self.mappingTest addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"address" destinationKeyPath:@"address" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) { RKTestAddress *address = (RKTestAddress *)mappedValue; return [address.addressID isEqualToNumber:@(1234)] && [address.city isEqualToString:@"Carrboro"] && [address.state isEqualToString:@"North Carolina"] && [address.country isEqualToString:@"USA"]; return YES; }]]; expect([self.mappingTest evaluate]).to.equal(YES); } - (void)testEvaluateExpectationReturnsUnsatisfiedExpectationErrorForUnmappedKeyPath { NSError *error = nil; BOOL success = [self.mappingTest evaluateExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"nonexistant" destinationKeyPath:@"name" value:@"Invalid"] error:&error]; expect(success).to.equal(NO); expect(error).notTo.beNil(); expect(error.code).to.equal(RKMappingTestUnsatisfiedExpectationError); expect([error localizedDescription]).to.equal(@"expected to map 'nonexistant' to 'name', but did not."); } - (void)testEvaluateExpectationReturnsValueInequalityErrorErrorForValueMismatch { NSError *error = nil; BOOL success = [self.mappingTest evaluateExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"name" destinationKeyPath:@"name" value:@"Incorrect"] error:&error]; expect(success).to.equal(NO); expect(error).notTo.beNil(); expect(error.code).to.equal(RKMappingTestValueInequalityError); expect([error localizedDescription]).to.equal(@"mapped to unexpected __NSCFString value 'Blake Watters'"); } - (void)testEvaluateExpectationReturnsEvaluationBlockErrorForBlockFailure { NSError *error = nil; BOOL success = [self.mappingTest evaluateExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"address" destinationKeyPath:@"address" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) { return NO; }] error:&error]; expect(success).to.equal(NO); expect(error).notTo.beNil(); expect(error.code).to.equal(RKMappingTestEvaluationBlockError); assertThat([error localizedDescription], startsWith(@"evaluation block returned `NO` for RKTestAddress value '