Skip to content

Commit

Permalink
add tests to new mapping methods, make sure that asserts on mapping p…
Browse files Browse the repository at this point in the history
…rotocol happen only for class mappings. Add a warning on recursive mappings.
  • Loading branch information
DenTelezhkin committed Dec 7, 2014
1 parent c579262 commit 805657a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
7 changes: 5 additions & 2 deletions EasyMapping/EKObjectMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@
- (void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property;

/**
Map to-one relationship for keyPath. ObjectClass should conform to `EKMappingProtocol`.
Map to-one relationship for keyPath.
@param keyPath keyPath to child object representation in JSON
@param property Name of the property, that will receive mapped object.
@param objectMapping optional mapping override for child object
@warning If you have recursive mappings, do not use this method, cause it can cause infinite recursion to happen. Or you need to handle recursive mappings situation by yourself, subclassing EKObjectMapping and providing different mappings for different mapping levels.
*/
- (void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping;

Expand All @@ -229,14 +231,15 @@
- (void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property;

/**
Map to-many relationship for keyPath. ObjectClass should conform to `EKMappingProtocol`.
Map to-many relationship for keyPath.
@param keyPath keyPath to child objects representation in JSON
@param property Name of the property, that will receive mapped objects.
@param objectMapping optional mapping override for child objects
@warning If you have recursive mappings, do not use this method, cause it can cause infinite recursion to happen. Or you need to handle recursive mappings situation by yourself, subclassing EKObjectMapping and providing different mappings for different mapping levels.
*/
-(void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping;

Expand Down
12 changes: 8 additions & 4 deletions EasyMapping/EKObjectMapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ -(void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSS

-(void)hasOne:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping
{
NSParameterAssert([objectClass conformsToProtocol:@protocol(EKMappingProtocol)] ||
[objectClass conformsToProtocol:@protocol(EKManagedMappingProtocol)]);
if (!objectMapping) {
NSParameterAssert([objectClass conformsToProtocol:@protocol(EKMappingProtocol)] ||
[objectClass conformsToProtocol:@protocol(EKManagedMappingProtocol)]);
}
NSParameterAssert(keyPath);
NSParameterAssert(property);

Expand All @@ -211,8 +213,10 @@ -(void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NS

-(void)hasMany:(Class)objectClass forKeyPath:(NSString *)keyPath forProperty:(NSString *)property withObjectMapping:(EKObjectMapping*)objectMapping
{
NSParameterAssert([objectClass conformsToProtocol:@protocol(EKMappingProtocol)] ||
[objectClass conformsToProtocol:@protocol(EKManagedMappingProtocol)]);
if (!objectMapping) {
NSParameterAssert([objectClass conformsToProtocol:@protocol(EKMappingProtocol)] ||
[objectClass conformsToProtocol:@protocol(EKManagedMappingProtocol)]);
}
NSParameterAssert(keyPath);
NSParameterAssert(property);

Expand Down
66 changes: 66 additions & 0 deletions EasyMappingExample/Tests/Specs/EKObjectMappingSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@
[[mapping should] respondToSelector:@selector(hasMany:forKeyPath:forProperty:)];
});

specify(^{
[[mapping should] respondToSelector:@selector(hasOne:forKeyPath:forProperty:withObjectMapping:)];
});

specify(^{
[[mapping should] respondToSelector:@selector(hasMany:forKeyPath:forProperty:withObjectMapping:)];
});

specify(^{
[[mapping should] respondToSelector:@selector(mapPropertiesFromMappingObject:)];
});
Expand Down Expand Up @@ -591,6 +599,64 @@

});

describe(@"#hasOne:forKeyPath:forProperty:withObjectMapping:", ^{
__block EKObjectMapping * mapping;
__block EKObjectMapping * phoneMapping;

beforeEach(^{
mapping = [[EKObjectMapping alloc] initWithObjectClass:[Person class]];
phoneMapping = [Phone objectMapping];
// Use different class on purpose, checking object mapping of the relationship
[mapping hasOne:[Car class]
forKeyPath:@"phone"
forProperty:@"phone"
withObjectMapping:phoneMapping];
});

specify(^{
[mapping.hasOneMappings shouldNotBeNil];
});

specify(^{
[[mapping.hasOneMappings objectForKey:@"phone"] shouldNotBeNil];
});

specify(^{
EKRelationshipMapping * relationship = [mapping.hasOneMappings objectForKey:@"phone"];

[[[relationship objectMapping] should] equal:phoneMapping];
});
});

describe(@"#hasMany:forKeyPath:forProperty:withObjectMapping:", ^{
__block EKObjectMapping * mapping;
__block EKObjectMapping * phoneMapping;

beforeEach(^{
mapping = [[EKObjectMapping alloc] initWithObjectClass:[Person class]];
phoneMapping = [Phone objectMapping];
// Use different class on purpose, checking object mapping of the relationship
[mapping hasMany:[Car class]
forKeyPath:@"phone"
forProperty:@"phone"
withObjectMapping:phoneMapping];
});

specify(^{
[mapping.hasManyMappings shouldNotBeNil];
});

specify(^{
[[mapping.hasManyMappings objectForKey:@"phone"] shouldNotBeNil];
});

specify(^{
EKRelationshipMapping * relationship = [mapping.hasManyMappings objectForKey:@"phone"];

[[[relationship objectMapping] should] equal:phoneMapping];
});
});

});

SPEC_END
Expand Down

0 comments on commit 805657a

Please sign in to comment.