forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryStateUnitTests.m
241 lines (182 loc) · 8.6 KB
/
QueryStateUnitTests.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "PFMacros.h"
#import "PFMutableQueryState.h"
#import "PFTestCase.h"
@interface QueryStateUnitTests : PFTestCase
@end
@implementation QueryStateUnitTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (PFQueryState *)sampleQueryState {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
state.limit = 100;
state.skip = 200;
state.cachePolicy = kPFCachePolicyCacheOnly;
state.maxCacheAge = 100500.0;
state.trace = YES;
state.shouldIgnoreACLs = YES;
state.shouldIncludeDeletingEventually = YES;
state.queriesLocalDatastore = YES;
state.localDatastorePinName = @"Yolo!";
[state setEqualityConditionWithObject:@"a" forKey:@"b"];
[state setRelationConditionWithObject:@"c" forKey:@"d"];
[state sortByKey:@"a" ascending:NO];
[state includeKey:@"yolo"];
[state selectKeys:@[ @"yolo" ]];
[state redirectClassNameForKey:@"ABC"];
return state;
}
- (void)assertQueryState:(PFQueryState *)state equalToState:(PFQueryState *)differentState {
XCTAssertEqualObjects(state, differentState);
XCTAssertEqualObjects(state.parseClassName, differentState.parseClassName);
XCTAssertEqual(state.limit, differentState.limit);
XCTAssertEqual(state.skip, differentState.skip);
XCTAssertEqual(state.cachePolicy, differentState.trace);
XCTAssertEqual(state.trace, differentState.trace);
XCTAssertEqual(state.shouldIgnoreACLs, differentState.shouldIgnoreACLs);
XCTAssertEqual(state.shouldIncludeDeletingEventually, differentState.shouldIncludeDeletingEventually);
XCTAssertEqual(state.queriesLocalDatastore, differentState.queriesLocalDatastore);
XCTAssertEqualObjects(state.conditions, differentState.conditions);
XCTAssertEqualObjects(state.sortKeys, differentState.sortKeys);
XCTAssertEqualObjects(state.sortOrderString, differentState.sortOrderString);
XCTAssertEqualObjects(state.includedKeys, differentState.includedKeys);
XCTAssertEqualObjects(state.selectedKeys, differentState.selectedKeys);
XCTAssertEqualObjects(state.extraOptions, differentState.extraOptions);
XCTAssertEqualObjects(state.localDatastorePinName, differentState.localDatastorePinName);
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testDefaultValues {
PFQueryState *state = [[PFQueryState alloc] init];
XCTAssertEqual(state.cachePolicy, kPFCachePolicyIgnoreCache);
XCTAssertEqual(state.maxCacheAge, INFINITY);
XCTAssertEqual(state.limit, -1);
state = [[PFMutableQueryState alloc] init];
XCTAssertEqual(state.cachePolicy, kPFCachePolicyIgnoreCache);
XCTAssertEqual(state.maxCacheAge, INFINITY);
XCTAssertEqual(state.limit, -1);
}
- (void)testInitWithState {
PFQueryState *sampleState = [self sampleQueryState];
PFQueryState *state = [[PFQueryState alloc] initWithState:sampleState];
[self assertQueryState:state equalToState:sampleState];
state = [[PFMutableQueryState alloc] initWithState:sampleState];
[self assertQueryState:state equalToState:sampleState];
state = [PFQueryState stateWithState:sampleState];
[self assertQueryState:state equalToState:sampleState];
state = [PFMutableQueryState stateWithState:sampleState];
[self assertQueryState:state equalToState:sampleState];
}
- (void)testInitWithClassName {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
state = [PFMutableQueryState stateWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
}
- (void)testCopying {
PFQueryState *sampleState = [self sampleQueryState];
PFQueryState *state = [sampleState copy];
XCTAssertFalse([state isKindOfClass:[PFMutableQueryState class]]);
[self assertQueryState:state equalToState:sampleState];
}
- (void)testMutableCopying {
PFQueryState *sampleState = [self sampleQueryState];
PFMutableQueryState *state = [sampleState mutableCopy];
XCTAssert([state isKindOfClass:[PFMutableQueryState class]]);
[self assertQueryState:state equalToState:sampleState];
}
- (void)testGenericConditions {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state setConditionType:@"$yolo" withObject:@"a" forKey:@"yarr"];
XCTAssertEqualObjects(state.conditions[@"yarr"][@"$yolo"], @"a");
}
- (void)testEqualityConditions {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state setEqualityConditionWithObject:@"a" forKey:@"yarr"];
XCTAssertEqualObjects(state.conditions[@"yarr"], @"a");
}
- (void)testRelationConditions {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state setRelationConditionWithObject:@"a" forKey:@"yarr"];
XCTAssertEqualObjects(state.conditions[@"$relatedTo"][@"object"], @"a");
XCTAssertEqualObjects(state.conditions[@"$relatedTo"][@"key"], @"yarr");
}
- (void)testRemoveConditions {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state setEqualityConditionWithObject:@"a" forKey:@"b"];
XCTAssertEqual(state.conditions.count, 1);
[state removeAllConditions];
XCTAssertEqual(state.conditions.count, 0);
}
- (void)testSortByKey {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state sortByKey:@"a" ascending:YES];
XCTAssertEqualObjects(state.sortKeys, @[ @"a" ]);
[state sortByKey:@"b" ascending:NO];
XCTAssertEqualObjects(state.sortKeys, @[ @"-b" ]);
}
- (void)testAddSortKey {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state addSortKey:@"a" ascending:YES];
XCTAssertEqualObjects(state.sortKeys, @[ @"a" ]);
[state addSortKey:@"b" ascending:NO];
NSArray *sortKeys = @[ @"a", @"-b" ];
XCTAssertEqualObjects(state.sortKeys, sortKeys);
[state addSortKey:nil ascending:YES];
XCTAssertEqualObjects(state.sortKeys, sortKeys);
[state addSortKey:nil ascending:NO];
XCTAssertEqualObjects(state.sortKeys, sortKeys);
}
- (void)testAddSortKeysFromDescriptors {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
NSArray *sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"a" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"b" ascending:NO] ];
[state addSortKeysFromSortDescriptors:sortDescriptors];
NSArray *sortKeys = @[ @"a", @"-b" ];
XCTAssertEqualObjects(state.sortKeys, sortKeys);
}
- (void)testIncludeKeys {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state includeKey:@"a"];
[state includeKey:@"b"];
NSSet *includedKeys = PF_SET(@"a", @"b");
XCTAssertEqualObjects(state.includedKeys, includedKeys);
}
- (void)testIncludeMultipleKeys {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state includeKeys:@[ @"a", @"b", @"c" ]];
[state includeKey:@"a"];
NSSet *includedKeys = PF_SET(@"a", @"b", @"c");
XCTAssertEqualObjects(state.includedKeys, includedKeys);
}
- (void)testSelectKeys {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
NSArray *selectedKeys = @[ @"a", @"b" ];
[state selectKeys:selectedKeys];
XCTAssertEqualObjects(state.selectedKeys, [NSSet setWithArray:selectedKeys]);
[state selectKeys:@[]];
XCTAssertEqualObjects(state.selectedKeys, [NSSet setWithArray:selectedKeys]);
[state selectKeys:nil];
XCTAssertNil(state.selectedKeys);
}
- (void)testRedirectClassName {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
[state redirectClassNameForKey:@"yolo"];
XCTAssertEqualObjects(state.extraOptions, @{ @"redirectClassNameForKey" : @"yolo" });
}
- (void)testDebugQuickLookObject {
PFMutableQueryState *state = [[PFMutableQueryState alloc] initWithParseClassName:@"Yarr"];
id quickLookObject = [state debugQuickLookObject];
XCTAssertNotNil(quickLookObject);
XCTAssertEqualObjects(quickLookObject, [[state dictionaryRepresentation] description]);
}
@end