Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vilanovi committed Feb 16, 2015
1 parent 7ff5563 commit c34c883
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 87 deletions.
133 changes: 133 additions & 0 deletions PersistentModel/PersistentModel Tests/PMObjectContext_Tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// PersistentModel_Tests.m
// PersistentModel Tests
//
// Created by Joan Martin on 16/12/14.
// Copyright (c) 2014 Joan Martin. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import "TestUtilities.h"
#import "PersistentModel.h"

@interface PMObjectContext_Tests : XCTestCase

@end

@implementation PMObjectContext_Tests
{
PMObjectContext *_objectContext;
}

- (void)setUp
{
[super setUp];

// Creating an object context (without persistent store)
_objectContext = [[PMObjectContext alloc] initWithPersistentStore:nil];
}

- (void)tearDown
{
// Deleting the object context
_objectContext = nil;

[super tearDown];
}

#pragma mark - Creation

- (void)testObjectIDNull
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:nil];
XCTAssert(object.objectID == nil, @"Object ID must be null before insertion");
}

- (void)testObjectIDCreation
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
XCTAssert(object.objectID != nil, @"Object ID cannot be null after insertion");
}

- (void)testObjectContextAssignment
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
XCTAssertEqual(object.context, _objectContext, @"Object context doesn't match the expected context");
}

#pragma mark - Retrival

- (void)testObjectRegisteredRetrival
{
PMBaseObject *object1 = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
PMBaseObject *object2 = [_objectContext objectRegisteredForID:object1.objectID];
XCTAssertEqual(object1, object2, @"Object registered for ID doesn't match the expected object");
}

- (void)testObjectWithIDRetrival
{
PMBaseObject *object1 = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
PMBaseObject *object2 = [_objectContext objectWithID:object1.objectID];
XCTAssertEqual(object1, object2, @"Object with ID doesn't match the expected object");
}

#pragma mark - Insertion

- (void)testObjectInsertion
{
PMBaseObject *object1 = [[PMBaseObject alloc] initAndInsertToContext:nil];
BOOL succeed = [_objectContext insertObject:object1];
XCTAssert(succeed, @"insertObject: method must return YES");

PMBaseObject *object2 = [_objectContext objectWithID:object1.objectID];
XCTAssertEqual(object1, object2, @"Object with ID doesn't match the expected object");
}

- (void)testObjectContextAssignment2
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:nil];
XCTAssert(object.context == nil, @"Object context is not nil");
[_objectContext insertObject:object];
XCTAssertEqual(object.context, _objectContext, @"Object context doesn't match the expected context");
}

- (void)testObjectInsertTwice
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:nil];
BOOL succeed1 = [_objectContext insertObject:object];
BOOL succeed2 = [_objectContext insertObject:object];

XCTAssert(succeed1 == YES, @"insertObject: method must return YES");
XCTAssert(succeed2 == YES, @"insertObject: method must return YES when inserting twice an object to the same context");
}

#pragma mark - Deletion

- (void)testObjectDeletionContextRetrival
{
PMBaseObject *object1 = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
[_objectContext deleteObject:object1];

PMBaseObject *object2 = [_objectContext objectRegisteredForID:object1.objectID];
XCTAssert(object2 == nil, @"Object should not be in the context");

PMBaseObject *object3 = [_objectContext objectWithID:object1.objectID];
XCTAssert(object3 == nil, @"Object should not be in the context");
}


- (void)testObjectDeletionList
{
PMBaseObject *object = [[PMBaseObject alloc] initAndInsertToContext:_objectContext];
[_objectContext deleteObject:object];

NSArray *deletedObjects = [_objectContext deletedObjects];
XCTAssert(deletedObjects.count == 1, @"Deleted objects should contain only one object");
XCTAssertEqual(deletedObjects[0], object, @"The deleted object doesn't match the deleted object in the context");
}



@end
55 changes: 55 additions & 0 deletions PersistentModel/PersistentModel Tests/PMPersistentStore_Tests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// PMPersistentStore_Tests.m
// PersistentModel
//
// Created by Joan Martin on 16/12/14.
// Copyright (c) 2014 Joan Martin. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>

#import "TestUtilities.h"
#import "PersistentModel.h"

@interface PMPersistentStore_Tests : XCTestCase

@end

@implementation PMPersistentStore_Tests
{
PMPersistentStore *_persistentStore;
}

- (void)setUp
{
[super setUp];

// Creating the URL where we will store the database
NSURL *url = [applicationCacheDirectory() URLByAppendingPathComponent:@"persistentstore_test.sql"];

// Instantiating the persistent store
_persistentStore = [[PMSQLiteStore alloc] initWithURL:url];
}

- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testExample
{
// This is an example of a functional test case.
XCTAssert(YES, @"Pass");
}

- (void)testPerformanceExample
{
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}

@end
83 changes: 0 additions & 83 deletions PersistentModel/PersistentModel Tests/PersistentModel_Tests.m

This file was deleted.

11 changes: 11 additions & 0 deletions PersistentModel/PersistentModel Tests/TestUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// TestUtilities.h
// PersistentModel
//
// Created by Joan Martin on 16/12/14.
// Copyright (c) 2014 Joan Martin. All rights reserved.
//

#import <Foundation/Foundation.h>

NSURL* applicationCacheDirectory();
31 changes: 31 additions & 0 deletions PersistentModel/PersistentModel Tests/TestUtilities.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// TestUtilities.m
// PersistentModel
//
// Created by Joan Martin on 16/12/14.
// Copyright (c) 2014 Joan Martin. All rights reserved.
//

#import "TestUtilities.h"

NSURL* applicationCacheDirectory()
{
static NSURL *url = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [pathList[0] stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]];

// Create cache path if it doesn't exist, yet:
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:&error];

url = [NSURL fileURLWithPath:cachePath];
});

return url;
}
18 changes: 14 additions & 4 deletions PersistentModel/PersistentModel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
D201AA2318DC7C6600E5F26D /* PMVideo.m in Sources */ = {isa = PBXBuildFile; fileRef = D201AA2218DC7C6600E5F26D /* PMVideo.m */; };
D201AA2618DC7C6E00E5F26D /* PMUser.m in Sources */ = {isa = PBXBuildFile; fileRef = D201AA2518DC7C6E00E5F26D /* PMUser.m */; };
D251290B19E6DE4F000904BD /* PMObjectIndex.m in Sources */ = {isa = PBXBuildFile; fileRef = D251290A19E6DE4F000904BD /* PMObjectIndex.m */; };
D257646E1A4075EA00C59CD1 /* PersistentModel_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = D257646D1A4075EA00C59CD1 /* PersistentModel_Tests.m */; };
D257646E1A4075EA00C59CD1 /* PMObjectContext_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = D257646D1A4075EA00C59CD1 /* PMObjectContext_Tests.m */; };
D25764741A40776D00C59CD1 /* PMBaseObject.m in Sources */ = {isa = PBXBuildFile; fileRef = D201AA0D18DC75E600E5F26D /* PMBaseObject.m */; };
D25764751A40776D00C59CD1 /* PMObjectContext.m in Sources */ = {isa = PBXBuildFile; fileRef = D201AA1118DC75E600E5F26D /* PMObjectContext.m */; };
D25764761A40776D00C59CD1 /* PMObjectID.m in Sources */ = {isa = PBXBuildFile; fileRef = D2C986BC19DAB8F300043329 /* PMObjectID.m */; };
Expand All @@ -31,6 +31,8 @@
D257647E1A40776D00C59CD1 /* PMSQLiteStore.m in Sources */ = {isa = PBXBuildFile; fileRef = D201AA1918DC75E600E5F26D /* PMSQLiteStore.m */; };
D25764831A407AE500C59CD1 /* Pods-PersistentModel.debug.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = D25764811A407AE500C59CD1 /* Pods-PersistentModel.debug.xcconfig */; };
D25764841A407AE500C59CD1 /* Pods-PersistentModel.release.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = D25764821A407AE500C59CD1 /* Pods-PersistentModel.release.xcconfig */; };
D25764871A407CF400C59CD1 /* TestUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = D25764861A407CF400C59CD1 /* TestUtilities.m */; };
D25764891A407D2900C59CD1 /* PMPersistentStore_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = D25764881A407D2900C59CD1 /* PMPersistentStore_Tests.m */; };
D26C2B3518BFB1CF00E8BE90 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D26C2B3418BFB1CF00E8BE90 /* Foundation.framework */; };
D26C2B3718BFB1CF00E8BE90 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D26C2B3618BFB1CF00E8BE90 /* CoreGraphics.framework */; };
D26C2B3918BFB1CF00E8BE90 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D26C2B3818BFB1CF00E8BE90 /* UIKit.framework */; };
Expand Down Expand Up @@ -84,10 +86,13 @@
D251290C19E6F1A2000904BD /* PMObjectContext_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PMObjectContext_Private.h; sourceTree = "<group>"; };
D25764691A4075EA00C59CD1 /* PersistentModel Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "PersistentModel Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
D257646C1A4075EA00C59CD1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D257646D1A4075EA00C59CD1 /* PersistentModel_Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PersistentModel_Tests.m; sourceTree = "<group>"; };
D257646D1A4075EA00C59CD1 /* PMObjectContext_Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = PMObjectContext_Tests.m; sourceTree = "<group>"; };
D257647F1A4077A700C59CD1 /* libPods.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libPods.a; path = "Pods/build/Debug-iphoneos/libPods.a"; sourceTree = "<group>"; };
D25764811A407AE500C59CD1 /* Pods-PersistentModel.debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = "Pods-PersistentModel.debug.xcconfig"; path = "Pods/Target Support Files/Pods-PersistentModel/Pods-PersistentModel.debug.xcconfig"; sourceTree = "<group>"; };
D25764821A407AE500C59CD1 /* Pods-PersistentModel.release.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = "Pods-PersistentModel.release.xcconfig"; path = "Pods/Target Support Files/Pods-PersistentModel/Pods-PersistentModel.release.xcconfig"; sourceTree = "<group>"; };
D25764851A407CF400C59CD1 /* TestUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TestUtilities.h; sourceTree = "<group>"; };
D25764861A407CF400C59CD1 /* TestUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TestUtilities.m; sourceTree = "<group>"; };
D25764881A407D2900C59CD1 /* PMPersistentStore_Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PMPersistentStore_Tests.m; sourceTree = "<group>"; };
D26C2B3118BFB1CF00E8BE90 /* PersistentModel.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PersistentModel.app; sourceTree = BUILT_PRODUCTS_DIR; };
D26C2B3418BFB1CF00E8BE90 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
D26C2B3618BFB1CF00E8BE90 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -161,7 +166,10 @@
D257646A1A4075EA00C59CD1 /* PersistentModel Tests */ = {
isa = PBXGroup;
children = (
D257646D1A4075EA00C59CD1 /* PersistentModel_Tests.m */,
D25764851A407CF400C59CD1 /* TestUtilities.h */,
D25764861A407CF400C59CD1 /* TestUtilities.m */,
D257646D1A4075EA00C59CD1 /* PMObjectContext_Tests.m */,
D25764881A407D2900C59CD1 /* PMPersistentStore_Tests.m */,
D257646B1A4075EA00C59CD1 /* Supporting Files */,
);
path = "PersistentModel Tests";
Expand Down Expand Up @@ -447,6 +455,7 @@
buildActionMask = 2147483647;
files = (
D25764771A40776D00C59CD1 /* PMFetchRequest.m in Sources */,
D25764891A407D2900C59CD1 /* PMPersistentStore_Tests.m in Sources */,
D25764781A40776D00C59CD1 /* PMKeyedArchiver.m in Sources */,
D25764761A40776D00C59CD1 /* PMObjectID.m in Sources */,
D257647B1A40776D00C59CD1 /* PMPersistentObject.m in Sources */,
Expand All @@ -455,8 +464,9 @@
D257647E1A40776D00C59CD1 /* PMSQLiteStore.m in Sources */,
D25764751A40776D00C59CD1 /* PMObjectContext.m in Sources */,
D257647D1A40776D00C59CD1 /* PMSQLiteObject.m in Sources */,
D25764871A407CF400C59CD1 /* TestUtilities.m in Sources */,
D257647A1A40776D00C59CD1 /* PMObjectIndex.m in Sources */,
D257646E1A4075EA00C59CD1 /* PersistentModel_Tests.m in Sources */,
D257646E1A4075EA00C59CD1 /* PMObjectContext_Tests.m in Sources */,
D257647C1A40776D00C59CD1 /* PMPersistentStore.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 6 additions & 0 deletions Source/PMObjectContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ extern NSString * const PMObjectContextDeletedObjectsKey;
**/
- (NSArray*)registeredObjects;

/**
* This method returns all instances that have been delted from that context.
* @return An array with all deleted instances from the current context.
**/
- (NSArray*)deletedObjects;

/** ---------------------------------------------------------------- **
* @name Object Management
** ---------------------------------------------------------------- **/
Expand Down
Loading

0 comments on commit c34c883

Please sign in to comment.