forked from anagromataf/PinacotecaCore
-
Notifications
You must be signed in to change notification settings - Fork 4
/
PCModelLayerTests.m
110 lines (83 loc) · 3.99 KB
/
PCModelLayerTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// PCModelLayerTests.m
// PinacotecaCore
//
// Created by Tobias Kräntzer on 23.06.13.
// Copyright (c) 2013 Tobias Kräntzer. All rights reserved.
//
#import <SenTestingKit/SenTestingKit.h>
#import "PCImage+Private.h"
@interface PCModelLayerTests : SenTestCase
@property (nonatomic, strong) NSManagedObjectContext *context;
@property (nonatomic, strong) NSPersistentStoreCoordinator *storeCoordinator;
@end
@implementation PCModelLayerTests
- (void)setUp
{
[super setUp];
NSBundle *frameworkBundle = [NSBundle bundleForClass:[PCImage class]];
NSURL *modelURL = [frameworkBundle URLForResource:@"Model" withExtension:@"momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
NSError *error = nil;
NSPersistentStore *store = [self.storeCoordinator addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error];
NSAssert(store, nil);
self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
self.context.persistentStoreCoordinator = self.storeCoordinator;
}
#pragma mark Tests
- (void)testEntityDescription
{
NSEntityDescription *entityDescription = [PCImage entityDescriptionInManagedObjectContext:self.context];
STAssertNotNil(entityDescription, nil);
STAssertEqualObjects(entityDescription.name, @"PCImage", nil);
STAssertEqualObjects(entityDescription.managedObjectClassName, @"PCImage", nil);
}
- (void)testCreateImage
{
NSError *error = nil;
NSDictionary *values = @{@"id":@"123",
@"title":@"My first Image",
@"url":@"http://data.example.com/239f8z3z48g3.jpeg"};
PCImage *image = nil;
// Createn an image
BOOL created = NO;
image = [PCImage createOrUpdateWithValues:values
inManagedObjectContext:self.context
created:&created
error:&error];
STAssertNotNil(image, [error localizedDescription]);
STAssertTrue(created, nil);
STAssertEqualObjects(image.imageId, @"123", nil);
STAssertEqualObjects(image.title, @"My first Image", nil);
STAssertEqualObjects(image.url, @"http://data.example.com/239f8z3z48g3.jpeg", nil);
// Save the created image
BOOL success = [self.context save:&error];
STAssertTrue(success, [error localizedDescription]);
// Fetch the image again
image = [PCImage imageWithId:@"123"
inManagedObjectContext:self.context
error:&error];
STAssertNotNil(image, [error localizedDescription]);
STAssertEqualObjects(image.imageId, @"123", nil);
STAssertEqualObjects(image.title, @"My first Image", nil);
STAssertEqualObjects(image.url, @"http://data.example.com/239f8z3z48g3.jpeg", nil);
// Update the image
NSDictionary *newValues = @{@"id":@"123",
@"title":@"My best Image",
@"url":@"http://data.example.com/239f8z3z48g3.jpeg"};
image = [PCImage createOrUpdateWithValues:newValues
inManagedObjectContext:self.context
created:&created
error:&error];
STAssertNotNil(image, [error localizedDescription]);
STAssertFalse(created, nil);
STAssertEqualObjects(image.imageId, @"123", nil);
STAssertEqualObjects(image.title, @"My best Image", nil);
STAssertEqualObjects(image.url, @"http://data.example.com/239f8z3z48g3.jpeg", nil);
}
@end