/
DogTest.m
145 lines (115 loc) · 4.18 KB
/
DogTest.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// DogTest.m
// StoredModel
#import "DogTest.h"
@implementation DogTest
- (void)setUp {
[super setUp];
[Dog deleteAll];
dog = [Dog new];
dog.name = @"Wishbone";
}
- (void)testSaving {
STAssertTrue([dog save], @"");
STAssertTrue([[Dog all] count] == 1, @"");
STAssertTrue([[Dog count] intValue] == 1, @"");
dog = [Dog new];
STAssertFalse([dog save], @""); // name is required
}
- (void)testSettingAttributes {
[dog save];
Dog *fetchedDog = [[Dog all] objectAtIndex:0];
STAssertTrue([fetchedDog.name isEqualToString:@"Wishbone"], @"");
}
- (void)testFinding {
[dog save];
NSMutableArray *dogs;
dogs = [Dog find:@"name = '%@'", @"Wishbone"];
STAssertTrue([dogs count] == 1, @"");
dogs = [Dog find:@"name = '%@'", @"Fido"];
STAssertTrue([dogs count] == 0, @"");
STAssertEqualObjects(dog, [Dog findFirst:@"name = 'Wishbone'"], @"");
STAssertNil([Dog findFirst:@"name = 'Spot'"], @"");
}
- (void)testOrdering {
[dog save];
Dog *otherDog = [Dog new];
otherDog.name = @"Scruff"; [otherDog save];
NSMutableArray *dogs;
dogs = [Dog findByOrder:@"name"];
STAssertEqualObjects(otherDog, [dogs objectAtIndex:0], @"");
STAssertEqualObjects(dog, [dogs objectAtIndex:1], @"");
dogs = [Dog findByOrder:@"name desc"];
STAssertEqualObjects(dog, [dogs objectAtIndex:0], @"");
STAssertEqualObjects(otherDog, [dogs objectAtIndex:1], @"");
STAssertEqualObjects(otherDog, [Dog findFirstByOrder:@"name"], @"");
STAssertEqualObjects(dog, [Dog findFirstByOrder:@"name desc"], @"");
}
- (void)testCounting {
[Dog deleteAll];
STAssertEquals(0, [[Dog count] intValue], @"");
dog = [Dog new];
dog.name = @"Wishbone"; [dog save];
STAssertEquals(1, [[Dog count] intValue], @"");
Dog *otherDog = [Dog new];
otherDog.name = @"Scruff"; [otherDog save];
STAssertEquals(2, [[Dog count] intValue], @"");
STAssertEquals(1, [[Dog count:@"name = 'Scruff'"] intValue], @"");
}
- (void)testDestroying {
[dog save];
STAssertEquals(1, [[Dog count] intValue], @"");
STAssertTrue([dog destroy], @"");
STAssertEquals(0, [[Dog count] intValue], @"");
}
- (void)testDateQuerying {
NSDate *oneDayAgo = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSDate *twoDaysAgo = [NSDate dateWithTimeIntervalSinceNow:-86400*2];
[Dog deleteAll];
dog = [Dog new];
dog.name = @"Wishbone";
dog.createdAt = oneDayAgo;
Dog *otherDog = [Dog new];
otherDog.name = @"Fluffy";
otherDog.createdAt = twoDaysAgo;
[dog save]; [otherDog save];
NSMutableArray *dogs;
dogs = [Dog find:@"createdAt > %@", [oneDayAgo queryFormat]];
STAssertTrue([dogs count] == 0, @"");
dogs = [Dog find:@"createdAt > %@", [twoDaysAgo queryFormat]];
STAssertTrue(1 == [dogs count], @"");
}
- (void)testInitWithDictionary {
[Dog deleteAll];
dog = [Dog new:[NSDictionary dictionaryWithObjectsAndKeys:@"Fido", @"name", nil]];
STAssertTrue([dog.name isEqualToString:@"Fido"], @"name is %@", dog.name);
}
- (void)testId {
[dog save];
NSString *path = [[[dog objectID] URIRepresentation] path];
NSArray *components = [path componentsSeparatedByString:@"/"];
STAssertTrue([[components objectAtIndex:[components count]-1] isEqualToString:@"p1"], @"");
STAssertTrue([dog persistenceID] == 1, @"id is %i", [dog persistenceID]);
}
- (void)testKeyValueFinding {
NSDate *oneDayAgo = [NSDate dateWithTimeIntervalSinceNow:-86400];
NSDate *twoDaysAgo = [NSDate dateWithTimeIntervalSinceNow:-86400*2];
[Dog deleteAll];
dog = [Dog new];
dog.name = @"Wishbone";
dog.createdAt = oneDayAgo;
Dog *otherDog = [Dog new];
otherDog.name = @"Scruff";
otherDog.createdAt = twoDaysAgo;
[dog save]; [otherDog save];
dog = [[Dog findByKey:@"name" withValue:@"Wishbone"] objectAtIndex:0];
STAssertTrue([dog.name isEqualToString:@"Wishbone"], @"");
STAssertEqualObjects(dog.createdAt, oneDayAgo, @"");
dog = [[Dog findByKey:@"createdAt" withValue:twoDaysAgo] objectAtIndex:0];
STAssertTrue([dog.name isEqualToString:@"Scruff"], @"");
STAssertEqualObjects(dog.createdAt, twoDaysAgo, @"");
dog = [Dog findFirstByKey:@"createdAt" withValue:oneDayAgo];
STAssertTrue([dog.name isEqualToString:@"Wishbone"], @"");
STAssertEqualObjects(dog.createdAt, oneDayAgo, @"");
}
@end