From f8153c86535e83adecb40d00a2a0a0e66a8d85d9 Mon Sep 17 00:00:00 2001 From: Saul Mora Date: Mon, 8 Aug 2011 18:08:52 -0600 Subject: [PATCH] Implement tests for entity relationship importing --- Magical Record.xcodeproj/project.pbxproj | 4 ++ .../NSManagedObject+MagicalDataImport.m | 19 +++++- Unit Tests/Fixtures/SingleRelatedEntity.json | 39 ++++++++++++- .../iOS/TestEntities/_AbstractRelatedEntity.h | 13 ----- .../iOS/TestEntities/_AbstractRelatedEntity.m | 4 -- .../iOS/TestEntities/_SingleRelatedEntity.h | 57 ++++++++++++++++-- .../iOS/TestEntities/_SingleRelatedEntity.m | 28 ++++++++- Unit Tests/ImportSingleEntityTests.m | 2 +- ...portSingleEntityWithRelatedEntitiesTests.m | 58 +++++++++++-------- Unit Tests/MagicalRecordHelperTests.m | 10 ++-- 10 files changed, 178 insertions(+), 56 deletions(-) diff --git a/Magical Record.xcodeproj/project.pbxproj b/Magical Record.xcodeproj/project.pbxproj index 757a574..a68a133 100644 --- a/Magical Record.xcodeproj/project.pbxproj +++ b/Magical Record.xcodeproj/project.pbxproj @@ -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", diff --git a/Source/Categories/NSManagedObject+MagicalDataImport.m b/Source/Categories/NSManagedObject+MagicalDataImport.m index fd0ad0e..60202c8 100644 --- a/Source/Categories/NSManagedObject+MagicalDataImport.m +++ b/Source/Categories/NSManagedObject+MagicalDataImport.m @@ -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])]; diff --git a/Unit Tests/Fixtures/SingleRelatedEntity.json b/Unit Tests/Fixtures/SingleRelatedEntity.json index ae5139a..4cd1968 100644 --- a/Unit Tests/Fixtures/SingleRelatedEntity.json +++ b/Unit Tests/Fixtures/SingleRelatedEntity.json @@ -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" + } + ] } \ No newline at end of file diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.h b/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.h index 652e5b5..b78001d 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.h +++ b/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.h @@ -4,7 +4,6 @@ #import -@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 diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.m b/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.m index 614d679..438a764 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.m +++ b/Unit Tests/Fixtures/iOS/TestEntities/_AbstractRelatedEntity.m @@ -43,10 +43,6 @@ -@dynamic mainTestEntity; - - - diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.h b/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.h index 7f3b275..6e59301 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.h +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.h @@ -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 diff --git a/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.m b/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.m index 72c8165..1487b67 100644 --- a/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.m +++ b/Unit Tests/Fixtures/iOS/TestEntities/_SingleRelatedEntity.m @@ -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; diff --git a/Unit Tests/ImportSingleEntityTests.m b/Unit Tests/ImportSingleEntityTests.m index b31b0a8..db1ca7a 100644 --- a/Unit Tests/ImportSingleEntityTests.m +++ b/Unit Tests/ImportSingleEntityTests.m @@ -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"]; diff --git a/Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m b/Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m index 51a4ada..6246dcf 100644 --- a/Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m +++ b/Unit Tests/ImportSingleEntityWithRelatedEntitiesTests.m @@ -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 diff --git a/Unit Tests/MagicalRecordHelperTests.m b/Unit Tests/MagicalRecordHelperTests.m index 734fa0e..b8f300c 100644 --- a/Unit Tests/MagicalRecordHelperTests.m +++ b/Unit Tests/MagicalRecordHelperTests.m @@ -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)); }