mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-06-12 00:35:21 +08:00
Other changes include: * Eliminated the RKObjectFactory protocol and implementations. Object mapping instances themselves are now responsible for instantiating target objects for mapping. * Introduced RKObjectAbstractMapping superclass for RKObjectMapping and RKObjectPolymorphicMapping. * Updated example applications to use block object loaders (RKTwitter and RKTwitterCoreData) * Refactored method signatures of RKObjectMapper, RKObjectMapping, and RKObjectMappingProvider to reflect the existence of abstract mapping types. This was necessary to make polymorphic mappings integrate cleanly. * Fixed overlap in RestKit error domains between network and object mapping. fixes #208
119 lines
4.7 KiB
Objective-C
119 lines
4.7 KiB
Objective-C
//
|
|
// RKManagedObjectMappingSpec.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 5/31/11.
|
|
// Copyright 2011 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "RKSpecEnvironment.h"
|
|
#import "RKManagedObjectMapping.h"
|
|
#import "RKHuman.h"
|
|
#import "RKMappableObject.h"
|
|
|
|
@interface RKManagedObjectMappingSpec : RKSpec {
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
@implementation RKManagedObjectMappingSpec
|
|
|
|
- (void)itShouldReturnTheDefaultValueForACoreDataAttribute {
|
|
// Load Core Data
|
|
RKSpecNewManagedObjectStore();
|
|
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForEntityWithName:@"RKCat"];
|
|
id value = [mapping defaultValueForMissingAttribute:@"name"];
|
|
assertThat(value, is(equalTo(@"Kitty Cat!")));
|
|
}
|
|
|
|
- (void)itShouldCreateNewInstancesOfUnmanagedObjects {
|
|
RKSpecNewManagedObjectStore();
|
|
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKMappableObject class]];
|
|
id object = [mapping mappableObjectForData:[NSDictionary dictionary]];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat([object class], is(equalTo([RKMappableObject class])));
|
|
}
|
|
|
|
- (void)itShouldCreateNewInstancesOfManagedObjectsWhenTheMappingIsAnRKObjectMapping {
|
|
RKSpecNewManagedObjectStore();
|
|
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKMappableObject class]];
|
|
id object = [mapping mappableObjectForData:[NSDictionary dictionary]];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat([object class], is(equalTo([RKMappableObject class])));
|
|
}
|
|
|
|
- (void)itShouldFindExistingManagedObjectsByPrimaryKey {
|
|
RKManagedObjectStore* store = RKSpecNewManagedObjectStore();
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForClass:[RKHuman class]];
|
|
mapping.primaryKeyAttribute = @"railsID";
|
|
[mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:@"id" toKeyPath:@"railsID"]];
|
|
|
|
RKHuman* human = [RKHuman object];
|
|
human.railsID = [NSNumber numberWithInt:123];
|
|
[store save];
|
|
assertThatBool([RKHuman hasAtLeastOneEntity], is(equalToBool(YES)));
|
|
|
|
NSDictionary* data = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:123] forKey:@"id"];
|
|
id object = [mapping mappableObjectForData:data];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat(object, is(equalTo(human)));
|
|
}
|
|
|
|
- (void)itShouldFindExistingManagedObjectsByPrimaryKeyPath {
|
|
RKManagedObjectStore* store = RKSpecNewManagedObjectStore();
|
|
[RKHuman truncateAll];
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForClass:[RKHuman class]];
|
|
mapping.primaryKeyAttribute = @"railsID";
|
|
[mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:@"monkey.id" toKeyPath:@"railsID"]];
|
|
|
|
[RKHuman truncateAll];
|
|
RKHuman* human = [RKHuman object];
|
|
human.railsID = [NSNumber numberWithInt:123];
|
|
[store save];
|
|
assertThatBool([RKHuman hasAtLeastOneEntity], is(equalToBool(YES)));
|
|
|
|
NSDictionary* data = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:123] forKey:@"id"];
|
|
NSDictionary* nestedDictionary = [NSDictionary dictionaryWithObject:data forKey:@"monkey"];
|
|
id object = [mapping mappableObjectForData:nestedDictionary];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat(object, is(equalTo(human)));
|
|
}
|
|
|
|
- (void)itShouldCreateNewManagedObjectInstancesWhenThereIsNoPrimaryKeyInTheData {
|
|
RKSpecNewManagedObjectStore();
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForClass:[RKHuman class]];
|
|
mapping.primaryKeyAttribute = @"railsID";
|
|
|
|
NSDictionary* data = [NSDictionary dictionary];
|
|
id object = [mapping mappableObjectForData:data];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat(object, is(instanceOf([RKHuman class])));
|
|
}
|
|
|
|
- (void)itShouldCreateNewManagedObjectInstancesWhenThereIsNoPrimaryKeyAttribute {
|
|
RKSpecNewManagedObjectStore();
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForClass:[RKHuman class]];
|
|
|
|
NSDictionary* data = [NSDictionary dictionary];
|
|
id object = [mapping mappableObjectForData:data];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat(object, is(instanceOf([RKHuman class])));
|
|
}
|
|
|
|
- (void)itShouldCreateANewManagedObjectWhenThePrimaryKeyValueIsNSNull {
|
|
RKSpecNewManagedObjectStore();
|
|
RKManagedObjectMapping* mapping = [RKManagedObjectMapping mappingForClass:[RKHuman class]];
|
|
mapping.primaryKeyAttribute = @"railsID";
|
|
[mapping addAttributeMapping:[RKObjectAttributeMapping mappingFromKeyPath:@"id" toKeyPath:@"railsID"]];
|
|
|
|
NSDictionary* data = [NSDictionary dictionaryWithObject:[NSNull null] forKey:@"id"];
|
|
id object = [mapping mappableObjectForData:data];
|
|
assertThat(object, isNot(nilValue()));
|
|
assertThat(object, is(instanceOf([RKHuman class])));
|
|
}
|
|
|
|
@end
|