Skip to content

Commit

Permalink
Integrate tests verifying the behavior of using an externally integra…
Browse files Browse the repository at this point in the history
…ted value transformer for mapping and object parameterization
  • Loading branch information
Blake Watters committed Sep 24, 2013
1 parent e87a917 commit cee1337
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Code/ObjectMapping/RKMappingOperation.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ - (NSArray *)relationshipMappings


- (BOOL)transformValue:(id)inputValue toValue:(__autoreleasing id *)outputValue withPropertyMapping:(RKPropertyMapping *)propertyMapping error:(NSError *__autoreleasing *)error - (BOOL)transformValue:(id)inputValue toValue:(__autoreleasing id *)outputValue withPropertyMapping:(RKPropertyMapping *)propertyMapping error:(NSError *__autoreleasing *)error
{ {
Class transformedValueClass = [self.objectMapping classForKeyPath:propertyMapping.destinationKeyPath]; Class transformedValueClass = propertyMapping.propertyValueClass ?: [self.objectMapping classForKeyPath:propertyMapping.destinationKeyPath];
if (! transformedValueClass) { if (! transformedValueClass) {
*outputValue = inputValue; *outputValue = inputValue;
return YES; return YES;
Expand Down
7 changes: 7 additions & 0 deletions Code/ObjectMapping/RKPropertyMapping.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
/// @name Specifying a Value Transformer /// @name Specifying a Value Transformer
///------------------------------------- ///-------------------------------------


/**
Specifies the class used to represent the value of the mapped property. A value of `Nil` (which is the default value) indicates the property class is to be determined by runtime introspection.
In cases where run-time type introspection cannot be performed (such as during object parameterization) you can specify the class used to represent the value of the property being mapped.
*/
@property (nonatomic, strong) Class propertyValueClass;

/** /**
A value transformer with which to process input values being mapped with the receiver. If `nil`, then the `valueTransformer` of the parent `objectMapping` will be used instead. A value transformer with which to process input values being mapped with the receiver. If `nil`, then the `valueTransformer` of the parent `objectMapping` will be used instead.
*/ */
Expand Down
20 changes: 20 additions & 0 deletions Tests/Logic/ObjectMapping/RKObjectMappingNextGenTest.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#import "RKDynamicMapping.h" #import "RKDynamicMapping.h"
#import "RKMIMETypeSerialization.h" #import "RKMIMETypeSerialization.h"
#import "ISO8601DateFormatterValueTransformer.h" #import "ISO8601DateFormatterValueTransformer.h"
#import "RKCLLocationValueTransformer.h"


// Managed Object Serialization Testific // Managed Object Serialization Testific
#import "RKHuman.h" #import "RKHuman.h"
Expand Down Expand Up @@ -2519,6 +2520,25 @@ - (void)testAggregatingPropertyMappingUsingNilKeyPath
expect(user.coordinate.longitude).to.equal(200.5); expect(user.coordinate.longitude).to.equal(200.5);
} }


- (void)testMappingDictionaryToCLLocationUsingValueTransformer
{
NSDictionary *objectRepresentation = @{ @"name": @"Blake", @"latitude": @(125.55), @"longitude": @(200.5) };
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKTestUser class]];
[userMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"location"];
attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
[userMapping addPropertyMapping:attributeMapping];
RKTestUser *user = [RKTestUser new];
RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:objectRepresentation destinationObject:user mapping:userMapping];
RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
mappingOperation.dataSource = dataSource;
[mappingOperation start];
expect(mappingOperation.error).to.beNil();
expect(user.location).notTo.beNil();
expect(user.location.coordinate.latitude).to.equal(125.55);
expect(user.location.coordinate.longitude).to.equal(200.5);
}

- (void)testThatAggregatedRelationshipMappingsAreOnlyAppliedIfThereIsAtLeastOneValueInTheRepresentation - (void)testThatAggregatedRelationshipMappingsAreOnlyAppliedIfThereIsAtLeastOneValueInTheRepresentation
{ {
NSDictionary *objectRepresentation = @{ @"name": @"Blake" }; NSDictionary *objectRepresentation = @{ @"name": @"Blake" };
Expand Down
24 changes: 24 additions & 0 deletions Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#import "RKHuman.h" #import "RKHuman.h"
#import "RKTestUser.h" #import "RKTestUser.h"
#import "RKCat.h" #import "RKCat.h"
#import "RKCLLocationValueTransformer.h"


@interface RKMIMETypeSerialization () @interface RKMIMETypeSerialization ()
@property (nonatomic, strong) NSMutableArray *registrations; @property (nonatomic, strong) NSMutableArray *registrations;
Expand Down Expand Up @@ -504,6 +505,29 @@ - (void)testParameterizationOfCoreDataEntityWithDateToNestedKeypath
expect(string).to.equal(@"{\"nestedPath\":{\"birthday\":\"1970-01-01T00:00:00Z\"}}"); expect(string).to.equal(@"{\"nestedPath\":{\"birthday\":\"1970-01-01T00:00:00Z\"}}");
} }


- (void)testParameterizationFromLocationToNestedDictionaryUsingValueTransformer
{
RKTestUser *user = [RKTestUser new];
user.name = @"Blake";
user.location = [[CLLocation alloc] initWithLatitude:125.55 longitude:200.5];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"location" toKeyPath:@"location"];
attributeMapping.propertyValueClass = [NSDictionary class];
attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
[userMapping addPropertyMapping:attributeMapping];

NSError *error = nil;
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[RKTestUser class] rootKeyPath:nil method:RKRequestMethodAny];
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:user requestDescriptor:requestDescriptor error:&error];

expect(parameters).notTo.beNil();
expect(error).to.beNil();
expect(parameters[@"location"]).notTo.beNil();
expect(parameters[@"location"][@"latitude"]).to.equal(125.55);
expect(parameters[@"location"][@"longitude"]).to.equal(200.5);
}

@end @end


#pragma mark - Dynamic Request Paramterization #pragma mark - Dynamic Request Paramterization
Expand Down
2 changes: 2 additions & 0 deletions Tests/Models/RKTestUser.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// //


#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "RKTestAddress.h" #import "RKTestAddress.h"
#import "RKHuman.h" #import "RKHuman.h"


Expand Down Expand Up @@ -38,6 +39,7 @@
@property (nonatomic, strong) NSOrderedSet *friendsOrderedSet; @property (nonatomic, strong) NSOrderedSet *friendsOrderedSet;
@property (nonatomic, strong) NSData *data; @property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) RKTestCoordinate *coordinate; @property (nonatomic, strong) RKTestCoordinate *coordinate;
@property (nonatomic, copy) CLLocation *location;
@property (nonatomic, assign) NSInteger age; @property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) NSDate *createdAt; @property (nonatomic, strong) NSDate *createdAt;
@property (nonatomic, assign) NSInteger position; @property (nonatomic, assign) NSInteger position;
Expand Down

0 comments on commit cee1337

Please sign in to comment.