forked from wireload/Ratatosk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WLRemoteObjectTest.j
402 lines (321 loc) · 13.2 KB
/
WLRemoteObjectTest.j
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* WLRemoteObjectTest.j
* Ratatosk
*
* Created by Alexander Ljungberg on September 16, 2010.
* Copyright 2010, WireLoad Inc. All rights reserved.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
* with the distribution. Neither the name of WireLoad Inc. nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
@import <OJUnit/OJTestCase.j>
@import "WLRemoteObject.j"
@import "WLRemoteTransformers.j"
@implementation WLRemoteObjectTest : OJTestCase
{
}
- (void)tearDown
{
[[WLRemoteLink sharedRemoteLink] cancelAllActions];
[[WLRemoteContext sharedRemoteContext] reset];
_lastAction = nil;
_lastRequest = nil;
}
- (void)testInstanceByPk
{
[self assert:nil equals:[TestRemoteObject instanceForPk:nil] message:"nothing for pk nil"];
var test1 = [[TestRemoteObject alloc] initWithJson:{'id': 5, 'name': 'test1'}],
test2 = [[TestRemoteObject alloc] initWithJson:{'id': 15, 'name': 'test2'}];
[self assertTrue:[TestRemoteObject instanceForPk:5] === test1 message:"test1 at pk 5"];
[self assertTrue:[TestRemoteObject instanceForPk:15] === test2 message:"test2 at pk 15"];
[test1 setPk:nil];
[self assert:nil equals:[TestRemoteObject instanceForPk:5] message:"test1 no longer at pk 5"];
[test1 setPk:7];
[self assertTrue:[TestRemoteObject instanceForPk:5] === nil message:"nothing at pk 5"];
[self assertTrue:[TestRemoteObject instanceForPk:7] === test1 message:"test1 now at pk 7"];
[self assertTrue:[TestRemoteObject instanceForPk:15] === test2];
// No conflicts between different kinds of remote objects.
var test3 = [[OtherRemoteObject alloc] initWithJson:{'id': 5}],
test4 = [[OtherRemoteObject alloc] initWithJson:{'id': 7}],
test5 = [[OtherRemoteObject alloc] initWithJson:{'id': 15}];
[self assertTrue:[TestRemoteObject instanceForPk:7] === test1];
[self assertTrue:[TestRemoteObject instanceForPk:15] === test2];
[self assertTrue:[OtherRemoteObject instanceForPk:7] === test4];
[self assertTrue:[OtherRemoteObject instanceForPk:15] === test5];
}
- (void)testDeferredProperties
{
[WLRemoteObject setDirtProof:YES];
var test1 = [[TestRemoteObject alloc] initWithJson:{}],
test2 = [[TestRemoteObject alloc] initWithJson:{'id': 1}],
test3 = [[TestRemoteObject alloc] initWithJson:{'id': 2, 'name': 'a name'}];
[WLRemoteObject setDirtProof:NO];
[self assertTrue:[test1 isPropertyDeferred:'pk'] message:'id defined in test 1'];
[self assertTrue:[test1 isPropertyDeferred:'name'] message:'name defined in test 1'];
[self assertTrue:[test1 isPropertyDeferred:'count'] message:'count defined in test 1'];
[self assertFalse:[test2 isPropertyDeferred:'pk']];
[self assertTrue:[test2 isPropertyDeferred:'name']];
[self assertTrue:[test2 isPropertyDeferred:'count']];
[self assertFalse:[test3 isPropertyDeferred:'pk']];
[self assertFalse:[test3 isPropertyDeferred:'name']];
[self assertTrue:[test3 isPropertyDeferred:'count']];
[test3 setCount:3];
[self assertFalse:[test3 isPropertyDeferred:'count']];
[WLRemoteObject setDirtProof:YES];
[test1 updateFromJson:{'name': 'bob', 'count': 9}];
[WLRemoteObject setDirtProof:NO];
[self assertTrue:[test1 isPropertyDeferred:'pk']];
[self assertFalse:[test1 isPropertyDeferred:'name']];
[self assertFalse:[test1 isPropertyDeferred:'count']];
[test1 setName:'hello'];
[self assertTrue:[test1 isPropertyDirty:'name']];
[test1 updateFromJson:{'name': 'bah'} preservingDirtyProperties:YES];
[self assert:[test1 name] equals:'hello' message:"once a property has been loaded local changes should not be overwritten by remote updates"];
[test1 invalidateProperty:"name"];
[self assertTrue:[test1 isPropertyDeferred:'name']];
[test1 updateFromJson:{'name': 'bah'} preservingDirtyProperties:YES];
[self assert:[test1 name] equals:"bah"];
}
- (void)testForeignObjectsTransformer
{
var test1 = [[TestRemoteObject alloc] initWithJson:{}],
test2 = [[TestRemoteObject alloc] initWithJson:{'id': 1, 'name': 'test2 name', 'other_objects':
[{'id': 5, 'coolness': 17}, {'id': 9}]
}];
[self assert:[] equals:[test1 otherObjects]];
[self assertTrue:[[test2 otherObjects] count] == 2];
var r1 = [test2 otherObjects][0];
[self assert:5 equals:[r1 pk]];
[self assert:[r1 coolness] equals:17];
var r2 = [test2 otherObjects][1];
[self assert:[r2 pk] equals:9];
[self assert:[r2 coolness] equals:nil];
[self assertTrue:[r2 isPropertyDeferred:'coolness']];
[test2 makeAllDirty];
var jso = [test2 asPatchJSObject];
//CPLog.warn([CPDictionary dictionaryWithJSObject:jso['other_objects']]);
[self assertTrue:5 == jso['other_objects'][0]['id'] || 9 == jso['other_objects'][0]['id']];
[self assertTrue:5 == jso['other_objects'][1]['id'] || 9 == jso['other_objects'][1]['id']];
}
- (void)testDirt
{
[WLRemoteObject setDirtProof:YES];
var test1 = [[TestRemoteObject alloc] initWithJson:{}],
test2 = [[TestRemoteObject alloc] initWithJson:{'id': 1, 'name': 'test2 name', 'other_objects':
[{'id': 5, 'coolness': 17}, {'id': 9}]
}];
[WLRemoteObject setDirtProof:NO];
[self assertFalse:[test1 isDirty] message:"test1 isDirty after creation"];
[self assertFalse:[test2 isDirty] message:"test2 isDirty after creation"];
[test2 setName:"Bob"];
[self assertFalse:[test1 isDirty] message:"test1 isDirty"];
[self assertTrue:[test2 isDirty] message:"test2 isDirty"];
[test2 cleanAll];
[self assertFalse:[test2 isDirty] message:"test2 isDirty after clean"];
}
- (void)testKeepActionPath
{
// Normally a remote object will set its action path at the last moment in remoteActionWillBegin:,
// rather than when first setting up the action, to give the path time to be changed by the result
// of previous actions. However, if the action does not belong to the remote object, if it's just
// an unrelated action which happens to have the remote object as its delegate, the remote object
// shouldn't overwrite the path of the action.
var test1 = [[TestRemoteObject alloc] initWithJson:{'id': 1, 'name': 'test2 name', 'other_objects':[{'id': 5, 'coolness': 17}, {'id': 9}]}],
testAction = [WLRemoteAction schedule:WLRemoteActionGetType path:"somepath.json" delegate:test1 message:"Loading..."];
[[WLRemoteLink sharedRemoteLink] setShouldFlushActions:YES];
[self assert:@"somepath.json" equals:[testAction path] message:@"remote path of unrelated action not changed"];
[[WLRemoteLink sharedRemoteLink] setShouldFlushActions:NO];
[[WLRemoteLink sharedRemoteLink] cancelAllActions];
// When the remote action is managing its action it should set the path.
[test1 setName:@"other name"];
[test1 ensureSaved];
// CPLog.error([WLRemoteLink sharedRemoteLink].actionQueue);
[[WLRemoteLink sharedRemoteLink] setShouldFlushActions:YES];
var lastAction = [WLRemoteAction lastAction];
[self assert:testAction notSame:lastAction];
[self assert:1 equals:[lastAction path] message:@"remote path of ensureSaved successfully set"];
}
- (void)testLoadOnly
{
var test = [[LoadOnlyRemoteObject alloc] initWithJson:{'id': 1, 'count': 5, 'name': 'test2 name', 'other_objects':
[{'id': 5, 'coolness': 17}, {'id': 9}]
}];
[test setName:@"other name"];
[test setOtherObjects:[]];
// Load only properties track dirtiness but a dirty load only property does not mean the object is dirty.
[self assertFalse:[test isDirty]];
// Load only objects don't cause server save actions when changed.
[test ensureSaved];
[[WLRemoteLink sharedRemoteLink] setShouldFlushActions:YES];
var lastAction = [WLRemoteAction lastAction];
[self assertFalse:lastAction message:@"expect no action for load only property change"];
// Load only properties should not be included when sending PATCHes.
[test setCount:6];
var patchVersion = JSON.stringify([test asPatchJSObject]);
[self assert:'{"count":6}' equals:patchVersion message:"only count to patch"];
}
- (void)testArchive
{
var test1 = [[TestRemoteObject alloc] initWithJson:{'id': 1, 'name': 'test2 name', 'other_objects':[{'id': 5, 'coolness': 17}, {'id': 9}]}],
archived = [CPKeyedArchiver archivedDataWithRootObject:test1],
unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived];
[self assert:[test1 UID] same:[unarchived UID] message:@"unarchived instance should be exact same instance"];
var test2 = [[TestRemoteObject alloc] init];
[test2 setName:@"unsaved object"];
[test2 setOtherObjects:[]];
[test2 setCount:3];
archived = [CPKeyedArchiver archivedDataWithRootObject:test2];
unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived];
[self assertTrue:[unarchived isNew] message:@"unarchived unsaved object should be new"];
[self assert:@"unsaved object" equals:[unarchived name]];
[self assert:[] equals:[unarchived otherObjects]];
[self assert:3 equals:[unarchived count]];
var other1 = [[OtherRemoteObject alloc] initWithJson:{'coolness': 10}],
other2 = [OtherRemoteObject instanceForPk:5];
[test2 setOtherObjects:[other1]];
archived = [CPKeyedArchiver archivedDataWithRootObject:test2];
unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived];
var newOther1 = [[unarchived otherObjects] firstObject];
[self assertTrue:[newOther1 isNew]];
[self assert:10 equals:[newOther1 coolness]];
[test2 setOtherObjects:[other2]];
archived = [CPKeyedArchiver archivedDataWithRootObject:test2];
unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived];
[self assert:[other2] equals:[unarchived otherObjects]];
}
@end
@implementation TestRemoteObject : WLRemoteObject
{
CPString name @accessors;
long count @accessors;
CPArray otherObjects @accessors;
}
+ (CPArray)remoteProperties
{
return [
['pk', 'id'],
['name'],
['count'],
['otherObjects', 'other_objects', [WLForeignObjectsTransformer forObjectClass:OtherRemoteObject]],
];
}
- (id)init
{
if (self = [super init])
{
count = 5;
otherObjects = [];
}
return self;
}
- (CPString)description
{
return [self UID] + " " + [self pk] + " " + [self name];
}
@end
@implementation OtherRemoteObject : WLRemoteObject
{
CPString coolness @accessors;
}
+ (CPArray)remoteProperties
{
return [
['pk', 'id'],
['coolness'],
];
}
- (CPString)description
{
return "OtherRemoteObject: " + [self UID] + " " + [self pk];
}
@end
@implementation LoadOnlyRemoteObject : WLRemoteObject
{
CPString name @accessors;
long count @accessors;
CPArray otherObjects @accessors;
}
+ (CPArray)remoteProperties
{
return [
['pk', 'id'],
['count'],
['name', 'name', nil, YES], // load-only
['otherObjects', 'other_objects', [WLForeignObjectsTransformer forObjectClass:OtherRemoteObject], YES], //load-only
];
}
- (id)init
{
if (self = [super init])
{
count = 5;
name = @"unnamed";
otherObjects = [];
}
return self;
}
- (CPString)description
{
return [self UID] + " " + [self pk] + " " + [self name];
}
@end
@implementation AutoTransformerObject : WLRemoteObject
{
CPDate date @accessors;
}
+ (CPArray)remoteProperties
{
return [
['pk', 'id'],
['date'],
];
}
- (id)init
{
if (self = [super init])
{
date = [CPDate dateWithTimeIntervalSince1970:123456789];
}
}
@end
var _lastAction,
_lastRequest;
@implementation WLRemoteAction (UnitTest)
+ (WLRemoteAction)lastAction
{
return _lastAction;
}
+ (CPURLRequest)lastRequest
{
return _lastRequest;
}
- (CPURLConnection)makeConnectionWithRequest:(CPURLRequest)aRequest
{
// CPLog.error(self + " makeConnection: " + aRequest);
_lastAction = self;
_lastRequest = aRequest;
// Don't actually send anything.
}
@end