From e5d876a1461b7c5c1663a46b27e020cf0d19edc9 Mon Sep 17 00:00:00 2001 From: Saul Mora Date: Thu, 18 Aug 2011 16:15:38 -0600 Subject: [PATCH] Improve date import handling Added secondary attribute key mapping support Added test for secondary relationship key mapping support --- Magical Record.xcodeproj/project.pbxproj | 24 +++++++- .../NSManagedObject+MagicalDataImport.h | 4 ++ .../NSManagedObject+MagicalDataImport.m | 59 ++++++++++++++++++- Source/MagicalRecordHelpers.h | 3 +- Source/MagicalRecordHelpers.m | 20 +++++-- ...edToMappedEntityWithSecondaryMappings.json | 8 +++ .../SingleEntityWithNoRelationships.plist | 2 + ...latedToMappedEntityWithSecondaryMappings.h | 5 ++ ...latedToMappedEntityWithSecondaryMappings.m | 7 +++ ...latedToMappedEntityWithSecondaryMappings.h | 59 +++++++++++++++++++ ...latedToMappedEntityWithSecondaryMappings.m | 54 +++++++++++++++++ .../_SingleEntityWithNoRelationships.h | 15 +++++ .../_SingleEntityWithNoRelationships.m | 7 +++ .../TestModel.xcdatamodel/contents | 30 ++++++++-- ...ToMappedEntityUsingMappedPrimaryKeyTests.m | 1 + ...ToMappedEntityWithSecondaryMappingsTests.m | 39 ++++++++++++ ...portSingleEntityWithNoRelationshipsTests.m | 54 +++++++++++++---- 17 files changed, 366 insertions(+), 25 deletions(-) create mode 100644 Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json create mode 100644 Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.h create mode 100644 Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.m create mode 100644 Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h create mode 100644 Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.m create mode 100644 Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m diff --git a/Magical Record.xcodeproj/project.pbxproj b/Magical Record.xcodeproj/project.pbxproj index 06cfaff..d86ca89 100644 --- a/Magical Record.xcodeproj/project.pbxproj +++ b/Magical Record.xcodeproj/project.pbxproj @@ -66,6 +66,10 @@ C77E5FBB13D0D2AE00298F87 /* FixtureHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = C77E5FBA13D0D2AE00298F87 /* FixtureHelpers.m */; }; C784349013F0FEE000463CEE /* _MappedEntity.m in Sources */ = {isa = PBXBuildFile; fileRef = C784348C13F0FEE000463CEE /* _MappedEntity.m */; }; C784349213F0FEE000463CEE /* MappedEntity.m in Sources */ = {isa = PBXBuildFile; fileRef = C784348E13F0FEE000463CEE /* MappedEntity.m */; }; + C78F8FD313FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json in Resources */ = {isa = PBXBuildFile; fileRef = C78F8FD213FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */; }; + C78F8FD713FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C78F8FD613FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */; }; + C78F8FDA13FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */ = {isa = PBXBuildFile; fileRef = C78F8FD913FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */; }; + C78F8FDD13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */ = {isa = PBXBuildFile; fileRef = C78F8FDC13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */; }; C7913B9513FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m in Sources */ = {isa = PBXBuildFile; fileRef = C7913B9413FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m */; }; C7913B9913FAEDF8007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m in Sources */ = {isa = PBXBuildFile; fileRef = C7913B9813FAEDF8007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m */; }; C7913B9A13FAEF0F007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json in Resources */ = {isa = PBXBuildFile; fileRef = C7B7379913FAE5D500EE4940 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json */; }; @@ -211,6 +215,12 @@ C784348C13F0FEE000463CEE /* _MappedEntity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _MappedEntity.m; sourceTree = ""; }; C784348D13F0FEE000463CEE /* MappedEntity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MappedEntity.h; sourceTree = ""; }; C784348E13F0FEE000463CEE /* MappedEntity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MappedEntity.m; sourceTree = ""; }; + C78F8FD213FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToMappedEntityWithSecondaryMappings.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json"; sourceTree = ""; }; + C78F8FD613FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m"; sourceTree = ""; }; + C78F8FD813FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToMappedEntityWithSecondaryMappings.h; sourceTree = ""; }; + C78F8FD913FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityRelatedToMappedEntityWithSecondaryMappings.m; sourceTree = ""; }; + C78F8FDB13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _SingleEntityRelatedToMappedEntityWithSecondaryMappings.h; sourceTree = ""; }; + C78F8FDC13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m; sourceTree = ""; }; C7913B9313FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h; sourceTree = ""; }; C7913B9413FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m; sourceTree = ""; }; C7913B9713FAEDF8007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h; sourceTree = ""; }; @@ -512,6 +522,7 @@ C7C9A38C13F45438002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m */, C7B7379D13FAE6D000EE4940 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m */, C7913BAC13FB1DF9007E09CC /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m */, + C78F8FD613FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */, ); name = "Data Import Tests"; sourceTree = ""; @@ -567,6 +578,7 @@ C7C9A37E13F44B29002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.json */, C7B7379913FAE5D500EE4940 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json */, C7913BAE13FB1E11007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json */, + C78F8FD213FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */, ); name = "Sample Data Files"; sourceTree = ""; @@ -574,6 +586,8 @@ C7913BAA13FB1D36007E09CC /* Generated */ = { isa = PBXGroup; children = ( + C78F8FDB13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */, + C78F8FDC13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */, C7BD887813DBFA6200274567 /* _AbstractRelatedEntity.h */, C7BD887913DBFA6200274567 /* _AbstractRelatedEntity.m */, C7BD887A13DBFA6200274567 /* _ConcreteRelatedEntity.h */, @@ -660,6 +674,8 @@ C7BD888913DBFA6200274567 /* SingleEntityWithNoRelationships.m */, C7BD888A13DBFA6200274567 /* SingleRelatedEntity.h */, C7BD888B13DBFA6200274567 /* SingleRelatedEntity.m */, + C78F8FD813FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */, + C78F8FD913FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */, C7C9A38613F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.h */, C7C9A38713F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.m */, C7C9A38813F4524B002C5B0C /* SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey.h */, @@ -775,6 +791,7 @@ C7C9A37F13F44B29002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.json in Resources */, C7913B9A13FAEF0F007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json in Resources */, C7913BAF13FB1E11007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json in Resources */, + C78F8FD313FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -897,6 +914,9 @@ C7913BA613FB1D17007E09CC /* _SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m in Sources */, C7913BA913FB1D2A007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.m in Sources */, C7913BAD13FB1DF9007E09CC /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m in Sources */, + C78F8FD713FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m in Sources */, + C78F8FDA13FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */, + C78F8FDD13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -1065,7 +1085,7 @@ "\"$(DEVELOPER_FRAMEWORKS_DIR)\"", ); INFOPLIST_FILE = "iOS App Unit Tests/iOS App Unit Tests-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/iOS App Unit Tests/Frameworks/OCMockLibrary\"", @@ -1109,7 +1129,7 @@ "\"$(DEVELOPER_FRAMEWORKS_DIR)\"", ); INFOPLIST_FILE = "iOS App Unit Tests/iOS App Unit Tests-Info.plist"; - IPHONEOS_DEPLOYMENT_TARGET = 5.0; + IPHONEOS_DEPLOYMENT_TARGET = 4.3; LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/iOS App Unit Tests/Frameworks/OCMockLibrary\"", diff --git a/Source/Categories/NSManagedObject+MagicalDataImport.h b/Source/Categories/NSManagedObject+MagicalDataImport.h index 1589ecc..463b0e3 100644 --- a/Source/Categories/NSManagedObject+MagicalDataImport.h +++ b/Source/Categories/NSManagedObject+MagicalDataImport.h @@ -8,6 +8,7 @@ #import +extern NSString * const kMagicalRecordImportCustomDateFormatKey; extern NSString * const kMagicalRecordImportDefaultDateFormatString; extern NSString * const kMagicalRecordImportAttributeKeyMapKey; extern NSString * const kMagicalRecordImportAttributeValueClassNameKey; @@ -23,6 +24,9 @@ extern NSString * const kMagicalRecordImportRelationshipTypeKey; + (id) MR_importFromDictionary:(NSDictionary *)data; + (id) MR_importFromDictionary:(NSDictionary *)data inContext:(NSManagedObjectContext *)context; ++ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData; ++ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context; + @end diff --git a/Source/Categories/NSManagedObject+MagicalDataImport.m b/Source/Categories/NSManagedObject+MagicalDataImport.m index 6e163e8..f5a805e 100644 --- a/Source/Categories/NSManagedObject+MagicalDataImport.m +++ b/Source/Categories/NSManagedObject+MagicalDataImport.m @@ -8,7 +8,8 @@ #import "CoreData+MagicalRecord.h" -NSString * const kMagicalRecordImportDefaultDateFormatString = @"YYYY-MM-dd'T'HH:mm:ss'Z'"; +NSString * const kMagicalRecordImportCustomDateFormatKey = @"dateFormat"; +NSString * const kMagicalRecordImportDefaultDateFormatString = @"yyyy-MM-dd'T'HH:mm:ss'Z'"; NSString * const kMagicalRecordImportAttributeKeyMapKey = @"mappedKeyName"; NSString * const kMagicalRecordImportAttributeValueClassNameKey = @"attributeValueClassName"; @@ -41,7 +42,20 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type"; if (value == nil || [value isEqual:[NSNull null]]) { - return nil; + for (int i=1; i < 10 && value == nil; i++) + { + NSString *userInfoKey = [NSString stringWithFormat:@"%@.%d", kMagicalRecordImportAttributeKeyMapKey, i]; + NSString *dataLookup = [[attributeInfo userInfo] valueForKey:userInfoKey]; + if (dataLookup == nil) + { + return nil; + } + value = [jsonData valueForKeyPath:dataLookup]; + } + if (value == nil) + { + return nil; + } } NSAttributeType attributeType = [attributeInfo attributeType]; @@ -57,7 +71,15 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type"; { if (attributeType == NSDateAttributeType) { - value = dateFromString([value description]); + if (![value isKindOfClass:[NSDate class]]) + { + NSString *dateFormat = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportCustomDateFormatKey]; + value = dateFromString([value description], dateFormat ?: kMagicalRecordImportDefaultDateFormatString); + } + else + { + value = adjustDateForDST(value); + } } } @@ -226,4 +248,35 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type"; return [self MR_updateFromDictionary:objectData inContext:[NSManagedObjectContext defaultContext]]; } + ++ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData +{ + return [self MR_importFromArray:listOfObjectData inContext:[NSManagedObjectContext context]]; +} + ++ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context +{ + NSMutableArray *objectIDs = [NSMutableArray array]; + [MRCoreDataAction saveDataWithBlock:^(NSManagedObjectContext *localContext) + { + [listOfObjectData enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) + { + NSDictionary *objectData = (NSDictionary *)obj; + + NSManagedObject *dataObject = [self MR_importFromDictionary:objectData inContext:localContext]; + + if ([context obtainPermanentIDsForObjects:[NSArray arrayWithObject:dataObject] error:nil]) + { + [objectIDs addObject:[dataObject objectID]]; + } + }]; + + // NSError *error = nil; + // [context obtainPermanentIDsForObjects:objectIDs error:&error]; + // NSLog(@"Error: %@", error); + }]; + + return [self findAllWithPredicate:[NSPredicate predicateWithFormat:@"self IN %@", objectIDs] inContext:context]; +} + @end diff --git a/Source/MagicalRecordHelpers.h b/Source/MagicalRecordHelpers.h index 4ec132e..d0cd866 100644 --- a/Source/MagicalRecordHelpers.h +++ b/Source/MagicalRecordHelpers.h @@ -64,7 +64,8 @@ typedef void (^CoreDataBlock)(NSManagedObjectContext *context); //Helper Functions -NSDate * dateFromString(NSString *value); +NSDate * adjustDateForDST(NSDate *date); +NSDate * dateFromString(NSString *value, NSString *format); NSString * attributeNameFromString(NSString *value); NSString * primaryKeyNameFromString(NSString *value); diff --git a/Source/MagicalRecordHelpers.m b/Source/MagicalRecordHelpers.m index bda57e3..e924abf 100644 --- a/Source/MagicalRecordHelpers.m +++ b/Source/MagicalRecordHelpers.m @@ -210,12 +210,24 @@ NSString * primaryKeyNameFromString(NSString *value) return [firstCharacter stringByAppendingString:[value substringFromIndex:1]]; } -NSDate * dateFromString(NSString *value) +NSDate * adjustDateForDST(NSDate *date) { - NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; - [formatter setDateFormat:kMagicalRecordImportDefaultDateFormatString]; + NSTimeInterval dstOffset = [[NSTimeZone localTimeZone] daylightSavingTimeOffsetForDate:date]; + NSDate *actualDate = [date dateByAddingTimeInterval:dstOffset]; + + return actualDate; +} + +NSDate * dateFromString(NSString *value, NSString *format) +{ + NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; + [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; + [formatter setLocale:[NSLocale currentLocale]]; + [formatter setDateFormat:format]; - return [formatter dateFromString:value]; + NSDate *parsedDate = [formatter dateFromString:value]; + + return adjustDateForDST(parsedDate); } NSInteger* newColorComponentsFromString(NSString *serializedColor); diff --git a/Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json b/Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json new file mode 100644 index 0000000..aa093ae --- /dev/null +++ b/Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json @@ -0,0 +1,8 @@ +{ + "actualMappedKey": "mapped value from sample json file", + "actualRelatedObject": + { + "id": 50, + "sampleAttribute": "actualMappedRelationship value from sample json file" + } +} \ No newline at end of file diff --git a/Unit Tests/Fixtures/SingleEntityWithNoRelationships.plist b/Unit Tests/Fixtures/SingleEntityWithNoRelationships.plist index ef08aa3..26899b3 100644 --- a/Unit Tests/Fixtures/SingleEntityWithNoRelationships.plist +++ b/Unit Tests/Fixtures/SingleEntityWithNoRelationships.plist @@ -24,5 +24,7 @@ rgba(64,128,225,1) mappedAttributeWithStringValue Mapped value + dateWithCustomFormat + 8/5/2011 1-56-04 AM diff --git a/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.h b/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.h new file mode 100644 index 0000000..6f69cb8 --- /dev/null +++ b/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.h @@ -0,0 +1,5 @@ +#import "_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h" + +@interface SingleEntityRelatedToMappedEntityWithSecondaryMappings : _SingleEntityRelatedToMappedEntityWithSecondaryMappings {} +// Custom logic goes here. +@end diff --git a/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.m b/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.m new file mode 100644 index 0000000..44a0017 --- /dev/null +++ b/Unit Tests/Fixtures/iOS/TestEntities/SingleEntityRelatedToMappedEntityWithSecondaryMappings.m @@ -0,0 +1,7 @@ +#import "SingleEntityRelatedToMappedEntityWithSecondaryMappings.h" + +@implementation SingleEntityRelatedToMappedEntityWithSecondaryMappings + +// Custom logic goes here. + +@end diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h new file mode 100644 index 0000000..00fc5a5 --- /dev/null +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h @@ -0,0 +1,59 @@ +// DO NOT EDIT. This file is machine-generated and constantly overwritten. +// Make changes to SingleEntityRelatedToMappedEntityWithSecondaryMappings.h instead. + +#import + + +@class MappedEntity; + + + +@interface SingleEntityRelatedToMappedEntityWithSecondaryMappingsID : NSManagedObjectID {} +@end + +@interface _SingleEntityRelatedToMappedEntityWithSecondaryMappings : NSManagedObject {} ++ (id)insertInManagedObjectContext:(NSManagedObjectContext*)moc_; ++ (NSString*)entityName; ++ (NSEntityDescription*)entityInManagedObjectContext:(NSManagedObjectContext*)moc_; +- (SingleEntityRelatedToMappedEntityWithSecondaryMappingsID*)objectID; + + + + +@property (nonatomic, retain) NSString *secondaryMappedAttribute; + + +//- (BOOL)validateSecondaryMappedAttribute:(id*)value_ error:(NSError**)error_; + + + + + +@property (nonatomic, retain) MappedEntity* mappedRelationship; + +//- (BOOL)validateMappedRelationship:(id*)value_ error:(NSError**)error_; + + + + +@end + +@interface _SingleEntityRelatedToMappedEntityWithSecondaryMappings (CoreDataGeneratedAccessors) + +@end + +@interface _SingleEntityRelatedToMappedEntityWithSecondaryMappings (CoreDataGeneratedPrimitiveAccessors) + + +- (NSString*)primitiveSecondaryMappedAttribute; +- (void)setPrimitiveSecondaryMappedAttribute:(NSString*)value; + + + + + +- (MappedEntity*)primitiveMappedRelationship; +- (void)setPrimitiveMappedRelationship:(MappedEntity*)value; + + +@end diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.m b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.m new file mode 100644 index 0000000..753664a --- /dev/null +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityRelatedToMappedEntityWithSecondaryMappings.m @@ -0,0 +1,54 @@ +// DO NOT EDIT. This file is machine-generated and constantly overwritten. +// Make changes to SingleEntityRelatedToMappedEntityWithSecondaryMappings.m instead. + +#import "_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h" + +@implementation SingleEntityRelatedToMappedEntityWithSecondaryMappingsID +@end + +@implementation _SingleEntityRelatedToMappedEntityWithSecondaryMappings + ++ (id)insertInManagedObjectContext:(NSManagedObjectContext*)moc_ { + NSParameterAssert(moc_); + return [NSEntityDescription insertNewObjectForEntityForName:@"SingleEntityRelatedToMappedEntityWithSecondaryMappings" inManagedObjectContext:moc_]; +} + ++ (NSString*)entityName { + return @"SingleEntityRelatedToMappedEntityWithSecondaryMappings"; +} + ++ (NSEntityDescription*)entityInManagedObjectContext:(NSManagedObjectContext*)moc_ { + NSParameterAssert(moc_); + return [NSEntityDescription entityForName:@"SingleEntityRelatedToMappedEntityWithSecondaryMappings" inManagedObjectContext:moc_]; +} + +- (SingleEntityRelatedToMappedEntityWithSecondaryMappingsID*)objectID { + return (SingleEntityRelatedToMappedEntityWithSecondaryMappingsID*)[super objectID]; +} + ++ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { + NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; + + + return keyPaths; +} + + + + +@dynamic secondaryMappedAttribute; + + + + + + +@dynamic mappedRelationship; + + + + + + + +@end diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.h b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.h index 05e2c76..72b3d38 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.h +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.h @@ -17,6 +17,7 @@ + @interface SingleEntityWithNoRelationshipsID : NSManagedObjectID {} @end @@ -57,6 +58,14 @@ +@property (nonatomic, retain) NSDate *dateWithCustomFormat; + + +//- (BOOL)validateDateWithCustomFormat:(id*)value_ error:(NSError**)error_; + + + + @property (nonatomic, retain) NSDecimalNumber *decimalTestAttribute; @@ -172,6 +181,12 @@ +- (NSDate*)primitiveDateWithCustomFormat; +- (void)setPrimitiveDateWithCustomFormat:(NSDate*)value; + + + + - (NSDecimalNumber*)primitiveDecimalTestAttribute; - (void)setPrimitiveDecimalTestAttribute:(NSDecimalNumber*)value; diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.m b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.m index dd80199..8570584 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.m +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleEntityWithNoRelationships.m @@ -100,6 +100,13 @@ +@dynamic dateWithCustomFormat; + + + + + + @dynamic decimalTestAttribute; diff --git a/Unit Tests/Fixtures/iOS/TestModel.xcdatamodeld/TestModel.xcdatamodel/contents b/Unit Tests/Fixtures/iOS/TestModel.xcdatamodeld/TestModel.xcdatamodel/contents index 1c0845a..c144d63 100644 --- a/Unit Tests/Fixtures/iOS/TestModel.xcdatamodeld/TestModel.xcdatamodel/contents +++ b/Unit Tests/Fixtures/iOS/TestModel.xcdatamodeld/TestModel.xcdatamodel/contents @@ -1,5 +1,5 @@ - + @@ -42,6 +42,22 @@ + + + + + + + + + + + + + + + + @@ -50,6 +66,11 @@ + + + + + @@ -77,9 +98,10 @@ - - - + + + + \ No newline at end of file diff --git a/Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m b/Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m index 67ec9fa..90a835d 100644 --- a/Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m +++ b/Unit Tests/ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m @@ -62,4 +62,5 @@ assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file")); } + @end diff --git a/Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m b/Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m new file mode 100644 index 0000000..000dda6 --- /dev/null +++ b/Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m @@ -0,0 +1,39 @@ +// +// ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m +// Magical Record +// +// Created by Saul Mora on 8/18/11. +// Copyright (c) 2011 Magical Panda Software LLC. All rights reserved. +// +#import "MagicalDataImportTestCase.h" +#import "SingleEntityRelatedToMappedEntityWithSecondaryMappings.h" +#import "MappedEntity.h" + +@interface ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests : MagicalDataImportTestCase + +@end + +@implementation ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests + +- (Class) testEntityClass +{ + return [SingleEntityRelatedToMappedEntityWithSecondaryMappings class]; +} + +- (void) testImportMappedAttributeUsingSecondaryMappedKeyName +{ + SingleEntityRelatedToMappedEntityWithSecondaryMappings *entity = (SingleEntityRelatedToMappedEntityWithSecondaryMappings *)self.testEntity; + + assertThat(entity, is(notNilValue())); + assertThat([entity secondaryMappedAttribute], containsString(@"sample json file")); +} + +- (void) testImportMappedEntityUsingThirdPriorityMappedKeyName +{ + SingleEntityRelatedToMappedEntityWithSecondaryMappings *entity = (SingleEntityRelatedToMappedEntityWithSecondaryMappings *)self.testEntity; + id testRelatedEntity = entity.mappedRelationship; + + assertThat(testRelatedEntity, is(notNilValue())); + assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file")); +} +@end diff --git a/Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m b/Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m index 536dcce..858f26f 100644 --- a/Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m +++ b/Unit Tests/ImportSingleEntityWithNoRelationshipsTests.m @@ -83,19 +83,37 @@ assertThat(testEntity.mappedStringAttribute, is(equalTo(@"Mapped value"))); } -#if TARGET_OS_IPHONE && __IPHONE_5_0 >= 50000 +#if TARGET_OS_IPHONE + +#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0 - (void) testImportUIColorAttributeToEntity { UIColor *actualColor = testEntity.colorTestAttribute; - - CGFloat red, blue, green, alpha; - [actualColor getRed:&red green:&green blue:&blue alpha:&alpha]; + + if ([actualColor respondsToSelector:@selector(getRed:green:blue:alpha:)]) + { + CGFloat red, blue, green, alpha; + [actualColor getRed:&red green:&green blue:&blue alpha:&alpha]; - assertThatFloat(alpha, is(equalToFloat(1.))); - assertThatFloat(red, is(equalToFloat(64./255.))); - assertThatFloat(green, is(equalToFloat(128./255.))); - assertThatFloat(blue, is(equalToFloat(225./255.))); + assertThatFloat(alpha, is(equalToFloat(1.))); + assertThatFloat(red, is(equalToFloat(64./255.))); + assertThatFloat(green, is(equalToFloat(128./255.))); + assertThatFloat(blue, is(equalToFloat(225./255.))); + } +} +#endif + +- (NSDate *) dateFromString:(NSString *)date +{ + NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; + formatter.dateFormat = @"MMM d, yyyy hh:mm:ss a zzz"; + formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; + formatter.locale = [NSLocale currentLocale]; + + NSDate *expectedDate = [formatter dateFromString:date]; + + return expectedDate; } #else @@ -110,11 +128,25 @@ assertThatFloat(actualColor.blueComponent, is(equalToFloat(225./255.))); } -- (void) testImportDateAttributeToEntity +- (NSDate *) dateFromString:(NSString *)date { - NSDate *expectedDate = [NSDate dateWithString:@"Jul 23, 2011 10:30:40 PM"]; - assertThat(testEntity.dateTestAttribute, is(equalTo(expectedDate))); + NSDate *expectedDate = [NSDate dateWithString:date]; + return expectedDate; } + #endif +- (void) testImportDateAttributeToEntity +{ + NSDate *expectedDate = [self dateFromString:@"Jul 23, 2011 10:30:40 PM MST"]; + assertThat(testEntity.dateTestAttribute, is(equalTo(expectedDate))); +} + +- (void) testImportDataAttributeWithCustomFormat +{ + NSDate *expectedDate = [self dateFromString:@"Aug 5, 2011 01:56:04 AM MST"]; + assertThat(testEntity.dateWithCustomFormat, is(equalTo(expectedDate))); + +} + @end