Skip to content

Commit

Permalink
Add another test for orphaned object deletion.
Browse files Browse the repository at this point in the history
Use a real object, rather than a OCMock to prove that the right
thing happens with @optional selector is not implemented.
  • Loading branch information
rayfix committed Dec 11, 2011
1 parent d95338b commit d739e78
Showing 1 changed file with 87 additions and 16 deletions.
103 changes: 87 additions & 16 deletions Specs/CoreData/RKManagedObjectLoaderSpec.m
Expand Up @@ -25,12 +25,43 @@
#import "RKCat.h"
#import "NSManagedObject+ActiveRecord.h"


/*
* A special mock for testing the managed object cache.
* We are not using OCMock here so we can test the case
* where optional protocol selectors are not defined.
*/
@interface TestObjectCache : NSObject<RKManagedObjectCache> {

}

@end

@implementation TestObjectCache
- (NSArray *)fetchRequestsForResourcePath:(NSString *)resourcePath
{
return [NSArray arrayWithObject:[RKHuman fetchRequest]];
}
@end

@interface RKManagedObjectLoaderSpec : RKSpec {

}

@end

@interface TestCacheRemoveOddOrphans : TestObjectCache {
}
@end

@implementation TestCacheRemoveOddOrphans
- (BOOL)shouldDeleteOrphanedObject:(NSManagedObject *)managedObject
{
RKHuman* human = (RKHuman*)managedObject;
return [human.railsID integerValue] % 2 == 0 ? NO : YES;
}
@end

@implementation RKManagedObjectLoaderSpec

- (void)testShouldDeleteObjectFromLocalStoreOnDELETE {
Expand Down Expand Up @@ -99,25 +130,21 @@ - (void)testShouldDeleteObjectsMissingFromPayloadReturnedByObjectCache {
[objectManager.mappingProvider setMapping:humanMapping forKeyPath:@"human"];
RKSpecStubNetworkAvailability(YES);
objectManager.objectStore = store;

id mockObjectCache = [OCMockObject mockForProtocol:@protocol(RKManagedObjectCache)];
NSArray* fetchRequests = [NSArray arrayWithObject:[RKHuman fetchRequest]];
[[[mockObjectCache expect] andReturn:fetchRequests] fetchRequestsForResourcePath:OCMOCK_ANY];
BOOL yes = YES;
[[[mockObjectCache stub] andReturnValue:OCMOCK_VALUE(yes)] shouldDeleteOrphanedObject:OCMOCK_ANY];
objectManager.objectStore.managedObjectCache = mockObjectCache;
objectManager.objectStore.managedObjectCache = [[[TestObjectCache alloc] init] autorelease];

RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
responseLoader.timeout = 25;
RKManagedObjectLoader* objectLoader = [RKManagedObjectLoader loaderWithResourcePath:@"/JSON/humans/all.json" objectManager:objectManager delegate:responseLoader];
[objectLoader send];
[responseLoader waitForResponse];

assertThatUnsignedInteger([RKHuman count:nil], is(equalToInt(2)));
assertThatBool([blake isDeleted], is(equalToBool(NO)));
assertThatBool([other isDeleted], is(equalToBool(NO)));
assertThatBool([deleteMe isDeleted], is(equalToBool(YES)));
}

- (void)testShouldNotDeleteCertainObjectsMissingFromPayloadReturnedByObjectCache
- (void)testShouldNotDeleteOrphansFromManagedObjectCache
{
RKManagedObjectStore* store = RKSpecNewManagedObjectStore();
RKManagedObjectMapping* humanMapping = [RKManagedObjectMapping mappingForEntityWithName:@"RKHuman"];
Expand All @@ -132,10 +159,10 @@ - (void)testShouldNotDeleteCertainObjectsMissingFromPayloadReturnedByObjectCache
blake.railsID = [NSNumber numberWithInt:123];
RKHuman* other = [RKHuman createEntity];
other.railsID = [NSNumber numberWithInt:456];
RKHuman* doNotDeleteMe1 = [RKHuman createEntity];
doNotDeleteMe1.railsID = [NSNumber numberWithInt:9999];
RKHuman* doNotDeleteMe2 = [RKHuman createEntity];
doNotDeleteMe2.railsID = [NSNumber numberWithInt:1000];
RKHuman* deleteOdd = [RKHuman createEntity];
deleteOdd.railsID = [NSNumber numberWithInt:9999];
RKHuman* doNotDeleteMe = [RKHuman createEntity];
doNotDeleteMe.railsID = [NSNumber numberWithInt:1000];
[store save];
assertThatUnsignedInteger([RKHuman count:nil], is(equalToInt(4)));

Expand All @@ -158,9 +185,53 @@ - (void)testShouldNotDeleteCertainObjectsMissingFromPayloadReturnedByObjectCache
[responseLoader waitForResponse];

NSArray* humans = [RKHuman findAll];
assertThatUnsignedInteger([humans count], is(equalToInt(4)));
assertThatBool([doNotDeleteMe1 isDeleted], is(equalToBool(NO)));
assertThatBool([doNotDeleteMe2 isDeleted], is(equalToBool(NO)));
assertThatUnsignedInteger([humans count], is(equalToInt(4)));
assertThatBool([blake isDeleted], is(equalToBool(NO)));
assertThatBool([other isDeleted], is(equalToBool(NO)));
assertThatBool([deleteOdd isDeleted], is(equalToBool(NO)));
assertThatBool([doNotDeleteMe isDeleted], is(equalToBool(NO)));
}

- (void)testShouldNotDeleteOddOrphansFromManagedObjectCache
{
RKManagedObjectStore* store = RKSpecNewManagedObjectStore();
RKManagedObjectMapping* humanMapping = [RKManagedObjectMapping mappingForEntityWithName:@"RKHuman"];
[humanMapping mapKeyPath:@"id" toAttribute:@"railsID"];
[humanMapping mapAttributes:@"name", nil];
humanMapping.primaryKeyAttribute = @"railsID";

// Create 4 objects, we will expect 4 after the load
[RKHuman truncateAll];
assertThatUnsignedInteger([RKHuman count:nil], is(equalToInt(0)));
RKHuman* blake = [RKHuman createEntity];
blake.railsID = [NSNumber numberWithInt:123];
RKHuman* other = [RKHuman createEntity];
other.railsID = [NSNumber numberWithInt:456];
RKHuman* deleteOdd = [RKHuman createEntity];
deleteOdd.railsID = [NSNumber numberWithInt:9999];
RKHuman* doNotDeleteMe = [RKHuman createEntity];
doNotDeleteMe.railsID = [NSNumber numberWithInt:1000];
[store save];
assertThatUnsignedInteger([RKHuman count:nil], is(equalToInt(4)));

RKObjectManager* objectManager = RKSpecNewObjectManager();
[objectManager.mappingProvider setMapping:humanMapping forKeyPath:@"human"];
RKSpecStubNetworkAvailability(YES);
objectManager.objectStore = store;
objectManager.objectStore.managedObjectCache = [[[TestCacheRemoveOddOrphans alloc] init] autorelease];

RKSpecResponseLoader* responseLoader = [RKSpecResponseLoader responseLoader];
responseLoader.timeout = 25;
RKManagedObjectLoader* objectLoader = [RKManagedObjectLoader loaderWithResourcePath:@"/JSON/humans/all.json" objectManager:objectManager delegate:responseLoader];
[objectLoader send];
[responseLoader waitForResponse];

NSArray* humans = [RKHuman findAll];
assertThatUnsignedInteger([humans count], is(equalToInt(3)));
assertThatBool([blake isDeleted], is(equalToBool(NO)));
assertThatBool([other isDeleted], is(equalToBool(NO)));
assertThatBool([deleteOdd isDeleted], is(equalToBool(YES)));
assertThatBool([doNotDeleteMe isDeleted], is(equalToBool(NO)));
}


Expand Down

0 comments on commit d739e78

Please sign in to comment.