Improve date import handling

Added secondary attribute key mapping support
Added test for secondary relationship key mapping support
This commit is contained in:
Saul Mora
2011-08-18 16:15:38 -06:00
parent d6bf4cc9b0
commit e5d876a146
17 changed files with 366 additions and 25 deletions

View File

@@ -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 = "<group>"; };
C784348D13F0FEE000463CEE /* MappedEntity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MappedEntity.h; sourceTree = "<group>"; };
C784348E13F0FEE000463CEE /* MappedEntity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MappedEntity.m; sourceTree = "<group>"; };
C78F8FD213FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; name = SingleEntityRelatedToMappedEntityWithSecondaryMappings.json; path = "Unit Tests/Fixtures/SingleEntityRelatedToMappedEntityWithSecondaryMappings.json"; sourceTree = "<group>"; };
C78F8FD613FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m; path = "Unit Tests/ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m"; sourceTree = "<group>"; };
C78F8FD813FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToMappedEntityWithSecondaryMappings.h; sourceTree = "<group>"; };
C78F8FD913FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SingleEntityRelatedToMappedEntityWithSecondaryMappings.m; sourceTree = "<group>"; };
C78F8FDB13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _SingleEntityRelatedToMappedEntityWithSecondaryMappings.h; sourceTree = "<group>"; };
C78F8FDC13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m; sourceTree = "<group>"; };
C7913B9313FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h; sourceTree = "<group>"; };
C7913B9413FAEDE9007E09CC /* _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.m; sourceTree = "<group>"; };
C7913B9713FAEDF8007E09CC /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.h; sourceTree = "<group>"; };
@@ -512,6 +522,7 @@
C7C9A38C13F45438002C5B0C /* ImportSingleEntityRelatedToMappedEntityUsingMappedPrimaryKeyTests.m */,
C7B7379D13FAE6D000EE4940 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyTests.m */,
C7913BAC13FB1DF9007E09CC /* ImportSingleEntityRelatedToMappedEntityWithNestedMappedAttributesTests.m */,
C78F8FD613FDC31700549DD8 /* ImportSingleEntityRelatedToMappedEntityWithSecondaryMappingsTests.m */,
);
name = "Data Import Tests";
sourceTree = "<group>";
@@ -567,6 +578,7 @@
C7C9A37E13F44B29002C5B0C /* SingleEntityRelatedToMappedEntityUsingDefaults.json */,
C7B7379913FAE5D500EE4940 /* SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey.json */,
C7913BAE13FB1E11007E09CC /* SingleEntityRelatedToMappedEntityWithNestedMappedAttributes.json */,
C78F8FD213FDC2B600549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.json */,
);
name = "Sample Data Files";
sourceTree = "<group>";
@@ -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\"",

View File

@@ -8,6 +8,7 @@
#import <CoreData/CoreData.h>
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

View File

@@ -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

View File

@@ -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);

View File

@@ -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);

View File

@@ -0,0 +1,8 @@
{
"actualMappedKey": "mapped value from sample json file",
"actualRelatedObject":
{
"id": 50,
"sampleAttribute": "actualMappedRelationship value from sample json file"
}
}

View File

@@ -24,5 +24,7 @@
<string>rgba(64,128,225,1)</string>
<key>mappedAttributeWithStringValue</key>
<string>Mapped value</string>
<key>dateWithCustomFormat</key>
<string>8/5/2011 1-56-04 AM</string>
</dict>
</plist>

View File

@@ -0,0 +1,5 @@
#import "_SingleEntityRelatedToMappedEntityWithSecondaryMappings.h"
@interface SingleEntityRelatedToMappedEntityWithSecondaryMappings : _SingleEntityRelatedToMappedEntityWithSecondaryMappings {}
// Custom logic goes here.
@end

View File

@@ -0,0 +1,7 @@
#import "SingleEntityRelatedToMappedEntityWithSecondaryMappings.h"
@implementation SingleEntityRelatedToMappedEntityWithSecondaryMappings
// Custom logic goes here.
@end

View File

@@ -0,0 +1,59 @@
// DO NOT EDIT. This file is machine-generated and constantly overwritten.
// Make changes to SingleEntityRelatedToMappedEntityWithSecondaryMappings.h instead.
#import <CoreData/CoreData.h>
@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

View File

@@ -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

View File

@@ -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;

View File

@@ -100,6 +100,13 @@
@dynamic dateWithCustomFormat;
@dynamic decimalTestAttribute;

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="858" systemVersion="11A511" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="858" systemVersion="11B26" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="AbstractRelatedEntity" representedClassName="AbstractRelatedEntity" isAbstract="YES">
<attribute name="sampleBaseAttribute" optional="YES" attributeType="String"/>
</entity>
@@ -42,6 +42,22 @@
<entity name="SingleEntityRelatedToMappedEntityWithNestedMappedAttributes" representedClassName="SingleEntityRelatedToMappedEntityWithNestedMappedAttributes">
<relationship name="mappedEntity" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MappedEntity"/>
</entity>
<entity name="SingleEntityRelatedToMappedEntityWithSecondaryMappings" representedClassName="SingleEntityRelatedToMappedEntityWithSecondaryMappings">
<attribute name="secondaryMappedAttribute" optional="YES" attributeType="String">
<userInfo>
<entry key="mappedKeyName" value="no_in_use"/>
<entry key="mappedKeyName.1" value="getting_warmer"/>
<entry key="mappedKeyName.2" value="actualMappedKey"/>
</userInfo>
</attribute>
<relationship name="mappedRelationship" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MappedEntity">
<userInfo>
<entry key="mappedKeyName" value="not_in_use"/>
<entry key="mappedKeyName.1" value="almost_there"/>
<entry key="mappedKeyName.2" value="actualRelatedObject"/>
</userInfo>
</relationship>
</entity>
<entity name="SingleEntityWithNoRelationships" representedClassName="SingleEntityWithNoRelationships">
<attribute name="booleanTestAttribute" optional="YES" attributeType="Boolean"/>
<attribute name="colorTestAttribute" optional="YES" attributeType="Transformable">
@@ -50,6 +66,11 @@
</userInfo>
</attribute>
<attribute name="dateTestAttribute" optional="YES" attributeType="Date"/>
<attribute name="dateWithCustomFormat" optional="YES" attributeType="Date">
<userInfo>
<entry key="dateFormat" value="M/d/yyyy H-mm-ss a"/>
</userInfo>
</attribute>
<attribute name="decimalTestAttribute" optional="YES" attributeType="Decimal" defaultValueString="0.0"/>
<attribute name="doubleTestAttribute" optional="YES" attributeType="Double" defaultValueString="0.0"/>
<attribute name="floatTestAttribute" optional="YES" attributeType="Float" defaultValueString="0"/>
@@ -77,9 +98,10 @@
<element name="MappedEntity" positionX="421" positionY="558" width="128" height="105"/>
<element name="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey" positionX="657" positionY="450" width="351" height="60"/>
<element name="SingleEntityRelatedToMappedEntityUsingDefaults" positionX="621" positionY="333" width="272" height="60"/>
<element name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" positionX="612" positionY="738" width="342" height="60"/>
<element name="SingleEntityWithNoRelationships" positionX="-135" positionY="318" width="180" height="210"/>
<element name="SingleRelatedEntity" positionX="333" positionY="198" width="189" height="120"/>
<element name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" positionX="603" positionY="702" width="342" height="60"/>
<element name="SingleEntityRelatedToMappedEntityWithNestedMappedAttributes" positionX="664" positionY="597" width="470" height="60"/>
<element name="SingleEntityWithNoRelationships" positionX="-135" positionY="327" width="180" height="225"/>
<element name="SingleRelatedEntity" positionX="333" positionY="198" width="189" height="120"/>
<element name="SingleEntityRelatedToMappedEntityWithSecondaryMappings" positionX="-63" positionY="738" width="344" height="75"/>
</elements>
</model>

View File

@@ -62,4 +62,5 @@
assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file"));
}
@end

View File

@@ -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

View File

@@ -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