Files
RestKit/Tests/Logic/ObjectMapping/RKObjectMappingTest.m
Blake Watters 8dc54a89b2 Major overhaul to the Core Data managed object identification and relationship connection support.
* Replaces primary key with `RKEntityIdentifier`
* Add support for use of compound keys for object identification
* Refactor `RKConnectionMapping` to `RKConnectionDescription` and add support for connecting with multiple attributes
* Clarify naming of representation key methods to better match naming conventions
* Add type transformation support for object identification
* Greatly expand test coverage for object identification
* Drop the `NSEntityDescription` category
* Simplify the `RKManagedObjectCaching` protocol
* Add compound key support to the Fetch Request and In Memory Cache implementations
* Replace Kiwi with Specta for tests where contexts are helpful for organization
* Rename `defaultValueForMissingAttribute` to `defaultValueForAttribute`
2012-11-27 10:29:36 -05:00

223 lines
11 KiB
Objective-C

//
// RKObjectMappingTest.m
// RestKit
//
// Created by Blake Watters on 6/8/12.
// Copyright (c) 2012 RestKit. All rights reserved.
//
#import "RKTestEnvironment.h"
@interface RKObjectMappingTest : RKTestCase
@end
@implementation RKObjectMappingTest
- (void)tearDown
{
[RKObjectMapping setDefaultSourceToDestinationKeyTransformationBlock:nil];
}
- (void)testThatTwoMappingsWithTheSameAttributeMappingsButDifferentObjectClassesAreNotConsideredEqual
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSNumber class]];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(NO)));
}
- (void)testThatTwoMappingsForNilObjectClassAreConsideredEqual
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:nil];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:nil];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(YES)));
}
- (void)testThatTwoMappingsAreNotConsideredEqualIfOneHasNilObjectClass
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:nil];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(NO)));
}
- (void)testThatRelationshipMappingsWithTheSameObjectClassAndNoAttributesAreConsideredEqual
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(YES)));
}
- (void)testThatTwoMappingsForTheSameObjectClassContainingIdenticalAttributeMappingsAreConsideredEqual
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping1 addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"]];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping2 addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"]];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(YES)));
}
- (void)testThatTwoMappingsForTheSameObjectClassContainingDifferingAttributeMappingsAreNotConsideredEqual
{
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping1 addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"]];
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping2 addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"different" toKeyPath:@"that"]];
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(NO)));
}
- (void)testThatTwoMappingsWithEqualRelationshipMappingsAreConsideredEqual
{
RKObjectMapping *relationshipMapping1 = [RKObjectMapping mappingForClass:[NSSet class]];
RKObjectMapping *relationshipMapping2 = [RKObjectMapping mappingForClass:[NSSet class]];
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping1 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"that" withMapping:relationshipMapping1]];;
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping2 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"that" withMapping:relationshipMapping2]];;
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(YES)));
}
- (void)testThatTwoMappingsWithDifferingRelationshipMappingClassesAreNotConsideredEqual
{
RKObjectMapping *relationshipMapping1 = [RKObjectMapping mappingForClass:[NSSet class]];
RKObjectMapping *relationshipMapping2 = [RKObjectMapping mappingForClass:[NSNumber class]];
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping1 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"that" withMapping:relationshipMapping1]];;
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping2 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"that" withMapping:relationshipMapping2]];;
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(NO)));
}
- (void)testThatTwoMappingsWithDifferingRelationshipMappingKeyPathsAreNotConsideredEqual
{
RKObjectMapping *relationshipMapping1 = [RKObjectMapping mappingForClass:[NSSet class]];
RKObjectMapping *relationshipMapping2 = [RKObjectMapping mappingForClass:[NSSet class]];
RKObjectMapping *mapping1 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping1 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"that" withMapping:relationshipMapping1]];;
RKObjectMapping *mapping2 = [RKObjectMapping mappingForClass:[NSString class]];
[mapping2 addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"this" toKeyPath:@"different" withMapping:relationshipMapping2]];;
assertThatBool([mapping1 isEqualToMapping:mapping2], is(equalToBool(NO)));
}
- (void)testThatAddingAPropertyMappingThatExistsInAnotherMappingTriggersException
{
RKObjectMapping *firstMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"];
[firstMapping addPropertyMapping:attributeMapping];
RKObjectMapping *secondMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
NSException *exception = nil;
@try {
[secondMapping addPropertyMapping:attributeMapping];
}
@catch (NSException *caughtException) {
exception = caughtException;
}
@finally {
expect(exception).notTo.beNil();
expect(exception.reason).to.equal(@"Cannot add a property mapping object that has already been added to another `RKObjectMapping` object. You probably want to obtain a copy of the mapping: `[propertyMapping copy]`");
}
}
- (void)testThatAddingAnArrayOfPropertyMappingsThatExistInAnotherMappingTriggersException
{
RKObjectMapping *firstMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"];
[firstMapping addPropertyMapping:attributeMapping];
RKObjectMapping *secondMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
NSException *exception = nil;
@try {
[secondMapping addPropertyMappingsFromArray:@[attributeMapping]];
}
@catch (NSException *caughtException) {
exception = caughtException;
}
@finally {
expect(exception).notTo.beNil();
expect(exception.reason).to.equal(@"One or more of the property mappings in the given array has already been added to another `RKObjectMapping` object. You probably want to obtain a copy of the array of mappings: `[[NSArray alloc] initWithArray:arrayOfPropertyMappings copyItems:YES]`");
}
}
- (void)testThatAddingAnArrayOfAttributeMappingsThatExistInAnotherMappingTriggersException
{
RKObjectMapping *firstMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"this" toKeyPath:@"that"];
[firstMapping addPropertyMapping:attributeMapping];
RKObjectMapping *secondMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
NSException *exception = nil;
@try {
[secondMapping addAttributeMappingsFromArray:@[attributeMapping, @"stringValue"]];
}
@catch (NSException *caughtException) {
exception = caughtException;
}
@finally {
expect(exception).notTo.beNil();
expect(exception.reason).to.equal(@"One or more of the property mappings in the given array has already been added to another `RKObjectMapping` object. You probably want to obtain a copy of the array of mappings: `[[NSArray alloc] initWithArray:arrayOfPropertyMappings copyItems:YES]`");
}
}
#pragma mark - Key Transformations
- (void)testPropertyNameTransformationBlockForAttributes
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping setSourceToDestinationKeyTransformationBlock:^NSString *(RKObjectMapping *mapping, NSString *sourceKey) {
return [sourceKey uppercaseString];
}];
[mapping addAttributeMappingsFromArray:@[ @"name", @"rank" ]];
NSArray *expectedNames = @[ @"NAME", @"RANK" ];
expect([mapping.propertyMappingsByDestinationKeyPath allKeys]).to.equal(expectedNames);
}
- (void)testPropertyNameTransformationBlockForRelationships
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping setSourceToDestinationKeyTransformationBlock:^NSString *(RKObjectMapping *mapping, NSString *sourceKey) {
return [sourceKey uppercaseString];
}];
RKObjectMapping *relatedMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addRelationshipMappingWithSourceKeyPath:@"something" mapping:relatedMapping];
RKRelationshipMapping *relationshipMapping = [mapping propertyMappingsByDestinationKeyPath][@"SOMETHING"];
expect(relationshipMapping).notTo.beNil();
expect(relationshipMapping.sourceKeyPath).to.equal(@"something");
expect(relationshipMapping.destinationKeyPath).to.equal(@"SOMETHING");
}
- (void)testTransformationOfAttributeKeyPaths
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping setSourceToDestinationKeyTransformationBlock:^NSString *(RKObjectMapping *mapping, NSString *sourceKey) {
return [sourceKey capitalizedString];
}];
[mapping addAttributeMappingsFromArray:@[ @"user.comments" ]];
NSArray *expectedNames = @[ @"User.Comments" ];
expect([mapping.propertyMappingsByDestinationKeyPath allKeys]).to.equal(expectedNames);
}
- (void)testDefaultSourceToDestinationKeyTransformationBlock
{
[RKObjectMapping setDefaultSourceToDestinationKeyTransformationBlock:^NSString *(RKObjectMapping *mapping, NSString *sourceKey) {
return [sourceKey capitalizedString];
}];
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
[mapping addAttributeMappingsFromArray:@[ @"user.comments" ]];
NSArray *expectedNames = @[ @"User.Comments" ];
expect([mapping.propertyMappingsByDestinationKeyPath allKeys]).to.equal(expectedNames);
}
@end