forked from magicalpanda/MagicalRecord
/
NSManagedObjectHelperTests.m
124 lines (93 loc) · 3.67 KB
/
NSManagedObjectHelperTests.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// NSManagedObjectHelperTests.m
// Magical Record
//
// Created by Saul Mora on 7/15/11.
// Copyright 2011 Magical Panda Software LLC. All rights reserved.
//
#import "NSManagedObjectHelperTests.h"
#import "SingleRelatedEntity.h"
@implementation NSManagedObjectHelperTests
- (void) setUpClass
{
[NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel MR_managedObjectModelNamed:@"TestModel.momd"]];
}
- (void) setUp
{
[MagicalRecordHelpers setupCoreDataStackWithInMemoryStore];
}
- (void) tearDown
{
[MagicalRecordHelpers cleanUp];
}
-(BOOL)shouldRunOnMainThread
{
return YES;
}
//Test Request Creation
- (void) testCreateFetchRequestForEntity
{
NSFetchRequest *testRequest = [SingleRelatedEntity requestAll];
assertThat([[testRequest entity] name], is(equalTo(NSStringFromClass([SingleRelatedEntity class]))));
}
- (void) testCanRequestFirstEntityWithPredicate
{
NSPredicate *testPredicate = [NSPredicate predicateWithFormat:@"mappedStringAttribute = 'Test Predicate'"];
NSFetchRequest *testRequest = [SingleRelatedEntity requestFirstWithPredicate:testPredicate];
assertThatInteger([testRequest fetchLimit], is(equalToInteger(1)));
assertThat([testRequest predicate], is(equalTo([NSPredicate predicateWithFormat:@"mappedStringAttribute = 'Test Predicate'"])));
}
// Test return result set, all, first
- (void) testCreateRequestForFirstEntity
{
NSFetchRequest *testRequest = [SingleRelatedEntity requestFirstByAttribute:@"mappedStringAttribute" withValue:nil];
assertThat([[testRequest entity] name], is(equalTo(NSStringFromClass([SingleRelatedEntity class]))));
assertThatInteger([testRequest fetchLimit], is(equalToInteger(1)));
assertThatInteger([testRequest fetchOffset], is(equalToInteger(0)));
assertThat([testRequest predicate], is(equalTo([NSPredicate predicateWithFormat:@"mappedStringAttribute = nil"])));
}
- (void) testCanGetEntityDescriptionFromEntityClass
{
NSEntityDescription *testDescription = [SingleRelatedEntity entityDescription];
assertThat(testDescription, is(notNilValue()));
}
// Test Entity creation
- (void) testCanCreateEntityInstance
{
id testEntity = [SingleRelatedEntity createEntity];
assertThat(testEntity, is(notNilValue()));
}
// Test Entity Deletion
- (void) testCanDeleteEntityInstance
{
id testEntity = [SingleRelatedEntity createEntity];
[[NSManagedObjectContext MR_defaultContext] MR_save];
assertThatBool([testEntity isDeleted], is(equalToBool(NO)));
[testEntity MR_deleteEntity];
assertThat(testEntity, is(notNilValue()));
assertThatBool([testEntity isDeleted], is(equalToBool(YES)));
}
// Test Number of Entities
- (void) createSampleData:(NSInteger)numberOfTestEntitiesToCreate
{
for (NSInteger i = 0; i < numberOfTestEntitiesToCreate; i++)
{
SingleRelatedEntity *testEntity = [SingleRelatedEntity createEntity];
testEntity.mappedStringAttribute = [NSString stringWithFormat:@"%d", i / 5];
}
[[NSManagedObjectContext MR_defaultContext] MR_save];
}
- (void) testCanSearchForNumberOfAllEntities
{
NSInteger numberOfTestEntitiesToCreate = 20;
[self createSampleData:numberOfTestEntitiesToCreate];
assertThat([SingleRelatedEntity numberOfEntities], is(equalToInteger(numberOfTestEntitiesToCreate)));
}
- (void) testCanSearchForNumberOfEntitiesWithPredicate
{
NSInteger numberOfTestEntitiesToCreate = 20;
[self createSampleData:numberOfTestEntitiesToCreate];
NSPredicate *searchFilter = [NSPredicate predicateWithFormat:@"mappedStringAttribute = '1'"];
assertThat([SingleRelatedEntity numberOfEntitiesWithPredicate:searchFilter], is(equalToInteger(5)));
}
@end