This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
forked from vilanovi/PersistentModel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
255 additions
and
87 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
PersistentModel/PersistentModel Tests/PMObjectContext_Tests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
PersistentModel/PersistentModel Tests/PMPersistentStore_Tests.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
83
PersistentModel/PersistentModel Tests/PersistentModel_Tests.m
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.