Skip to content

Commit

Permalink
Simplified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseidhof committed Feb 18, 2012
1 parent b896c25 commit f05af80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
6 changes: 3 additions & 3 deletions BandCamp.xcdatamodeld/BandCamp.xcdatamodel/contents
Expand Up @@ -21,8 +21,8 @@
<attribute name="track_id" optional="YES" attributeType="Integer 64" defaultValueString="0" syncable="YES"/>
</entity>
<elements>
<element name="Album" positionX="160" positionY="192" width="128" height="90"/>
<element name="Band" positionX="160" positionY="192" width="128" height="135"/>
<element name="Track" positionX="160" positionY="192" width="128" height="90"/>
<element name="Album" positionX="171" positionY="144" width="128" height="90"/>
<element name="Band" positionX="-0" positionY="144" width="128" height="135"/>
<element name="Track" positionX="349" positionY="144" width="128" height="90"/>
</elements>
</model>
39 changes: 23 additions & 16 deletions IncrementalStoreTest/BandCampIS.m
Expand Up @@ -43,7 +43,9 @@ - (BOOL)loadMetadata:(NSError**)error {
return YES;
}

- (id)executeRequest:(NSPersistentStoreRequest*)request withContext:(NSManagedObjectContext*)context error:(NSError**)error {
- (id)executeRequest:(NSPersistentStoreRequest*)request
withContext:(NSManagedObjectContext*)context
error:(NSError**)error {
if(request.requestType == NSFetchRequestType)
{
NSFetchRequest *fetchRequest = (NSFetchRequest*)request;
Expand All @@ -56,15 +58,20 @@ - (id)executeRequest:(NSPersistentStoreRequest*)request withContext:(NSManagedOb
return nil;
}

- (id)fetchObjects:(NSFetchRequest*)request withContext:(NSManagedObjectContext*)context {
NSArray* items = [BandCampAPI apiRequestEntitiesWithName:request.entityName predicate:request.predicate];
- (id)fetchObjects:(NSFetchRequest*)request
withContext:(NSManagedObjectContext*)context {
NSArray* items = [BandCampAPI apiRequestEntitiesWithName:request.entityName
predicate:request.predicate];
return [items map:^(id item) {
NSManagedObjectID* oid = [self objectIdForNewObjectOfEntity:request.entity cacheValues:item];
NSManagedObjectID* oid = [self objectIdForNewObjectOfEntity:request.entity
cacheValues:item];
return [context objectWithID:oid];
}];
}

- (NSIncrementalStoreNode*)newValuesForObjectWithID:(NSManagedObjectID*)objectID withContext:(NSManagedObjectContext*)context error:(NSError**)error {
- (NSIncrementalStoreNode*)newValuesForObjectWithID:(NSManagedObjectID*)objectID
withContext:(NSManagedObjectContext*)context
error:(NSError**)error {
NSDictionary* values = [cache objectForKey:objectID];
NSIncrementalStoreNode* node =
[[NSIncrementalStoreNode alloc] initWithObjectID:objectID
Expand All @@ -79,22 +86,21 @@ - (NSArray*)obtainPermanentIDsForObjects:(NSArray*)array error:(NSError**)error
}

- (id)newValueForRelationship:(NSRelationshipDescription*)relationship forObjectWithID:(NSManagedObjectID*)objectID withContext:(NSManagedObjectContext*)context error:(NSError**)error {
NSString* entityName = relationship.entity.name;
BOOL isDiscography = [entityName isEqualToString:@"Band"] && [relationship.name isEqualToString:@"discography"];
BOOL isAlbumTracks = [entityName isEqualToString:@"Album"] && [relationship.name isEqualToString:@"tracks"];

if(isDiscography) {
return [self fetchDiscographyForBandWithId:objectID albumEntity:relationship.destinationEntity];
} else if(isAlbumTracks) {
return [self fetchTracksForAlbumWithId:objectID trackEntity:relationship.destinationEntity];
if([relationship.name isEqualToString:@"discography"]) {
return [self fetchDiscographyForBandWithId:objectID
albumEntity:relationship.destinationEntity];
} else if([relationship.name isEqualToString:@"tracks"]) {
return [self fetchTracksForAlbumWithId:objectID
trackEntity:relationship.destinationEntity];
}
NSLog(@"unknown relatioship: %@", relationship);
return nil;
}

#pragma mark Relationship fetching

- (NSArray*)fetchDiscographyForBandWithId:(NSManagedObjectID*)objectID albumEntity:(NSEntityDescription*)entity {
- (NSArray*)fetchDiscographyForBandWithId:(NSManagedObjectID*)objectID
albumEntity:(NSEntityDescription*)entity {
id bandId = [self referenceObjectForObjectID:objectID];
NSArray* discographyData = [BandCampAPI apiDiscographyForBandWithId:bandId];
return [discographyData map:^(id album) {
Expand All @@ -115,10 +121,11 @@ - (NSArray*)fetchTracksForAlbumWithId:(NSManagedObjectID*)objectID trackEntity:(
#pragma mark Caching

- (NSManagedObjectID*)objectIdForNewObjectOfEntity:(NSEntityDescription*)entityDescription
cacheValues:(NSDictionary*)values {
cacheValues:(NSDictionary*)values {
NSString* nativeKey = [self nativeKeyForEntityName:entityDescription.name];
id referenceId = [values objectForKey:nativeKey];
NSManagedObjectID *objectId = [self newObjectIDForEntity:entityDescription referenceObject:referenceId];
NSManagedObjectID *objectId = [self newObjectIDForEntity:entityDescription
referenceObject:referenceId];
[cache setObject:values forKey:objectId];
return objectId;
}
Expand Down

0 comments on commit f05af80

Please sign in to comment.