-
-
Notifications
You must be signed in to change notification settings - Fork 872
/
DecoderTests.m
307 lines (238 loc) · 12.2 KB
/
DecoderTests.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* 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 "PFDecoder.h"
#import "PFFieldOperation.h"
#import "PFFileObject.h"
#import "PFGeoPoint.h"
#import "PFPolygon.h"
#import "PFObjectPrivate.h"
#import "PFRelationPrivate.h"
#import "PFTestCase.h"
@interface DecoderTests : PFTestCase
@end
@implementation DecoderTests
- (void)testConstructors {
PFDecoder *decoder = [[PFDecoder alloc] init];
XCTAssertNotNil(decoder);
}
- (void)testDefaultObjectDecoder {
PFDecoder *decoder = [PFDecoder objectDecoder];
XCTAssertNotNil(decoder);
XCTAssertEqual(decoder, [PFDecoder objectDecoder]);
}
- (void)testDecodingFieldOperations {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"key" : @{@"__op" : @"Increment",
@"amount" : @100500} }];
XCTAssertNotNil(decoded);
PFIncrementOperation *operation = decoded[@"key"];
XCTAssertNotNil(operation);
PFAssertIsKindOfClass(operation, [PFIncrementOperation class]);
XCTAssertEqualObjects(operation.amount, @100500);
}
- (void)testDecodingDeleteOperation {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"key" : @{@"__op" : @"Delete"} }];
XCTAssertNotNil(decoded);
id operation = decoded[@"key"];
XCTAssertNotNil(operation);
PFAssertIsKindOfClass(operation, [PFDeleteOperation class]);
}
- (void)testDecodingDates {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"date" : @{@"__type" : @"Date",
@"iso" : @"1970-01-01T00:00:01.000Z"} }];
XCTAssertNotNil(decoded);
NSDate *date = decoded[@"date"];
XCTAssertNotNil(date);
PFAssertIsKindOfClass(date, [NSDate class]);
XCTAssertEqualObjects(date, [NSDate dateWithTimeIntervalSince1970:1.0]);
}
- (void)testDecodingBytes {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"data" : @{@"__type" : @"Bytes",
@"base64" : @"eW9sbw=="} }];
XCTAssertNotNil(decoded);
NSData *data = decoded[@"data"];
XCTAssertNotNil(data);
PFAssertIsKindOfClass(data, [NSData class]);
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
XCTAssertEqualObjects(string, @"yolo");
}
- (void)testDecodingGeoPoints {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"geoPoint" : @{@"__type" : @"GeoPoint",
@"latitude" : @10,
@"longitude" : @20} }];
XCTAssertNotNil(decoded);
PFGeoPoint *geoPoint = decoded[@"geoPoint"];
XCTAssertNotNil(geoPoint);
PFAssertIsKindOfClass(geoPoint, [PFGeoPoint class]);
XCTAssertEqualObjects(geoPoint, [PFGeoPoint geoPointWithLatitude:10.0 longitude:20.0]);
}
- (void)testDecodingPolygons {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSArray *coordinates = @[@[@0,@0],@[@0,@1],@[@1,@1],@[@1,@0]];
NSDictionary *decoded = [decoder decodeObject:@{ @"polygon" : @{@"__type" : @"Polygon",
@"coordinates" : coordinates }}];
XCTAssertNotNil(decoded);
PFPolygon *polygon = decoded[@"polygon"];
XCTAssertNotNil(polygon);
PFAssertIsKindOfClass(polygon, [PFPolygon class]);
XCTAssertEqualObjects(polygon, [PFPolygon polygonWithCoordinates:coordinates]);
}
- (void)testDecodingRelations {
PFDecoder *decoder = [[PFDecoder alloc] init];
PFObject *object = [PFObject objectWithClassName:@"Yolo"];
NSDictionary *decoded = [decoder decodeObject:@{ @"relation" : @{@"__type" : @"Relation",
@"className" : @"Yolo",
@"objects" : @[ object ]}
}];
XCTAssertNotNil(decoded);
PFRelation *relation = decoded[@"relation"];
XCTAssertNotNil(relation);
PFAssertIsKindOfClass(relation, [PFRelation class]);
XCTAssertEqualObjects(relation.targetClass, @"Yolo");
XCTAssertTrue([relation _hasKnownObject:object]);
}
- (void)testDecodingFiles {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"file" : @{@"__type" : @"File",
@"name" : @"yolo.png",
@"url" : @"http://yarr.com/yolo.png"} }];
XCTAssertNotNil(decoded);
PFFileObject *file = decoded[@"file"];
XCTAssertNotNil(file);
PFAssertIsKindOfClass(file, [PFFileObject class]);
XCTAssertEqualObjects(file.name, @"yolo.png");
XCTAssertEqualObjects(file.url, @"http://yarr.com/yolo.png");
}
- (void)testDecodingPointers {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"pointer1" : @{@"__type" : @"Pointer",
@"className" : @"Yolo",
@"objectId" : @"123"},
@"pointer2" : @{@"__type" : @"Pointer",
@"className" : @"Yolo1",
@"localId" : @"456"} }];
XCTAssertNotNil(decoded);
PFObject *object = decoded[@"pointer1"];
XCTAssertNotNil(object);
PFAssertIsKindOfClass(object, [PFObject class]);
XCTAssertEqualObjects(object.parseClassName, @"Yolo");
XCTAssertEqualObjects(object.objectId, @"123");
PFObject *localObject = decoded[@"pointer2"];
XCTAssertNotNil(localObject);
PFAssertIsKindOfClass(localObject, [PFObject class]);
XCTAssertEqualObjects(localObject.parseClassName, @"Yolo1");
XCTAssertNil(localObject.objectId);
XCTAssertEqualObjects([localObject getOrCreateLocalId], @"456");
}
- (void)testDecodingObjects {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"object" : @{@"__type" : @"Object",
@"className" : @"Yolo",
@"objectId" : @"123"} }];
XCTAssertNotNil(decoded);
PFObject *object = decoded[@"object"];
XCTAssertNotNil(object);
PFAssertIsKindOfClass(object, [PFObject class]);
XCTAssertEqualObjects(object.parseClassName, @"Yolo");
XCTAssertEqualObjects(object.objectId, @"123");
}
- (void)testDecodingObjectsWithDates {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"object" : @{@"__type" : @"Object",
@"className" : @"Yolo",
@"objectId" : @"123",
@"updatedAt" : @"1970-01-01T00:00:01.000Z",
@"createdAt" : @"1970-01-01T00:00:02.000Z"} }];
PFObject *object = decoded[@"object"];
XCTAssertEqualObjects(object.updatedAt, [NSDate dateWithTimeIntervalSince1970:1.0]);
XCTAssertEqualObjects(object.createdAt, [NSDate dateWithTimeIntervalSince1970:2.0]);
decoded = [decoder decodeObject:@{ @"object" : @{@"__type" : @"Object",
@"className" : @"Yolo",
@"objectId" : @"123",
@"updatedAt" : @{@"__type" : @"Date",
@"iso" : @"1970-01-01T00:00:01.000Z"},
@"createdAt" : @{@"__type" : @"Date",
@"iso" : @"1970-01-01T00:00:02.000Z"}} }];
object = decoded[@"object"];
XCTAssertEqualObjects(object.updatedAt, [NSDate dateWithTimeIntervalSince1970:1.0]);
XCTAssertEqualObjects(object.createdAt, [NSDate dateWithTimeIntervalSince1970:2.0]);
}
- (void)testDecodingUnknownType {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"yarr" : @{@"__type" : @"Yolo",
@"name" : @"Yolo!"} }];
XCTAssertNotNil(decoded);
NSDictionary *dictionary = decoded[@"yarr"];
XCTAssertNotNil(dictionary);
PFAssertIsKindOfClass(dictionary, [NSDictionary class]);
XCTAssertEqualObjects(dictionary[@"name"], @"Yolo!");
}
- (void)testDecodingArrays {
PFDecoder *decoder = [[PFDecoder alloc] init];
NSDictionary *decoded = [decoder decodeObject:@{ @"array" : @[ @1, @{@"a" : @"b"} ] }];
XCTAssertNotNil(decoded);
NSArray *array = decoded[@"array"];
XCTAssertNotNil(array);
PFAssertIsKindOfClass(array, [NSArray class]);
XCTAssertEqualObjects(array[0], @1);
XCTAssertEqualObjects(array[1], @{ @"a" : @"b" });
}
///--------------------------------------
#pragma mark - OfflineDecoder Tests
///--------------------------------------
- (void)testOfflineDecoderConstructors {
PFOfflineDecoder *decoder = [PFOfflineDecoder decoderWithOfflineObjects:@{ @"yolo11" : [PFObject objectWithClassName:@"Yolo"] }];
XCTAssertNotNil(decoder);
}
- (void)testOfflineDecoderDecoding {
NSDictionary *offlineObjects = @{ @"yolo11" : [BFTask taskWithResult:[PFObject objectWithClassName:@"Yolo"]] };
PFOfflineDecoder *decoder = [PFOfflineDecoder decoderWithOfflineObjects:offlineObjects];
NSArray *decoded = [decoder decodeObject:@[ @{ @"__type" : @"OfflineObject",
@"uuid" : @"yolo11"
},
@{ @"__type" : @"Object",
@"className" : @"Yarr"
} ]];
XCTAssertNotNil(decoded);
PFObject *offlineObject = decoded[0];
XCTAssertNotNil(offlineObject);
XCTAssertEqual(offlineObject, [offlineObjects[@"yolo11"] result]);
PFObject *object = decoded[1];
XCTAssertNotNil(object);
XCTAssertNotEqual(object, offlineObject);
XCTAssertEqualObjects(object.parseClassName, @"Yarr");
}
///--------------------------------------
#pragma mark - KnownParseObjectDecoder Tests
///--------------------------------------
- (void)testKnownParseObjectDecoderConstructors {
PFKnownParseObjectDecoder *decoder = [PFKnownParseObjectDecoder decoderWithFetchedObjects:@{ @"a" : [PFObject objectWithClassName:@"Yolo"] }];
XCTAssertNotNil(decoder);
}
- (void)testKnownParseObjectDecoderDecoding {
NSDictionary *objects = @{ @"a" : [PFObject objectWithClassName:@"Yolo"] };
PFKnownParseObjectDecoder *decoder = [PFKnownParseObjectDecoder decoderWithFetchedObjects:objects];
NSArray *decoded = [decoder decodeObject:@[ @{ @"__type" : @"Pointer",
@"className" : @"Yolo",
@"objectId" : @"a" },
@{ @"__type" : @"Pointer",
@"className" : @"Yarr",
@"objectId" : @"b" } ]];
XCTAssertNotNil(decoded);
PFObject *knownObject = decoded[0];
XCTAssertEqual(knownObject, objects[@"a"]);
PFObject *object = decoded[1];
XCTAssertNotEqual(knownObject, object);
XCTAssertEqualObjects(object.objectId, @"b");
}
@end