Fix some data import tests

Added data update tests
This commit is contained in:
Saul Mora
2011-09-05 12:30:01 -06:00
parent 3dfa3bf613
commit bee9f4a3cf
29 changed files with 600 additions and 142 deletions

View File

@@ -7,6 +7,16 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
C7005F1214140F9B0061B9F4 /* NSNumber+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1114140F9B0061B9F4 /* NSNumber+MagicalDataImport.m */; };
C7005F1314140F9B0061B9F4 /* NSNumber+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1114140F9B0061B9F4 /* NSNumber+MagicalDataImport.m */; };
C7005F1614140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1514140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m */; };
C7005F1714140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1514140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m */; };
C7005F1A14140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1914140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m */; };
C7005F1B14140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1914140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m */; };
C7005F1E1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1D1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m */; };
C7005F1F1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F1D1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m */; };
C7005F2214153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F2114153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m */; };
C7005F2314153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m in Sources */ = {isa = PBXBuildFile; fileRef = C7005F2114153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m */; };
C70B6E7113D0F62500709450 /* NSPersisentStoreHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */; }; C70B6E7113D0F62500709450 /* NSPersisentStoreHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */; };
C70B6E7413D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7313D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m */; }; C70B6E7413D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7313D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.m */; };
C70B6E7713D0F66000709450 /* NSManagedObjectModelHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7613D0F66000709450 /* NSManagedObjectModelHelperTests.m */; }; C70B6E7713D0F66000709450 /* NSManagedObjectModelHelperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C70B6E7613D0F66000709450 /* NSManagedObjectModelHelperTests.m */; };
@@ -141,6 +151,16 @@
/* End PBXCopyFilesBuildPhase section */ /* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
C7005F1014140F9B0061B9F4 /* NSNumber+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSNumber+MagicalDataImport.h"; path = "DataImport/NSNumber+MagicalDataImport.h"; sourceTree = "<group>"; };
C7005F1114140F9B0061B9F4 /* NSNumber+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSNumber+MagicalDataImport.m"; path = "DataImport/NSNumber+MagicalDataImport.m"; sourceTree = "<group>"; };
C7005F1414140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSAttributeDescription+MagicalDataImport.h"; path = "DataImport/NSAttributeDescription+MagicalDataImport.h"; sourceTree = "<group>"; };
C7005F1514140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSAttributeDescription+MagicalDataImport.m"; path = "DataImport/NSAttributeDescription+MagicalDataImport.m"; sourceTree = "<group>"; };
C7005F1814140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSRelationshipDescription+MagicalDataImport.h"; path = "DataImport/NSRelationshipDescription+MagicalDataImport.h"; sourceTree = "<group>"; };
C7005F1914140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSRelationshipDescription+MagicalDataImport.m"; path = "DataImport/NSRelationshipDescription+MagicalDataImport.m"; sourceTree = "<group>"; };
C7005F1C1414100F0061B9F4 /* NSDictionary+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSDictionary+MagicalDataImport.h"; path = "DataImport/NSDictionary+MagicalDataImport.h"; sourceTree = "<group>"; };
C7005F1D1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSDictionary+MagicalDataImport.m"; path = "DataImport/NSDictionary+MagicalDataImport.m"; sourceTree = "<group>"; };
C7005F2014153A060061B9F4 /* NSEntityDescription+MagicalDataImport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSEntityDescription+MagicalDataImport.h"; path = "DataImport/NSEntityDescription+MagicalDataImport.h"; sourceTree = "<group>"; };
C7005F2114153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSEntityDescription+MagicalDataImport.m"; path = "DataImport/NSEntityDescription+MagicalDataImport.m"; sourceTree = "<group>"; };
C70B6E6F13D0F62500709450 /* NSPersisentStoreHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersisentStoreHelperTests.h; path = "Unit Tests/NSPersisentStoreHelperTests.h"; sourceTree = "<group>"; }; C70B6E6F13D0F62500709450 /* NSPersisentStoreHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersisentStoreHelperTests.h; path = "Unit Tests/NSPersisentStoreHelperTests.h"; sourceTree = "<group>"; };
C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSPersisentStoreHelperTests.m; path = "Unit Tests/NSPersisentStoreHelperTests.m"; sourceTree = "<group>"; }; C70B6E7013D0F62500709450 /* NSPersisentStoreHelperTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = NSPersisentStoreHelperTests.m; path = "Unit Tests/NSPersisentStoreHelperTests.m"; sourceTree = "<group>"; };
C70B6E7213D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersistentStoreCoordinatorHelperTests.h; path = "Unit Tests/NSPersistentStoreCoordinatorHelperTests.h"; sourceTree = "<group>"; }; C70B6E7213D0F64000709450 /* NSPersistentStoreCoordinatorHelperTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = NSPersistentStoreCoordinatorHelperTests.h; path = "Unit Tests/NSPersistentStoreCoordinatorHelperTests.h"; sourceTree = "<group>"; };
@@ -327,6 +347,25 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
C7005F0E14140F690061B9F4 /* Data Import */ = {
isa = PBXGroup;
children = (
C75A4E7113D0D88D00790CEB /* NSManagedObject+MagicalDataImport.h */,
C75A4E7213D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m */,
C7005F1014140F9B0061B9F4 /* NSNumber+MagicalDataImport.h */,
C7005F1114140F9B0061B9F4 /* NSNumber+MagicalDataImport.m */,
C7005F1414140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.h */,
C7005F1514140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m */,
C7005F1814140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.h */,
C7005F1914140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m */,
C7005F1C1414100F0061B9F4 /* NSDictionary+MagicalDataImport.h */,
C7005F1D1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m */,
C7005F2014153A060061B9F4 /* NSEntityDescription+MagicalDataImport.h */,
C7005F2114153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m */,
);
name = "Data Import";
sourceTree = "<group>";
};
C721C7A013D0A3750097AB6F = { C721C7A013D0A3750097AB6F = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@@ -501,8 +540,7 @@
C721C85813D0C7030097AB6F /* Categories */ = { C721C85813D0C7030097AB6F /* Categories */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
C75A4E7113D0D88D00790CEB /* NSManagedObject+MagicalDataImport.h */, C7005F0E14140F690061B9F4 /* Data Import */,
C75A4E7213D0D88D00790CEB /* NSManagedObject+MagicalDataImport.m */,
C721C85B13D0C7030097AB6F /* NSManagedObject+MagicalRecord.h */, C721C85B13D0C7030097AB6F /* NSManagedObject+MagicalRecord.h */,
C721C85C13D0C7030097AB6F /* NSManagedObject+MagicalRecord.m */, C721C85C13D0C7030097AB6F /* NSManagedObject+MagicalRecord.m */,
C721C85D13D0C7030097AB6F /* NSManagedObjectContext+MagicalRecord.h */, C721C85D13D0C7030097AB6F /* NSManagedObjectContext+MagicalRecord.h */,
@@ -873,6 +911,11 @@
C7BD887013DBF88F00274567 /* SingleEntityWithNoRelationships.m in Sources */, C7BD887013DBF88F00274567 /* SingleEntityWithNoRelationships.m in Sources */,
C7BD887113DBF88F00274567 /* SingleRelatedEntity.m in Sources */, C7BD887113DBF88F00274567 /* SingleRelatedEntity.m in Sources */,
C7BD887213DBF88F00274567 /* TestModel.xcdatamodeld in Sources */, C7BD887213DBF88F00274567 /* TestModel.xcdatamodeld in Sources */,
C7005F1214140F9B0061B9F4 /* NSNumber+MagicalDataImport.m in Sources */,
C7005F1614140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m in Sources */,
C7005F1A14140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m in Sources */,
C7005F1E1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m in Sources */,
C7005F2214153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@@ -929,6 +972,11 @@
C78F8FDA13FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */, C78F8FDA13FDC3F400549DD8 /* SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
C78F8FDD13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */, C78F8FDD13FDC3FD00549DD8 /* _SingleEntityRelatedToMappedEntityWithSecondaryMappings.m in Sources */,
C738127A1410398A0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m in Sources */, C738127A1410398A0054EEF0 /* ImportSingleEntityRelatedToManyMappedEntitiesUsingListOfPrimaryKeysTests.m in Sources */,
C7005F1314140F9B0061B9F4 /* NSNumber+MagicalDataImport.m in Sources */,
C7005F1714140FB60061B9F4 /* NSAttributeDescription+MagicalDataImport.m in Sources */,
C7005F1B14140FD30061B9F4 /* NSRelationshipDescription+MagicalDataImport.m in Sources */,
C7005F1F1414100F0061B9F4 /* NSDictionary+MagicalDataImport.m in Sources */,
C7005F2314153A060061B9F4 /* NSEntityDescription+MagicalDataImport.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -0,0 +1,15 @@
//
// NSAttributeDescription+MagicalDataImport.h
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import <CoreData/CoreData.h>
@interface NSAttributeDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
@end

View File

@@ -0,0 +1,18 @@
//
// NSAttributeDescription+MagicalDataImport.m
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "NSAttributeDescription+MagicalDataImport.h"
@implementation NSAttributeDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
{
return nil;
}
@end

View File

@@ -0,0 +1,19 @@
//
// NSDictionary+MagicalDataImport.h
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDictionary (MagicalRecord_DataImport)
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo;
- (id) MR_valueForAttribute:(NSAttributeDescription *)attributeInfo;
- (NSString *) MR_lookupKeyForRelationship:(NSRelationshipDescription *)relationshipInfo;
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo;
@end

View File

@@ -0,0 +1,66 @@
//
// NSDictionary+MagicalDataImport.m
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "NSDictionary+MagicalDataImport.h"
NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
@implementation NSDictionary (MagicalRecord_DataImport)
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo;
{
NSString *attributeName = [attributeInfo name];
NSString *lookupKey = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportAttributeKeyMapKey] ?: attributeName;
id value = [self valueForKeyPath:lookupKey];
for (int i = 1; i < kMagicalRecordImportMaximumAttributeFailoverDepth && value == nil; i++)
{
attributeName = [NSString stringWithFormat:@"%@.%d", kMagicalRecordImportAttributeKeyMapKey, i];
lookupKey = [[attributeInfo userInfo] valueForKey:attributeName];
if (lookupKey == nil)
{
return nil;
}
value = [self valueForKeyPath:lookupKey];
}
return value != nil ? lookupKey : nil;
}
- (id) MR_valueForAttribute:(NSAttributeDescription *)attributeInfo
{
NSString *lookupKey = [self MR_lookupKeyForAttribute:attributeInfo];
return lookupKey ? [self valueForKeyPath:lookupKey] : nil;
}
- (NSString *) MR_lookupKeyForRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
if (destinationEntity == nil)
{
ARLog(@"Unable to find entity for type '%@'", [self valueForKey:kMagicalRecordImportRelationshipTypeKey]);
return nil;
}
NSString *primaryKeyName = [relationshipInfo MR_primaryKey];
NSAttributeDescription *primaryKeyAttribute = [[destinationEntity attributesByName] valueForKey:primaryKeyName];
NSString *lookupKey = [[primaryKeyAttribute userInfo] valueForKey:kMagicalRecordImportAttributeKeyMapKey] ?: [primaryKeyAttribute name];
return lookupKey;
}
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSString *lookupKey = [self MR_lookupKeyForRelationship:relationshipInfo];
return lookupKey ? [self valueForKeyPath:lookupKey] : nil;
}
@end

View File

@@ -0,0 +1,14 @@
//
// NSEntityDescription+MagicalDataImport.h
// Magical Record
//
// Created by Saul Mora on 9/5/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
@interface NSEntityDescription (MagicalRecord_DataImport)
- (NSAttributeDescription *) MR_primaryKeyAttribute;
@end

View File

@@ -0,0 +1,28 @@
//
// NSEntityDescription+MagicalDataImport.m
// Magical Record
//
// Created by Saul Mora on 9/5/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "CoreData+MagicalRecord.h"
NSString * const kMagicalRecordImportPrimaryAttributeKey = @"primaryAttributeKey";
@implementation NSEntityDescription (MagicalRecord_DataImport)
- (NSAttributeDescription *) MR_primaryKeyAttribute;
{
NSString *lookupKey = [[self userInfo] valueForKey:kMagicalRecordImportPrimaryAttributeKey] ?: primaryKeyNameFromString([self name]);
NSAttributeDescription *primaryAttribute = [[self attributesByName] valueForKey:lookupKey];
if (primaryAttribute == nil)
{
NSAssert3(primaryAttribute != nil, @"Unable to determine primary attribute for %@. Specify either an attribute named %@ or the primary key in userInfo named '%@'", [self name], primaryKeyNameFromString([self name]), kMagicalRecordImportPrimaryAttributeKey);
}
return primaryAttribute;
}
@end

View File

@@ -0,0 +1,16 @@
//
// NSNumber+MagicalDataImport.h
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSNumber (MagicalRecord_DataImport)
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo;
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo;
@end

View File

@@ -0,0 +1,25 @@
//
// NSNumber+MagicalDataImport.m
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "NSNumber+MagicalDataImport.h"
@implementation NSNumber (MagicalRecord_DataImport)
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
return self;
}
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo
{
return nil;
}
@end

View File

@@ -0,0 +1,15 @@
//
// NSRelationshipDescription+MagicalDataImport.h
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import <CoreData/CoreData.h>
@interface NSRelationshipDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
@end

View File

@@ -0,0 +1,21 @@
//
// NSRelationshipDescription+MagicalDataImport.m
// Magical Record
//
// Created by Saul Mora on 9/4/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "NSRelationshipDescription+MagicalDataImport.h"
@implementation NSRelationshipDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
{
NSString *primaryKeyName = [[self userInfo] valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?:
primaryKeyNameFromString([[self destinationEntity] name]);
return primaryKeyName;
}
@end

View File

@@ -19,7 +19,8 @@ extern NSString * const kMagicalRecordImportRelationshipTypeKey;
@interface NSManagedObject (NSManagedObject_DataImport) @interface NSManagedObject (NSManagedObject_DataImport)
- (void) MR_setValuesForKeysWithJSONDictionary:(NSDictionary *)jsonData; - (void) MR_importValuesForKeysWithDictionary:(NSDictionary *)objectData;
- (void) MR_updateValuesForKeysWithDictionary:(NSDictionary *)objectData;
+ (id) MR_importFromDictionary:(NSDictionary *)data; + (id) MR_importFromDictionary:(NSDictionary *)data;
+ (id) MR_importFromDictionary:(NSDictionary *)data inContext:(NSManagedObjectContext *)context; + (id) MR_importFromDictionary:(NSDictionary *)data inContext:(NSManagedObjectContext *)context;
@@ -27,6 +28,9 @@ extern NSString * const kMagicalRecordImportRelationshipTypeKey;
+ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData; + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData;
+ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context; + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context;
+ (id) MR_updateFromDictionary:(NSDictionary *)objectData;
+ (id) MR_updateFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context;
@end @end

View File

@@ -16,89 +16,7 @@ NSString * const kMagicalRecordImportAttributeValueClassNameKey = @"attributeVal
NSString * const kMagicalRecordImportRelationshipMapKey = @"mappedKeyName"; NSString * const kMagicalRecordImportRelationshipMapKey = @"mappedKeyName";
NSString * const kMagicalRecordImportRelationshipPrimaryKey = @"primaryRelationshipKey"; NSString * const kMagicalRecordImportRelationshipPrimaryKey = @"primaryRelationshipKey";
NSString * const kMagicalRecordImportRelationshipTypeKey = @"type"; NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
@implementation NSAttributeDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
{
return nil;
}
@end
@implementation NSRelationshipDescription (MagicalRecord_DataImport)
- (NSString *) MR_primaryKey;
{
NSString *primaryKeyName = [[self userInfo] valueForKey:kMagicalRecordImportRelationshipPrimaryKey] ?:
primaryKeyNameFromString([[self destinationEntity] name]);
return primaryKeyName;
}
@end
@implementation NSDictionary (MagicalRecord_DataImport)
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo;
{
NSString *attributeName = [attributeInfo name];
NSString *lookupKey = [[attributeInfo userInfo] valueForKey:kMagicalRecordImportAttributeKeyMapKey] ?: attributeName;
id value = [self valueForKeyPath:lookupKey];
for (int i = 1; i < kMagicalRecordImportMaximumAttributeFailoverDepth && value == nil; i++)
{
attributeName = [NSString stringWithFormat:@"%@.%d", kMagicalRecordImportAttributeKeyMapKey, i];
lookupKey = [[attributeInfo userInfo] valueForKey:attributeName];
if (lookupKey == nil)
{
return nil;
}
value = [self valueForKeyPath:lookupKey];
}
return value != nil ? lookupKey : nil;
}
- (NSString *) MR_lookupKeyForRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
if (destinationEntity == nil)
{
ARLog(@"Unable to find entity for type '%@'", [self valueForKey:kMagicalRecordImportRelationshipTypeKey]);
return nil;
}
NSString *primaryKeyName = [relationshipInfo MR_primaryKey];
NSAttributeDescription *primaryKeyAttribute = [[destinationEntity attributesByName] valueForKey:primaryKeyName];
NSString *lookupKey = [[primaryKeyAttribute userInfo] valueForKey:kMagicalRecordImportAttributeKeyMapKey] ?: [primaryKeyAttribute name];
return lookupKey;
}
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSString *lookupKey = [self MR_lookupKeyForRelationship:relationshipInfo];
return lookupKey ? [self valueForKeyPath:lookupKey] : nil;
}
@end
@implementation NSNumber (MagicalRecord_DataImport)
- (id) MR_relatedValueForRelationship:(NSRelationshipDescription *)relationshipInfo
{
return self;
}
- (NSString *) MR_lookupKeyForAttribute:(NSAttributeDescription *)attributeInfo
{
return nil;
}
@end
@implementation NSManagedObject (MagicalRecord_DataImport) @implementation NSManagedObject (MagicalRecord_DataImport)
@@ -152,7 +70,7 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
NSManagedObject *relatedObject = [NSEntityDescription insertNewObjectForEntityForName:[entityDescription name] NSManagedObject *relatedObject = [NSEntityDescription insertNewObjectForEntityForName:[entityDescription name]
inManagedObjectContext:[self managedObjectContext]]; inManagedObjectContext:[self managedObjectContext]];
[relatedObject MR_setValuesForKeysWithJSONDictionary:objectData]; [relatedObject MR_importValuesForKeysWithDictionary:objectData];
return relatedObject; return relatedObject;
} }
@@ -200,7 +118,7 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
} }
} }
- (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary:(NSDictionary *)relationshipData withBlock:(void(^)(NSRelationshipDescription *,NSDictionary *))setRelationshipBlock - (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary:(NSDictionary *)relationshipData withBlock:(void(^)(NSRelationshipDescription *,id))setRelationshipBlock
{ {
for (NSString *relationshipName in relationships) for (NSString *relationshipName in relationships)
{ {
@@ -215,7 +133,7 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
continue; continue;
} }
if ([relationshipInfo isToMany] ) //|| [relatedObjectData isKindOfClass:[NSArray class]]) if ([relationshipInfo isToMany])
{ {
for (id singleRelatedObjectData in relatedObjectData) for (id singleRelatedObjectData in relatedObjectData)
{ {
@@ -229,11 +147,40 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
} }
} }
- (void) MR_setRelationships:(NSDictionary *)relationships forKeysWithDictionary:(NSDictionary *)relationshipData - (void) MR_importValuesForKeysWithDictionary:(NSDictionary *)objectData
{ {
NSDictionary *attributes = [[self entity] attributesByName];
[self MR_setAttributes:attributes forKeysWithDictionary:objectData];
NSDictionary *relationships = [[self entity] relationshipsByName];
[self MR_setRelationships:relationships [self MR_setRelationships:relationships
forKeysWithDictionary:relationshipData forKeysWithDictionary:objectData
withBlock:^(NSRelationshipDescription *relationshipInfo, NSDictionary *objectData) withBlock:^(NSRelationshipDescription *relationshipInfo, id objectData)
{
NSManagedObject *relatedObject = nil;
if ([objectData isKindOfClass:[NSDictionary class]])
{
relatedObject = [self MR_createInstanceForEntity:[relationshipInfo destinationEntity] withDictionary:objectData];
}
else
{
relatedObject = [self MR_findObjectForRelationship:relationshipInfo withData:objectData];
}
[relatedObject MR_importValuesForKeysWithDictionary:objectData];
[self MR_addObject:relatedObject forRelationship:relationshipInfo];
}];
}
- (void) MR_updateValuesForKeysWithDictionary:(NSDictionary *)objectData
{
NSDictionary *attributes = [[self entity] attributesByName];
[self MR_setAttributes:attributes forKeysWithDictionary:objectData];
NSDictionary *relationships = [[self entity] relationshipsByName];
[self MR_setRelationships:relationships
forKeysWithDictionary:objectData
withBlock:^(NSRelationshipDescription *relationshipInfo, id objectData)
{ {
NSManagedObject *relatedObject = [self MR_findObjectForRelationship:relationshipInfo NSManagedObject *relatedObject = [self MR_findObjectForRelationship:relationshipInfo
withData:objectData]; withData:objectData];
@@ -244,26 +191,17 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
} }
else else
{ {
[relatedObject MR_setValuesForKeysWithJSONDictionary:objectData]; [relatedObject MR_importValuesForKeysWithDictionary:objectData];
} }
[self MR_addObject:relatedObject forRelationship:relationshipInfo]; [self MR_addObject:relatedObject forRelationship:relationshipInfo];
}]; }];
} }
- (void) MR_setValuesForKeysWithJSONDictionary:(NSDictionary *)objectData
{
NSDictionary *attributes = [[self entity] attributesByName];
[self MR_setAttributes:attributes forKeysWithDictionary:objectData];
NSDictionary *relationships = [[self entity] relationshipsByName];
[self MR_setRelationships:relationships forKeysWithDictionary:objectData];
}
+ (id) MR_importFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context; + (id) MR_importFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context;
{ {
NSManagedObject *managedObject = [self createInContext:context]; NSManagedObject *managedObject = [self createInContext:context];
[managedObject MR_setValuesForKeysWithJSONDictionary:objectData]; [managedObject MR_importValuesForKeysWithDictionary:objectData];
return managedObject; return managedObject;
} }
@@ -274,10 +212,21 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
+ (id) MR_updateFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context + (id) MR_updateFromDictionary:(NSDictionary *)objectData inContext:(NSManagedObjectContext *)context
{ {
//find object NSAttributeDescription *primaryAttribute = [[self entityDescription] MR_primaryKeyAttribute];
//create if not exists
//apply dictionary updates id value = [objectData MR_valueForAttribute:primaryAttribute];
return nil;
NSManagedObject *manageObject = [self findFirstByAttribute:[primaryAttribute name] withValue:value inContext:context];
if (!manageObject)
{
manageObject = [self createInContext:context];
[manageObject MR_importValuesForKeysWithDictionary:objectData];
}
else
{
[manageObject MR_updateValuesForKeysWithDictionary:objectData];
}
return manageObject;
} }
+ (id) MR_updateFromDictionary:(NSDictionary *)objectData + (id) MR_updateFromDictionary:(NSDictionary *)objectData
@@ -287,7 +236,7 @@ NSUInteger const kMagicalRecordImportMaximumAttributeFailoverDepth = 10;
+ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData
{ {
return [self MR_importFromArray:listOfObjectData inContext:[NSManagedObjectContext context]]; return [self MR_importFromArray:listOfObjectData inContext:[NSManagedObjectContext defaultContext]];
} }
+ (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context

View File

@@ -23,4 +23,9 @@
#import "NSManagedObjectModel+MagicalRecord.h" #import "NSManagedObjectModel+MagicalRecord.h"
#import "NSPersistentStore+MagicalRecord.h" #import "NSPersistentStore+MagicalRecord.h"
#import "NSManagedObject+MagicalDataImport.h" #import "NSManagedObject+MagicalDataImport.h"
#import "NSNumber+MagicalDataImport.h"
#import "NSDictionary+MagicalDataImport.h"
#import "NSAttributeDescription+MagicalDataImport.h"
#import "NSRelationshipDescription+MagicalDataImport.h"
#import "NSEntityDescription+MagicalDataImport.h"

View File

@@ -24,19 +24,27 @@
- (void) setupTestData - (void) setupTestData
{ {
NSManagedObjectContext *context = [NSManagedObjectContext defaultContext]; NSManagedObjectContext *context = [NSManagedObjectContext defaultContext];
MappedEntity *related = nil;
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
MappedEntity *testMappedEntity = [MappedEntity createInContext:context]; MappedEntity *testMappedEntity = [MappedEntity createInContext:context];
testMappedEntity.testMappedEntityIDValue = i; testMappedEntity.testMappedEntityIDValue = i;
testMappedEntity.sampleAttribute = [NSString stringWithFormat:@"test attribute %d", i]; testMappedEntity.sampleAttribute = [NSString stringWithFormat:@"test attribute %d", i];
related = testMappedEntity;
} }
SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *entity = [SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey createInContext:context];
entity.testPrimaryKeyValue = 84;
[entity addMappedEntitiesObject:related];
[context save]; [context save];
} }
- (void) testDataImport - (void) testDataImport
{ {
SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *testEntity = (SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *)self.testEntity; SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *testEntity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
assertThat(testEntity.mappedEntities, hasCountOf(4)); assertThat(testEntity.mappedEntities, hasCountOf(4));
for (MappedEntity *relatedEntity in testEntity.mappedEntities) for (MappedEntity *relatedEntity in testEntity.mappedEntities)
@@ -44,7 +52,31 @@
assertThat(relatedEntity.sampleAttribute, containsString(@"test attribute")); assertThat(relatedEntity.sampleAttribute, containsString(@"test attribute"));
} }
assertThat([SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(1))); assertThat([SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(2)));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(10))); assertThat([MappedEntity numberOfEntities], is(equalToInteger(10)));
} }
- (void) testDataUpdateWithLookupInfoInDataSet
{
SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *testEntity = [[self testEntityClass] MR_updateFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
assertThat([SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(1)));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(10)));
assertThat(testEntity, is(notNilValue()));
assertThat(testEntity.testPrimaryKey, is(equalToInteger(84)));
assertThat(testEntity.mappedEntities, hasCountOf(5));
}
- (void) testDataUpdateWithoutLookupData
{
SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *testEntity =
[SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey findFirstByAttribute:@"testPrimaryKey" withValue:[NSNumber numberWithInt:84]];
assertThat(testEntity, is(notNilValue()));
[testEntity MR_updateValuesForKeysWithDictionary:self.testEntityData];
}
@end @end

View File

@@ -1,4 +1,4 @@
{ {
"mappedEntities": [1,2,3,4], "mappedEntities": [1,2,3,4],
"sampleAttribute": "Imported from sample json file" "testPrimaryKey": 84
} }

View File

@@ -7,6 +7,7 @@
@class MappedEntity; @class MappedEntity;
@interface SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyID : NSManagedObjectID {} @interface SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKeyID : NSManagedObjectID {}
@end @end
@@ -19,6 +20,18 @@
@property (nonatomic, retain) NSNumber *testPrimaryKey;
@property short testPrimaryKeyValue;
- (short)testPrimaryKeyValue;
- (void)setTestPrimaryKeyValue:(short)value_;
//- (BOOL)validateTestPrimaryKey:(id*)value_ error:(NSError**)error_;
@property (nonatomic, retain) NSSet* mappedEntities; @property (nonatomic, retain) NSSet* mappedEntities;
@@ -41,6 +54,15 @@
@interface _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey (CoreDataGeneratedPrimitiveAccessors) @interface _SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber*)primitiveTestPrimaryKey;
- (void)setPrimitiveTestPrimaryKey:(NSNumber*)value;
- (short)primitiveTestPrimaryKeyValue;
- (void)setPrimitiveTestPrimaryKeyValue:(short)value_;
- (NSMutableSet*)primitiveMappedEntities; - (NSMutableSet*)primitiveMappedEntities;
- (void)setPrimitiveMappedEntities:(NSMutableSet*)value; - (void)setPrimitiveMappedEntities:(NSMutableSet*)value;

View File

@@ -29,6 +29,10 @@
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"testPrimaryKeyValue"]) {
NSSet *affectingKey = [NSSet setWithObject:@"testPrimaryKey"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
}
return keyPaths; return keyPaths;
} }
@@ -36,6 +40,32 @@
@dynamic testPrimaryKey;
- (short)testPrimaryKeyValue {
NSNumber *result = [self testPrimaryKey];
return [result shortValue];
}
- (void)setTestPrimaryKeyValue:(short)value_ {
[self setTestPrimaryKey:[NSNumber numberWithShort:value_]];
}
- (short)primitiveTestPrimaryKeyValue {
NSNumber *result = [self primitiveTestPrimaryKey];
return [result shortValue];
}
- (void)setPrimitiveTestPrimaryKeyValue:(short)value_ {
[self setPrimitiveTestPrimaryKey:[NSNumber numberWithShort:value_]];
}
@dynamic mappedEntities; @dynamic mappedEntities;

View File

@@ -7,6 +7,7 @@
@class MappedEntity; @class MappedEntity;
@interface SingleEntityRelatedToMappedEntityUsingDefaultsID : NSManagedObjectID {} @interface SingleEntityRelatedToMappedEntityUsingDefaultsID : NSManagedObjectID {}
@end @end
@@ -19,6 +20,18 @@
@property (nonatomic, retain) NSNumber *singleEntityRelatedToMappedEntityUsingDefaultsID;
@property short singleEntityRelatedToMappedEntityUsingDefaultsIDValue;
- (short)singleEntityRelatedToMappedEntityUsingDefaultsIDValue;
- (void)setSingleEntityRelatedToMappedEntityUsingDefaultsIDValue:(short)value_;
//- (BOOL)validateSingleEntityRelatedToMappedEntityUsingDefaultsID:(id*)value_ error:(NSError**)error_;
@property (nonatomic, retain) MappedEntity* mappedEntity; @property (nonatomic, retain) MappedEntity* mappedEntity;
@@ -36,6 +49,15 @@
@interface _SingleEntityRelatedToMappedEntityUsingDefaults (CoreDataGeneratedPrimitiveAccessors) @interface _SingleEntityRelatedToMappedEntityUsingDefaults (CoreDataGeneratedPrimitiveAccessors)
- (NSNumber*)primitiveSingleEntityRelatedToMappedEntityUsingDefaultsID;
- (void)setPrimitiveSingleEntityRelatedToMappedEntityUsingDefaultsID:(NSNumber*)value;
- (short)primitiveSingleEntityRelatedToMappedEntityUsingDefaultsIDValue;
- (void)setPrimitiveSingleEntityRelatedToMappedEntityUsingDefaultsIDValue:(short)value_;
- (MappedEntity*)primitiveMappedEntity; - (MappedEntity*)primitiveMappedEntity;
- (void)setPrimitiveMappedEntity:(MappedEntity*)value; - (void)setPrimitiveMappedEntity:(MappedEntity*)value;

View File

@@ -29,6 +29,10 @@
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key {
NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key];
if ([key isEqualToString:@"singleEntityRelatedToMappedEntityUsingDefaultsIDValue"]) {
NSSet *affectingKey = [NSSet setWithObject:@"singleEntityRelatedToMappedEntityUsingDefaultsID"];
keyPaths = [keyPaths setByAddingObjectsFromSet:affectingKey];
}
return keyPaths; return keyPaths;
} }
@@ -36,6 +40,32 @@
@dynamic singleEntityRelatedToMappedEntityUsingDefaultsID;
- (short)singleEntityRelatedToMappedEntityUsingDefaultsIDValue {
NSNumber *result = [self singleEntityRelatedToMappedEntityUsingDefaultsID];
return [result shortValue];
}
- (void)setSingleEntityRelatedToMappedEntityUsingDefaultsIDValue:(short)value_ {
[self setSingleEntityRelatedToMappedEntityUsingDefaultsID:[NSNumber numberWithShort:value_]];
}
- (short)primitiveSingleEntityRelatedToMappedEntityUsingDefaultsIDValue {
NSNumber *result = [self primitiveSingleEntityRelatedToMappedEntityUsingDefaultsID];
return [result shortValue];
}
- (void)setPrimitiveSingleEntityRelatedToMappedEntityUsingDefaultsIDValue:(short)value_ {
[self setPrimitiveSingleEntityRelatedToMappedEntityUsingDefaultsID:[NSNumber numberWithShort:value_]];
}
@dynamic mappedEntity; @dynamic mappedEntity;

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="864" systemVersion="11B26" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic"> <model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="562" systemVersion="11B26" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="AbstractRelatedEntity" representedClassName="AbstractRelatedEntity" isAbstract="YES"> <entity name="AbstractRelatedEntity" representedClassName="AbstractRelatedEntity" isAbstract="YES">
<attribute name="sampleBaseAttribute" optional="YES" attributeType="String"/> <attribute name="sampleBaseAttribute" optional="YES" attributeType="String"/>
</entity> </entity>
@@ -20,15 +20,23 @@
<entry key="mappedKeyName" value="id"/> <entry key="mappedKeyName" value="id"/>
</userInfo> </userInfo>
</attribute> </attribute>
<userInfo>
<entry key="primaryAttributeKey" value="mapped"/>
</userInfo>
</entity> </entity>
<entity name="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey" representedClassName="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey"> <entity name="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey" representedClassName="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey">
<attribute name="testPrimaryKey" optional="YES" attributeType="Integer 16" defaultValueString="0"/>
<relationship name="mappedEntities" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MappedEntity"> <relationship name="mappedEntities" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MappedEntity">
<userInfo> <userInfo>
<entry key="primaryRelationshipKey" value="testMappedEntityID"/> <entry key="primaryRelationshipKey" value="testMappedEntityID"/>
</userInfo> </userInfo>
</relationship> </relationship>
<userInfo>
<entry key="primaryAttributeKey" value="testPrimaryKey"/>
</userInfo>
</entity> </entity>
<entity name="SingleEntityRelatedToMappedEntityUsingDefaults" representedClassName="SingleEntityRelatedToMappedEntityUsingDefaults"> <entity name="SingleEntityRelatedToMappedEntityUsingDefaults" representedClassName="SingleEntityRelatedToMappedEntityUsingDefaults">
<attribute name="singleEntityRelatedToMappedEntityUsingDefaultsID" optional="YES" attributeType="Integer 16" defaultValueString="0"/>
<relationship name="mappedEntity" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MappedEntity"/> <relationship name="mappedEntity" optional="YES" minCount="1" maxCount="1" deletionRule="Nullify" destinationEntity="MappedEntity"/>
</entity> </entity>
<entity name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" representedClassName="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey"> <entity name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" representedClassName="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey">
@@ -99,7 +107,7 @@
<element name="EntityWithDiffernentClassName" positionX="-135" positionY="135" width="198" height="45"/> <element name="EntityWithDiffernentClassName" positionX="-135" positionY="135" width="198" height="45"/>
<element name="MappedEntity" positionX="421" positionY="558" width="128" height="105"/> <element name="MappedEntity" positionX="421" positionY="558" width="128" height="105"/>
<element name="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey" positionX="657" positionY="450" width="351" height="60"/> <element name="SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey" positionX="657" positionY="450" width="351" height="60"/>
<element name="SingleEntityRelatedToMappedEntityUsingDefaults" positionX="621" positionY="333" width="272" height="60"/> <element name="SingleEntityRelatedToMappedEntityUsingDefaults" positionX="621" positionY="333" width="297" height="75"/>
<element name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" positionX="603" positionY="702" width="342" height="60"/> <element name="SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey" positionX="603" positionY="702" width="342" height="60"/>
<element name="SingleEntityRelatedToMappedEntityWithNestedMappedAttributes" positionX="664" positionY="597" width="470" height="60"/> <element name="SingleEntityRelatedToMappedEntityWithNestedMappedAttributes" positionX="664" positionY="597" width="470" height="60"/>
<element name="SingleEntityRelatedToMappedEntityWithSecondaryMappings" positionX="-63" positionY="738" width="344" height="75"/> <element name="SingleEntityRelatedToMappedEntityWithSecondaryMappings" positionX="-63" positionY="738" width="344" height="75"/>

View File

@@ -22,7 +22,8 @@
- (void) testImportData - (void) testImportData
{ {
SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *entity = (SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *)self.testEntity; SingleEntityRelatedToManyMappedEntitiesUsingMappedPrimaryKey *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
assertThat(entity, is(notNilValue())); assertThat(entity, is(notNilValue()));
assertThat(entity.mappedEntities, hasCountOf(4)); assertThat(entity.mappedEntities, hasCountOf(4));

View File

@@ -29,18 +29,37 @@
testMappedEntity.mappedEntityIDValue = 42; testMappedEntity.mappedEntityIDValue = 42;
testMappedEntity.sampleAttribute = @"This attribute created as part of the test case setup"; testMappedEntity.sampleAttribute = @"This attribute created as part of the test case setup";
SingleEntityRelatedToMappedEntityUsingDefaults *entity = [SingleEntityRelatedToMappedEntityUsingDefaults createInContext:context];
entity.singleEntityRelatedToMappedEntityUsingDefaultsIDValue = 24;
[context save]; [context save];
} }
- (void) testImportMappedEntityViaToOneRelationship - (void) testImportMappedEntityViaToOneRelationship
{ {
SingleEntityRelatedToMappedEntityUsingDefaults *entity = (SingleEntityRelatedToMappedEntityUsingDefaults *)self.testEntity; SingleEntityRelatedToMappedEntityUsingDefaults *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
id testRelatedEntity = entity.mappedEntity; id testRelatedEntity = entity.mappedEntity;
assertThat(testRelatedEntity, is(notNilValue())); assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file")); assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file"));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(2)));
}
- (void) testUpdateMappedEntity
{
SingleEntityRelatedToMappedEntityUsingDefaults *testEntity =
[SingleEntityRelatedToMappedEntityUsingDefaults findFirstByAttribute:@"singleEntityRelatedToMappedEntityUsingDefaultsID" withValue:[NSNumber numberWithInt:24]];
[testEntity MR_updateValuesForKeysWithDictionary:self.testEntityData];
assertThat([MappedEntity numberOfEntities], is(equalToInteger(1))); assertThat([MappedEntity numberOfEntities], is(equalToInteger(1)));
assertThat(testEntity, is(notNilValue()));
} }
@end @end

View File

@@ -34,7 +34,9 @@
- (void) testImportMappedEntityRelatedViaToOneRelationship - (void) testImportMappedEntityRelatedViaToOneRelationship
{ {
SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = (SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *)self.testEntity; SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
id testRelatedEntity = entity.mappedEntity; id testRelatedEntity = entity.mappedEntity;
//verify mapping in relationship description userinfo //verify mapping in relationship description userinfo
@@ -42,14 +44,55 @@
NSRelationshipDescription *testRelationship = [[mappedEntity propertiesByName] valueForKey:@"mappedEntity"]; NSRelationshipDescription *testRelationship = [[mappedEntity propertiesByName] valueForKey:@"mappedEntity"];
assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipMapKey], is(equalTo(@"someRandomAttributeName"))); assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipMapKey], is(equalTo(@"someRandomAttributeName")));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(1))); assertThat([SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(1)));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(2)));
assertThat(testRelatedEntity, is(notNilValue())); assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleAttribute], is(containsString(@"sample json file"))); assertThat([testRelatedEntity sampleAttribute], is(containsString(@"sample json file")));
} }
- (void) testUpdateMappedEntityRelatedViaToOneRelationship
{
SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = [SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey createEntity];
[entity MR_updateValuesForKeysWithDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
id testRelatedEntity = entity.mappedEntity;
//verify mapping in relationship description userinfo
NSEntityDescription *mappedEntity = [entity entity];
NSRelationshipDescription *testRelationship = [[mappedEntity propertiesByName] valueForKey:@"mappedEntity"];
assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipMapKey], is(equalTo(@"someRandomAttributeName")));
assertThat([SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(1)));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(1)));
assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleAttribute], is(containsString(@"sample json file")));
}
- (void) testImportMappedEntityUsingPrimaryRelationshipKey - (void) testImportMappedEntityUsingPrimaryRelationshipKey
{ {
SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = (SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *)self.testEntity; SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
id testRelatedEntity = entity.mappedEntity;
//verify mapping in relationship description userinfo
NSEntityDescription *mappedEntity = [entity entity];
NSRelationshipDescription *testRelationship = [[mappedEntity propertiesByName] valueForKey:@"mappedEntity"];
assertThat([[testRelationship userInfo] valueForKey:kMagicalRecordImportRelationshipPrimaryKey], is(equalTo(@"testMappedEntityID")));
assertThat([SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey numberOfEntities], is(equalToInteger(1)));
assertThat([MappedEntity numberOfEntities], is(equalToInteger(2)));
assertThat([testRelatedEntity testMappedEntityID], is(equalToInteger(42)));
assertThat([testRelatedEntity sampleAttribute], containsString(@"sample json file"));
}
- (void) testUpdateMappedEntityUsingPrimaryRelationshipKey
{
SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey *entity = [SingleEntityRelatedToMappedEntityUsingMappedPrimaryKey createEntity];
[entity MR_updateValuesForKeysWithDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
id testRelatedEntity = entity.mappedEntity; id testRelatedEntity = entity.mappedEntity;
//verify mapping in relationship description userinfo //verify mapping in relationship description userinfo

View File

@@ -24,7 +24,8 @@
- (void) testDataImport - (void) testDataImport
{ {
SingleEntityRelatedToMappedEntityWithNestedMappedAttributes *entity = (SingleEntityRelatedToMappedEntityWithNestedMappedAttributes *)self.testEntity; SingleEntityRelatedToMappedEntityWithNestedMappedAttributes *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
assertThat(entity.mappedEntity, is(notNilValue())); assertThat(entity.mappedEntity, is(notNilValue()));
assertThat(entity.mappedEntity.mappedEntityID, is(equalToInteger(42))); assertThat(entity.mappedEntity.mappedEntityID, is(equalToInteger(42)));

View File

@@ -22,8 +22,9 @@
- (void) testImportMappedAttributeUsingSecondaryMappedKeyName - (void) testImportMappedAttributeUsingSecondaryMappedKeyName
{ {
SingleEntityRelatedToMappedEntityWithSecondaryMappings *entity = (SingleEntityRelatedToMappedEntityWithSecondaryMappings *)self.testEntity; SingleEntityRelatedToMappedEntityWithSecondaryMappings *entity = [[self testEntityClass] MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
assertThat(entity, is(notNilValue())); assertThat(entity, is(notNilValue()));
assertThat([entity secondaryMappedAttribute], containsString(@"sample json file")); assertThat([entity secondaryMappedAttribute], containsString(@"sample json file"));
} }

View File

@@ -10,8 +10,9 @@
#import "AbstractRelatedEntity.h" #import "AbstractRelatedEntity.h"
#import "ConcreteRelatedEntity.h" #import "ConcreteRelatedEntity.h"
#import "MappedEntity.h" #import "MappedEntity.h"
#import "MagicalDataImportTestCase.h"
@interface ImportSingleRelatedEntityTests : GHTestCase @interface ImportSingleRelatedEntityTests : MagicalDataImportTestCase
@property (nonatomic, retain) SingleRelatedEntity *testEntity; @property (nonatomic, retain) SingleRelatedEntity *testEntity;
@@ -32,21 +33,28 @@
[context save]; [context save];
} }
- (void) setUpClass //- (void) setUpClass
{ //{
[NSManagedObjectModel setDefaultManagedObjectModel:[NSManagedObjectModel managedObjectModelNamed:@"TestModel.momd"]]; // [NSManagedObjectModel setDefaultManagedObjectModel:[NSManagedObjectModel managedObjectModelNamed:@"TestModel.momd"]];
[MagicalRecordHelpers setupCoreDataStackWithInMemoryStore]; // [MagicalRecordHelpers setupCoreDataStackWithInMemoryStore];
//
[self setupTestData]; // [self setupTestData];
//
id singleEntity = [self dataFromJSONFixture]; // id singleEntity = [self dataFromJSONFixture];
//
self.testEntity = [SingleRelatedEntity MR_importFromDictionary:singleEntity]; // self.testEntity = [SingleRelatedEntity MR_importFromDictionary:singleEntity];
} //}
- (void) tearDownClass //- (void) tearDownClass
//{
// [MagicalRecordHelpers cleanUp];
//}
- (void) setUp
{ {
[MagicalRecordHelpers cleanUp]; [super setUp];
self.testEntity = [SingleRelatedEntity MR_importFromDictionary:self.testEntityData];
[[NSManagedObjectContext defaultContext] save];
} }
- (void) testImportAnEntityRelatedToAbstractEntityViaToOneRelationshop - (void) testImportAnEntityRelatedToAbstractEntityViaToOneRelationshop

View File

@@ -10,7 +10,8 @@
@interface MagicalDataImportTestCase : GHTestCase @interface MagicalDataImportTestCase : GHTestCase
@property (nonatomic, retain) NSManagedObject *testEntity; @property (nonatomic, retain) id testEntityData;
@property (nonatomic, retain) id testEntity;
- (Class) testEntityClass; - (Class) testEntityClass;

View File

@@ -10,6 +10,7 @@
@implementation MagicalDataImportTestCase @implementation MagicalDataImportTestCase
@synthesize testEntityData = testEntityData__;
@synthesize testEntity = testEntity__; @synthesize testEntity = testEntity__;
- (void) setUp - (void) setUp
@@ -22,11 +23,7 @@
[self performSelector:@selector(setupTestData)]; [self performSelector:@selector(setupTestData)];
} }
id singleEntity = [self dataFromJSONFixture]; self.testEntityData = [self dataFromJSONFixture];
self.testEntity = [[self testEntityClass] MR_importFromDictionary:singleEntity];
[[NSManagedObjectContext defaultContext] save];
} }
- (void) tearDown - (void) tearDown