forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectStateTests.m
234 lines (180 loc) · 8.63 KB
/
ObjectStateTests.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
/**
* 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 "PFEncoder.h"
#import "PFFieldOperation.h"
#import "PFMutableObjectState.h"
#import "PFOperationSet.h"
#import "PFTestCase.h"
@interface ObjectStateTests : PFTestCase
@end
@implementation ObjectStateTests
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (PFObjectState *)sampleObjectState {
PFMutableObjectState *state = [PFMutableObjectState stateWithParseClassName:@"Yarr"];
state.objectId = @"yolo";
state.complete = YES;
return state;
}
- (void)assertObjectState:(PFObjectState *)state equalToState:(PFObjectState *)differentState {
XCTAssertEqualObjects(state.parseClassName, differentState.parseClassName);
XCTAssertEqualObjects(state.objectId, differentState.objectId);
XCTAssertEqual(state.complete, differentState.complete);
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testInit {
PFObjectState *state = [[PFObjectState alloc] init];
XCTAssertNil(state.parseClassName);
XCTAssertNil(state.objectId);
XCTAssertFalse(state.complete);
state = [[PFMutableObjectState alloc] init];
XCTAssertNil(state.parseClassName);
XCTAssertNil(state.objectId);
XCTAssertFalse(state.complete);
}
- (void)testInitWithParseClassName {
PFObjectState *state = [[PFObjectState alloc] initWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
state = [PFObjectState stateWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
state = [[PFMutableObjectState alloc] initWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
state = [PFMutableObjectState stateWithParseClassName:@"Yarr"];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
state = [PFObjectState stateWithParseClassName:@"Yarr" objectId:@"a" isComplete:YES];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
XCTAssertEqualObjects(state.objectId, @"a");
XCTAssertTrue(state.complete);
state = [[PFObjectState alloc] initWithParseClassName:@"Yarr" objectId:@"a" isComplete:YES];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
XCTAssertEqualObjects(state.objectId, @"a");
XCTAssertTrue(state.complete);
state = [PFMutableObjectState stateWithParseClassName:@"Yarr" objectId:@"a" isComplete:YES];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
XCTAssertEqualObjects(state.objectId, @"a");
XCTAssertTrue(state.complete);
state = [[PFMutableObjectState alloc] initWithParseClassName:@"Yarr" objectId:@"a" isComplete:YES];
XCTAssertEqualObjects(state.parseClassName, @"Yarr");
XCTAssertEqualObjects(state.objectId, @"a");
XCTAssertTrue(state.complete);
}
- (void)testInitWithState {
PFObjectState *sampleState = [self sampleObjectState];
PFObjectState *state = [[PFObjectState alloc] initWithState:sampleState];
[self assertObjectState:state equalToState:sampleState];
state = [PFObjectState stateWithState:sampleState];
[self assertObjectState:state equalToState:sampleState];
state = [[PFMutableObjectState alloc] initWithState:sampleState];
[self assertObjectState:state equalToState:sampleState];
state = [PFMutableObjectState stateWithState:sampleState];
[self assertObjectState:state equalToState:sampleState];
}
- (void)testCopying {
PFObjectState *sampleState = [self sampleObjectState];
PFObjectState *stateCopy = [sampleState copy];
XCTAssertNotEqual(sampleState, stateCopy);
[self assertObjectState:stateCopy equalToState:sampleState];
PFMutableObjectState *mutableState = [PFMutableObjectState stateWithState:sampleState];
stateCopy = [mutableState copy];
XCTAssertNotEqual(mutableState, stateCopy);
[self assertObjectState:stateCopy equalToState:sampleState];
}
- (void)testMutableCopying {
PFObjectState *sampleState = [self sampleObjectState];
PFObjectState *stateCopy = [sampleState mutableCopy];
XCTAssertNotEqual(sampleState, stateCopy);
[self assertObjectState:stateCopy equalToState:sampleState];
PFMutableObjectState *state = [PFMutableObjectState stateWithState:sampleState];
stateCopy = [state mutableCopy];
XCTAssertNotEqual(state, stateCopy);
[self assertObjectState:stateCopy equalToState:sampleState];
}
- (void)testMutableAccessors {
PFMutableObjectState *mutableState = [[PFMutableObjectState alloc] init];
mutableState.parseClassName = @"Yolo";
XCTAssertEqualObjects(mutableState.parseClassName, @"Yolo");
mutableState.objectId = @"yarr";
XCTAssertEqualObjects(mutableState.objectId, @"yarr");
mutableState.complete = YES;
XCTAssertTrue(mutableState.complete);
NSString *isoDate = @"1970-01-01T00:00:00Z";
[mutableState setCreatedAtFromString:isoDate];
[mutableState setUpdatedAtFromString:isoDate];
XCTAssertEqualObjects(mutableState.createdAt, [NSDate dateWithTimeIntervalSince1970:0]);
XCTAssertEqualObjects(mutableState.updatedAt, [NSDate dateWithTimeIntervalSince1970:0]);
}
- (void)testServerData {
PFMutableObjectState *mutableState = [[PFMutableObjectState alloc] init];
XCTAssertEqualObjects(mutableState.serverData, @{});
[mutableState setServerDataObject:@"foo" forKey:@"bar"];
XCTAssertEqualObjects(mutableState.serverData, @{ @"bar": @"foo" });
[mutableState removeServerDataObjectForKey:@"bar"];
XCTAssertEqualObjects(mutableState.serverData, @{});
[mutableState setServerDataObject:@"foo" forKey:@"bar"];
[mutableState removeServerDataObjectsForKeys:@[ @"bar" ]];
XCTAssertEqualObjects(mutableState.serverData, @{});
mutableState.serverData = @{ @"foo": @"bar" };
XCTAssertEqualObjects(mutableState.serverData, @{ @"foo": @"bar" });
}
- (void)testDeleteFromServerData {
PFMutableObjectState *mutableState = [[PFMutableObjectState alloc] init];
XCTAssertEqualObjects(mutableState.serverData, @{});
[mutableState setServerDataObject:@"foo" forKey:@"bar"];
XCTAssertEqualObjects(mutableState.serverData, @{ @"bar": @"foo" });
[mutableState setServerDataObject:[PFDeleteOperation new] forKey:@"bar"];
XCTAssertEqualObjects(mutableState.serverData, @{});
}
- (void)testEncode {
PFMutableObjectState *mutableState = [[PFMutableObjectState alloc] init];
mutableState.objectId = @"objectId";
mutableState.createdAt = [NSDate dateWithTimeIntervalSince1970:0];
mutableState.updatedAt = [NSDate dateWithTimeIntervalSince1970:5];
mutableState.serverData = @{ @"a": @"b" };
NSDictionary *expected = @{
@"objectId": @"objectId",
@"createdAt": @"1970-01-01T00:00:00.000Z",
@"updatedAt": @"1970-01-01T00:00:05.000Z",
@"a": @"b"
};
NSDictionary *actual = [mutableState dictionaryRepresentationWithObjectEncoder:[PFEncoder objectEncoder] error:nil];
XCTAssertEqualObjects(actual, expected);
}
- (void)testApply {
PFMutableObjectState *objectState = [[PFMutableObjectState alloc] init];
objectState.objectId = @"betterObjectId";
objectState.createdAt = [NSDate dateWithTimeIntervalSince1970:17];
objectState.updatedAt = [NSDate dateWithTimeIntervalSince1970:25];
objectState.serverData = @{ @"a": @"b", @"c": @"d" };
PFMutableObjectState *secondState = [[PFMutableObjectState alloc] init];
secondState.objectId = @"anObjectId";
secondState.serverData = @{ @"a": @"d", @"e": @"f" };
[secondState applyState:objectState];
XCTAssertEqualObjects(secondState.objectId, @"betterObjectId");
XCTAssertEqualObjects(secondState.createdAt, [NSDate dateWithTimeIntervalSince1970:17]);
XCTAssertEqualObjects(secondState.updatedAt, [NSDate dateWithTimeIntervalSince1970:25]);
XCTAssertEqualObjects(secondState.serverData, (@{ @"a": @"b", @"c": @"d", @"e": @"f" }));
}
- (void)testApplyOperation {
PFMutableObjectState *objectState = [[PFMutableObjectState alloc] init];
objectState.serverData = @{
@"a": @13,
@"b": @"someValue",
@"c": @15
};
PFOperationSet *operationSet = [[PFOperationSet alloc] init];
operationSet[@"a"] = [[PFIncrementOperation alloc] initWithAmount:@10];
operationSet[@"b"] = [[PFDeleteOperation alloc] init];
operationSet[@"c"] = [[PFSetOperation alloc] initWithValue:@25];
[objectState applyOperationSet:operationSet];
XCTAssertEqualObjects(objectState.serverData, (@{ @"a": @23, @"c": @25 }));
}
@end