Implement tests for entity relationship importing

This commit is contained in:
Saul Mora
2011-08-08 18:08:52 -06:00
parent 23ae6474f4
commit f8153c8653
10 changed files with 178 additions and 56 deletions

View File

@@ -946,6 +946,8 @@
"$(inherited)",
"\"$(SRCROOT)/iOS App Unit Tests/Frameworks/OCMockLibrary\"",
);
MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS = YES;
MOMC_NO_WARNINGS = YES;
OTHER_LDFLAGS = (
"-all_load",
"-ObjC",
@@ -984,6 +986,8 @@
"$(inherited)",
"\"$(SRCROOT)/iOS App Unit Tests/Frameworks/OCMockLibrary\"",
);
MOMC_NO_INVERSE_RELATIONSHIP_WARNINGS = YES;
MOMC_NO_WARNINGS = YES;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
OTHER_LDFLAGS = (
"-all_load",

View File

@@ -73,9 +73,20 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
{
NSEntityDescription *originalDestinationEntity = [relationshipInfo destinationEntity];
NSDictionary *subentities = [originalDestinationEntity subentitiesByName];
NSEntityDescription *destinationEntity = [subentities count] /* && ![entityDescription isAbstract]*/ ?
[subentities valueForKey:[singleRelatedObjectData valueForKey:kMagicalRecordImportRelationshipTypeKey]] :
originalDestinationEntity;
NSEntityDescription *destinationEntity = originalDestinationEntity;
NSDictionary *relationshipUserInfo = [relationshipInfo userInfo];
NSString *mappedEntityName = [relationshipUserInfo valueForKey:kMagicalRecordImportRelationshipTypeKey];
if (mappedEntityName)
{
destinationEntity = [NSEntityDescription entityForName:mappedEntityName inManagedObjectContext:self.managedObjectContext];
}
else if ([originalDestinationEntity isAbstract] && [subentities count])
{
// NSString *mappedSubentity = [singleRelatedObjectData valueForKey:kMagicalRecordImportRelationshipTypeKey];
// [subentities valueForKey:mappedSubentity];
}
if (destinationEntity == nil)
{
@@ -95,6 +106,8 @@ NSString * const kMagicalRecordImportRelationshipTypeKey = @"type";
- (void) MR_addObject:(NSManagedObject *)relatedObject forRelationship:(NSRelationshipDescription *)relationshipInfo
{
NSAssert2(relatedObject != nil, @"Cannot add nil to %@ for attribute %@", NSStringFromClass([self class]), [relationshipInfo name]);
//add related object to set
NSString *addRelationMessageFormat = [relationshipInfo isToMany] ? @"add%@Object:" : @"set%@:";
NSString *addRelatedObjectToSetMessage = [NSString stringWithFormat:addRelationMessageFormat, attributeNameFromString([relationshipInfo name])];

View File

@@ -1,7 +1,44 @@
{
"testRelationship":
"testAbstractToOneRelationship":
{
"sampleBaseAttribute": "This should be imported as: BASE ATTRIBUTE TEST",
"sampleConcreteAttribute": "This should be imported as: DECENDANT ATTRIBUTE TEST"
}
,
"testAbstractToManyRelationship":
[
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST 1",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST 1"
}
,
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST 2",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST 2"
}
]
,
"testConcreteToOneRelationship":
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST"
}
,
"testConcreteToManyRelationship":
[
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST 1",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST 1"
}
,
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST 2",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST 2"
}
,
{
"sampleBaseAttribute": "Import as BASE ATTRIBUTE TEST 3",
"sampleConcreteAttribute": "Import as DECENDANT ATTRIBUTE TEST 3"
}
]
}

View File

@@ -4,7 +4,6 @@
#import <CoreData/CoreData.h>
@class SingleRelatedEntity;
@@ -29,13 +28,6 @@
@property (nonatomic, retain) SingleRelatedEntity* mainTestEntity;
//- (BOOL)validateMainTestEntity:(id*)value_ error:(NSError**)error_;
@end
@interface _AbstractRelatedEntity (CoreDataGeneratedAccessors)
@@ -51,9 +43,4 @@
- (SingleRelatedEntity*)primitiveMainTestEntity;
- (void)setPrimitiveMainTestEntity:(SingleRelatedEntity*)value;
@end

View File

@@ -43,10 +43,6 @@
@dynamic mainTestEntity;

View File

@@ -5,6 +5,9 @@
@class AbstractRelatedEntity;
@class AbstractRelatedEntity;
@class ConcreteRelatedEntity;
@class ConcreteRelatedEntity;
@interface SingleRelatedEntityID : NSManagedObjectID {}
@@ -20,9 +23,30 @@
@property (nonatomic, retain) AbstractRelatedEntity* testRelationship;
@property (nonatomic, retain) NSSet* testAbstractToManyRelationship;
//- (BOOL)validateTestRelationship:(id*)value_ error:(NSError**)error_;
- (NSMutableSet*)testAbstractToManyRelationshipSet;
@property (nonatomic, retain) AbstractRelatedEntity* testAbstractToOneRelationship;
//- (BOOL)validateTestAbstractToOneRelationship:(id*)value_ error:(NSError**)error_;
@property (nonatomic, retain) NSSet* testConcreteToManyRelationship;
- (NSMutableSet*)testConcreteToManyRelationshipSet;
@property (nonatomic, retain) ConcreteRelatedEntity* testConcreteToOneRelationship;
//- (BOOL)validateTestConcreteToOneRelationship:(id*)value_ error:(NSError**)error_;
@@ -31,14 +55,39 @@
@interface _SingleRelatedEntity (CoreDataGeneratedAccessors)
- (void)addTestAbstractToManyRelationship:(NSSet*)value_;
- (void)removeTestAbstractToManyRelationship:(NSSet*)value_;
- (void)addTestAbstractToManyRelationshipObject:(AbstractRelatedEntity*)value_;
- (void)removeTestAbstractToManyRelationshipObject:(AbstractRelatedEntity*)value_;
- (void)addTestConcreteToManyRelationship:(NSSet*)value_;
- (void)removeTestConcreteToManyRelationship:(NSSet*)value_;
- (void)addTestConcreteToManyRelationshipObject:(ConcreteRelatedEntity*)value_;
- (void)removeTestConcreteToManyRelationshipObject:(ConcreteRelatedEntity*)value_;
@end
@interface _SingleRelatedEntity (CoreDataGeneratedPrimitiveAccessors)
- (AbstractRelatedEntity*)primitiveTestRelationship;
- (void)setPrimitiveTestRelationship:(AbstractRelatedEntity*)value;
- (NSMutableSet*)primitiveTestAbstractToManyRelationship;
- (void)setPrimitiveTestAbstractToManyRelationship:(NSMutableSet*)value;
- (AbstractRelatedEntity*)primitiveTestAbstractToOneRelationship;
- (void)setPrimitiveTestAbstractToOneRelationship:(AbstractRelatedEntity*)value;
- (NSMutableSet*)primitiveTestConcreteToManyRelationship;
- (void)setPrimitiveTestConcreteToManyRelationship:(NSMutableSet*)value;
- (ConcreteRelatedEntity*)primitiveTestConcreteToOneRelationship;
- (void)setPrimitiveTestConcreteToOneRelationship:(ConcreteRelatedEntity*)value;
@end

View File

@@ -36,7 +36,33 @@
@dynamic testRelationship;
@dynamic testAbstractToManyRelationship;
- (NSMutableSet*)testAbstractToManyRelationshipSet {
[self willAccessValueForKey:@"testAbstractToManyRelationship"];
NSMutableSet *result = (NSMutableSet*)[self mutableSetValueForKey:@"testAbstractToManyRelationship"];
[self didAccessValueForKey:@"testAbstractToManyRelationship"];
return result;
}
@dynamic testAbstractToOneRelationship;
@dynamic testConcreteToManyRelationship;
- (NSMutableSet*)testConcreteToManyRelationshipSet {
[self willAccessValueForKey:@"testConcreteToManyRelationship"];
NSMutableSet *result = (NSMutableSet*)[self mutableSetValueForKey:@"testConcreteToManyRelationship"];
[self didAccessValueForKey:@"testConcreteToManyRelationship"];
return result;
}
@dynamic testConcreteToOneRelationship;

View File

@@ -20,7 +20,7 @@
- (void) setUp
{
[NSManagedObjectModel setDefaultManagedObjectModel:[NSManagedObjectModel managedObjectModelNamed:@"TestModel.momd"]];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel MR_managedObjectModelNamed:@"TestModel.momd"]];
[MagicalRecordHelpers setupCoreDataStackWithInMemoryStore];
id singleEntity = [FixtureHelpers dataFromPListFixtureNamed:@"SingleEntityWithNoRelationships"];

View File

@@ -19,55 +19,65 @@
@synthesize testEntity;
- (void) setUp
- (void) setUpClass
{
[NSManagedObjectModel setDefaultManagedObjectModel:[NSManagedObjectModel managedObjectModelNamed:@"TestModel.momd"]];
[MagicalRecordHelpers setupCoreDataStackWithInMemoryStore];
id singleEntity = [FixtureHelpers dataFromJSONFixtureNamed:@"SingleRelatedEntity"];
testEntity = [SingleRelatedEntity MR_importFromDictionary:singleEntity];
self.testEntity = [SingleRelatedEntity MR_importFromDictionary:singleEntity];
}
- (void) tearDown
- (void) tearDownClass
{
[MagicalRecordHelpers cleanUp];
}
- (void) testImportAnEntityRelatedToAnotherEntityWithAOneToOneRelationship
- (void) testImportAnEntityRelatedToAbstractEntityViaToOneRelationshop
{
assertThat(testEntity, is(notNilValue()));
id testRelatedEntity = testEntity.testAbstractToOneRelationship;
assertThat(testEntity.testRelationship, is(notNilValue()));
assertThat(testEntity.testRelationship.sampleBaseAttribute, containsString(@"BASE"));
assertThat(testEntity.testRelationship.mainTestEntity, is(equalTo(testEntity)));
assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleBaseAttribute], containsString(@"BASE"));
}
- (void) testImportAnEntityRelatedToAnotherEntityWithAManyToOneRelationship
- (void) testImportAnEntityRelatedToAbstractEntityViaToManyRelationship
{
GHFail(@"Test Not Implemented");
assertThat(testEntity, is(notNilValue()));
assertThatInteger([testEntity.testAbstractToManyRelationship count], is(equalToInteger(2)));
id testRelatedEntity = [testEntity.testAbstractToManyRelationship anyObject];
assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleBaseAttribute], containsString(@"BASE"));
}
- (void) testImportAnEntityRelatedToAnitherEntityWithAManyToManyRelationship
#pragma - Subentity tests
- (void) testImportAnEntityRelatedToAConcreteSubEntityViaToOneRelationship
{
GHFail(@"Test Not Implemented");
id testRelatedEntity = testEntity.testConcreteToOneRelationship;
assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleBaseAttribute], containsString(@"BASE"));
assertThat([testRelatedEntity sampleConcreteAttribute], containsString(@"DECENDANT"));
}
- (void) testImportAnEntityRelatedToASubEntityWithAOneToOneRelationship
- (void) testImportAnEntityRelatedToASubEntityViaToManyRelationship
{
// assertThat(testEntity.testRelationship.sampleConcreteAttribute, containsString(@"DECENDANT"));
}
- (void) testImportAnEntityRelatedToASubEntityWithAManyToOneRelationship
{
GHFail(@"Test Not Implemented");
}
- (void) testImportAnEntityRelatedToASubEntityWithAManyToManyRelationship
{
GHFail(@"Test Not Implemented");
assertThatInteger([testEntity.testConcreteToManyRelationship count], is(equalToInteger(3)));
id testRelatedEntity = [testEntity.testConcreteToManyRelationship anyObject];
assertThat(testRelatedEntity, is(notNilValue()));
assertThat([testRelatedEntity sampleBaseAttribute], containsString(@"BASE"));
assertThat([testRelatedEntity sampleConcreteAttribute], containsString(@"DECENDANT"));
}
//Test ordered to many
@end

View File

@@ -12,7 +12,7 @@
- (void) setUp
{
[NSManagedObjectModel setDefaultManagedObjectModel:[NSManagedObjectModel managedObjectModelNamed:@"TestModel.momd"]];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel MR_managedObjectModelNamed:@"TestModel.momd"]];
}
- (void) tearDown
@@ -26,7 +26,7 @@
assertThat([NSManagedObjectContext defaultContext], is(notNilValue()));
assertThat([NSManagedObjectModel MR_defaultManagedObjectModel], is(notNilValue()));
assertThat([NSPersistentStoreCoordinator MR_defaultStoreCoordinator], is(notNilValue()));
assertThat([NSPersistentStore defaultPersistentStore], is(notNilValue()));
assertThat([NSPersistentStore MR_defaultPersistentStore], is(notNilValue()));
}
- (void) testCreateDefaultCoreDataStack
@@ -35,7 +35,7 @@
[self assertDefaultStack];
NSPersistentStore *defaultStore = [NSPersistentStore defaultPersistentStore];
NSPersistentStore *defaultStore = [NSPersistentStore MR_defaultPersistentStore];
assertThat([[defaultStore URL] absoluteString], endsWith(kMagicalRecordDefaultStoreFileName));
assertThat([defaultStore type], is(equalTo(NSSQLiteStoreType)));
}
@@ -46,7 +46,7 @@
[self assertDefaultStack];
NSPersistentStore *defaultStore = [NSPersistentStore defaultPersistentStore];
NSPersistentStore *defaultStore = [NSPersistentStore MR_defaultPersistentStore];
assertThat([defaultStore type], is(equalTo(NSInMemoryStoreType)));
}
@@ -57,7 +57,7 @@
[self assertDefaultStack];
NSPersistentStore *defaultStore = [NSPersistentStore defaultPersistentStore];
NSPersistentStore *defaultStore = [NSPersistentStore MR_defaultPersistentStore];
assertThat([defaultStore type], is(equalTo(NSSQLiteStoreType)));
assertThat([[defaultStore URL] absoluteString], endsWith(testStoreName));
}