Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["tests.data.ItemFileWriteStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["tests.data.ItemFileWriteStore"] = true;
3
dojo.provide("tests.data.ItemFileWriteStore");
4
dojo.require("tests.data.readOnlyItemFileTestTemplates");
5
 
6
dojo.require("dojo.data.ItemFileWriteStore");
7
dojo.require("dojo.data.api.Read");
8
dojo.require("dojo.data.api.Identity");
9
dojo.require("dojo.data.api.Write");
10
dojo.require("dojo.data.api.Notification");
11
 
12
 
13
// First, make sure ItemFileWriteStore can still pass all the same unit tests
14
// that we use for its superclass, ItemFileReadStore:
15
tests.data.readOnlyItemFileTestTemplates.registerTestsForDatastore("dojo.data.ItemFileWriteStore");
16
 
17
 
18
// Now run some tests that are specific to the write-access features:
19
doh.register("tests.data.ItemFileWriteStore",
20
	[
21
		function test_getFeatures(){
22
			//	summary:
23
			//		Simple test of the getFeatures function of the store
24
			//	description:
25
			//		Simple test of the getFeatures function of the store
26
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
27
 
28
			var features = store.getFeatures();
29
 
30
			// make sure we have the expected features:
31
			doh.assertTrue(features["dojo.data.api.Read"] != null);
32
			doh.assertTrue(features["dojo.data.api.Identity"] != null);
33
			doh.assertTrue(features["dojo.data.api.Write"] != null);
34
			doh.assertTrue(features["dojo.data.api.Notification"] != null);
35
			doh.assertFalse(features["iggy"]);
36
 
37
			// and only the expected features:
38
			var count = 0;
39
			for(var i in features){
40
				doh.assertTrue((i === "dojo.data.api.Read" ||
41
					i === "dojo.data.api.Identity" ||
42
					i === "dojo.data.api.Write" ||
43
					i === "dojo.data.api.Notification"));
44
				count++;
45
			}
46
			doh.assertEqual(count, 4);
47
		},
48
		function testWriteAPI_setValue(){
49
			//	summary:
50
			//		Simple test of the setValue API
51
			//	description:
52
			//		Simple test of the setValue API
53
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
54
 
55
			var deferred = new doh.Deferred();
56
			function onComplete(items, request){
57
				doh.assertEqual(1, items.length);
58
				var item = items[0];
59
				doh.assertTrue(store.containsValue(item, "capital", "Cairo"));
60
 
61
				// FIXME:
62
				//    Okay, so this seems very odd.  Maybe I'm just being dense.
63
				//    These tests works:
64
				doh.assertEqual(store.isDirty(item), false);
65
				doh.assertTrue(store.isDirty(item) == false);
66
				//    But these seemingly equivalent tests will not work:
67
				// doh.assertFalse(store.isDirty(item));
68
				// doh.assertTrue(!(store.isDirty(item)));
69
				//
70
				//    All of which seems especially weird, given that this *does* work:
71
				doh.assertFalse(store.isDirty());
72
 
73
 
74
				doh.assertTrue(store.isDirty(item) == false);
75
				doh.assertTrue(!store.isDirty());
76
				store.setValue(item, "capital", "New Cairo");
77
				doh.assertTrue(store.isDirty(item));
78
				doh.assertTrue(store.isDirty());
79
				doh.assertEqual(store.getValue(item, "capital").toString(), "New Cairo");
80
				deferred.callback(true);
81
			}
82
			function onError(error, request){
83
				deferred.errback(error);
84
			}
85
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
86
			return deferred; //Object
87
		},
88
		function testWriteAPI_setValues(){
89
			//	summary:
90
			//		Simple test of the setValues API
91
			//	description:
92
			//		Simple test of the setValues API
93
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
94
 
95
			var deferred = new doh.Deferred();
96
			function onComplete(items, request){
97
				doh.assertEqual(1, items.length);
98
				var item = items[0];
99
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
100
				doh.assertTrue(store.isDirty(item) == false);
101
				doh.assertTrue(!store.isDirty());
102
				store.setValues(item, "name", ["Egypt 1", "Egypt 2"]);
103
				doh.assertTrue(store.isDirty(item));
104
				doh.assertTrue(store.isDirty());
105
				var values = store.getValues(item, "name");
106
				doh.assertTrue(values[0] == "Egypt 1");
107
				doh.assertTrue(values[1] == "Egypt 2");
108
				deferred.callback(true);
109
			}
110
			function onError(error, request){
111
				deferred.errback(error);
112
			}
113
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
114
			return deferred; //Object
115
		},
116
		function testWriteAPI_unsetAttribute(){
117
			//	summary:
118
			//		Simple test of the unsetAttribute API
119
			//	description:
120
			//		Simple test of the unsetAttribute API
121
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
122
 
123
			var deferred = new doh.Deferred();
124
			function onComplete(items, request) {
125
				doh.assertEqual(1, items.length);
126
				var item = items[0];
127
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
128
				doh.assertTrue(store.isDirty(item) == false);
129
				doh.assertTrue(!store.isDirty());
130
				store.unsetAttribute(item, "name");
131
				doh.assertTrue(store.isDirty(item));
132
				doh.assertTrue(store.isDirty());
133
				doh.assertTrue(!store.hasAttribute(item, "name"));
134
				deferred.callback(true);
135
			}
136
			function onError(error, request) {
137
				deferred.errback(error);
138
			}
139
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
140
			return deferred; //Object
141
		},
142
		function testWriteAPI_newItem(){
143
			//	summary:
144
			//		Simple test of the newItem API
145
			//	description:
146
			//		Simple test of the newItem API
147
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
148
 
149
			var deferred = new doh.Deferred();
150
			doh.assertTrue(!store.isDirty());
151
 
152
			var onNewInvoked = false;
153
			store.onNew = function(newItem, parentInfo){
154
 
155
				doh.assertTrue(newItem !== null);
156
				doh.assertTrue(parentInfo === null);
157
				doh.assertTrue(store.isItem(newItem));
158
				onNewInvoked = true;
159
			};
160
			var canada = store.newItem({name: "Canada", abbr:"ca", capital:"Ottawa"});
161
			doh.assertTrue(onNewInvoked);
162
 
163
			doh.assertTrue(store.isDirty(canada));
164
			doh.assertTrue(store.isDirty());
165
			doh.assertTrue(store.getValues(canada, "name") == "Canada");
166
			function onComplete(items, request){
167
				doh.assertEqual(1, items.length);
168
				var item = items[0];
169
				doh.assertTrue(store.containsValue(item, "name", "Canada"));
170
				deferred.callback(true);
171
			}
172
			function onError(error, request){
173
				deferred.errback(error);
174
			}
175
			store.fetch({query:{name:"Canada"}, onComplete: onComplete, onError: onError});
176
			return deferred; //Object
177
		},
178
		function testWriteAPI_newItem_withParent(){
179
			//	summary:
180
			//		Simple test of the newItem API with a parent assignment
181
			//	description:
182
			//		Simple test of the newItem API with a parent assignment
183
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
184
 
185
			var deferred = new doh.Deferred();
186
			doh.assertTrue(!store.isDirty());
187
			function onComplete(items, request){
188
				doh.assertEqual(1, items.length);
189
				var item = items[0];
190
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
191
 
192
				//Attach an onNew to validate we get expected values.
193
				var onNewInvoked = false;
194
				store.onNew = function(newItem, parentInfo){
195
					doh.assertEqual(item, parentInfo.item);
196
					doh.assertEqual("cities", parentInfo.attribute);
197
					doh.assertTrue(parentInfo.oldValue === undefined);
198
					doh.assertTrue(parentInfo.newValue === newItem);
199
					onNewInvoked = true;
200
				};
201
 
202
				//Attach an onSet and verify onSet is NOT called in this case.
203
				store.onSet = function(item, attribute, oldValue, newValue){
204
					doh.assertTrue(false);
205
				};
206
 
207
				//See if we can add in a new item representing the city of Cairo.
208
				//This should also call the onNew set above....
209
				var newItem = store.newItem({name: "Cairo", abbr: "Cairo"}, {parent: item, attribute: "cities"});
210
				doh.assertTrue(onNewInvoked);
211
 
212
				function onCompleteNewItemShallow(items, request){
213
					doh.assertEqual(0, items.length);
214
					function onCompleteNewItemDeep(items, request){
215
						doh.assertEqual(1, items.length);
216
						var item = items[0];
217
						doh.assertEqual("Cairo", store.getValue(item, "name"));
218
						deferred.callback(true);
219
					}
220
					//Do a deep search now, should find the new item of the city with name attribute Cairo.
221
					store.fetch({query:{name:"Cairo"}, onComplete: onCompleteNewItemDeep, onError: onError, queryOptions: {deep:true}});
222
				}
223
				//Do a shallow search first, should find nothing.
224
				store.fetch({query:{name:"Cairo"}, onComplete: onCompleteNewItemShallow, onError: onError});
225
			}
226
			function onError(error, request){
227
				deferred.errback(error);
228
			}
229
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
230
			return deferred; //Object
231
		},
232
 
233
		function testWriteAPI_newItem_multiple_withParent(){
234
			//	summary:
235
			//		Simple test of the newItem API with a parent assignment multiple times.
236
			//	description:
237
			//		Simple test of the newItem API with a parent assignment multiple times.
238
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
239
 
240
			var deferred = new doh.Deferred();
241
 
242
			doh.assertTrue(!store.isDirty());
243
 
244
			function onComplete(items, request){
245
				doh.assertEqual(1, items.length);
246
				var item = items[0];
247
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
248
 
249
				//Attach an onNew to validate we get expected values.
250
				store.onNew = function(newItem, parentInfo){
251
					doh.assertEqual(item, parentInfo.item);
252
					doh.assertEqual("cities", parentInfo.attribute);
253
 
254
					doh.assertTrue(parentInfo.oldValue === undefined);
255
 
256
					doh.assertTrue(parentInfo.newValue === newItem);
257
				};
258
 
259
				//See if we can add in a new item representing the city of Cairo.
260
				//This should also call the onNew set above....
261
				var newItem1 = store.newItem({name: "Cairo", abbr: "Cairo"}, {parent: item, attribute: "cities"});
262
 
263
				//Attach a new onNew to validate we get expected values.
264
				store.onNew = function(newItem, parentInfo){
265
					doh.assertEqual(item, parentInfo.item);
266
					doh.assertEqual("cities", parentInfo.attribute);
267
 
268
					console.log(parentInfo.oldValue);
269
					doh.assertTrue(parentInfo.oldValue == newItem1);
270
 
271
					doh.assertTrue(parentInfo.newValue[0] == newItem1);
272
					doh.assertTrue(parentInfo.newValue[1] == newItem);
273
				};
274
				var newItem2 = store.newItem({name: "Banha", abbr: "Banha"}, {parent: item, attribute: "cities"});
275
 
276
				//Attach a new onNew to validate we get expected values.
277
				store.onNew = function(newItem, parentInfo){
278
					doh.assertEqual(item, parentInfo.item);
279
					doh.assertEqual("cities", parentInfo.attribute);
280
 
281
					doh.assertTrue(parentInfo.oldValue[0] == newItem1);
282
					doh.assertTrue(parentInfo.oldValue[1] == newItem2);
283
 
284
					doh.assertTrue(parentInfo.newValue[0] == newItem1);
285
					doh.assertTrue(parentInfo.newValue[1] == newItem2);
286
					doh.assertTrue(parentInfo.newValue[2] == newItem);
287
				};
288
				var newItem3 = store.newItem({name: "Damanhur", abbr: "Damanhur"}, {parent: item, attribute: "cities"});
289
				deferred.callback(true);
290
			}
291
			function onError(error, request){
292
				deferred.errback(error);
293
			}
294
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
295
			return deferred; //Object
296
		},
297
 
298
		function testWriteAPI_deleteItem(){
299
			//	summary:
300
			//		Simple test of the deleteItem API
301
			//	description:
302
			//		Simple test of the deleteItem API
303
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
304
 
305
			var deferred = new doh.Deferred();
306
			function onComplete(items, request){
307
				doh.assertEqual(1, items.length);
308
				var item = items[0];
309
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
310
				doh.assertTrue(store.isDirty(item) == false);
311
				doh.assertTrue(!store.isDirty());
312
				store.deleteItem(item);
313
				doh.assertTrue(store.isDirty(item));
314
				doh.assertTrue(store.isDirty());
315
				function onCompleteToo(itemsToo, requestToo) {
316
					doh.assertEqual(0, itemsToo.length);
317
					deferred.callback(true);
318
				}
319
				store.fetch({query:{name:"Egypt"}, onComplete: onCompleteToo, onError: onError});
320
			}
321
			function onError(error, request){
322
				deferred.errback(error);
323
			}
324
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
325
			return deferred; //Object
326
		},
327
		function testWriteAPI_isDirty(){
328
			//	summary:
329
			//		Simple test of the isDirty API
330
			//	description:
331
			//		Simple test of the isDirty API
332
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
333
 
334
			var deferred = new doh.Deferred();
335
			function onComplete(items, request) {
336
				doh.assertEqual(1, items.length);
337
				var item = items[0];
338
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
339
				store.setValue(item, "name", "Egypt 2");
340
				doh.assertTrue(store.getValue(item, "name") == "Egypt 2");
341
				doh.assertTrue(store.isDirty(item));
342
				deferred.callback(true);
343
			}
344
			function onError(error, request) {
345
				deferred.errback(error);
346
			}
347
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
348
			return deferred; //Object
349
		},
350
		function testWriteAPI_revert(){
351
			//	summary:
352
			//		Simple test of the revert API
353
			//	description:
354
			//		Simple test of the revert API
355
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
356
 
357
			var deferred = new doh.Deferred();
358
			function onComplete(items, request) {
359
				doh.assertEqual(1, items.length);
360
				var item = items[0];
361
				doh.assertTrue(store.containsValue(item, "name", "Egypt"));
362
				doh.assertTrue(store.isDirty(item) == false);
363
				doh.assertTrue(!store.isDirty());
364
				store.setValue(item, "name", "Egypt 2");
365
				doh.assertTrue(store.getValue(item, "name") == "Egypt 2");
366
				doh.assertTrue(store.isDirty(item));
367
				doh.assertTrue(store.isDirty());
368
				store.revert();
369
 
370
				//Fetch again to see if it reset the state.
371
				function onCompleteToo(itemsToo, requestToo){
372
					doh.assertEqual(1, itemsToo.length);
373
					var itemToo = itemsToo[0];
374
					doh.assertTrue(store.containsValue(itemToo, "name", "Egypt"));
375
					deferred.callback(true);
376
				}
377
				store.fetch({query:{name:"Egypt"}, onComplete: onCompleteToo, onError: onError});
378
			}
379
			function onError(error, request){
380
				deferred.errback(error);
381
			}
382
			store.fetch({query:{name:"Egypt"}, onComplete: onComplete, onError: onError});
383
			return deferred; //Object
384
		},
385
		function testWriteAPI_save(){
386
			//	summary:
387
			//		Simple test of the save API
388
			//	description:
389
			//		Simple test of the save API
390
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
391
 
392
			var deferred = new doh.Deferred();
393
			function onError(error){
394
				deferred.errback(error);
395
			}
396
			function onItem(item){
397
				store.setValue(item, "capital", "New Cairo");
398
				function onComplete() {
399
					deferred.callback(true);
400
				}
401
				store.save({onComplete:onComplete, onError:onError});
402
			}
403
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
404
			return deferred; //Object
405
		},
406
		function testWriteAPI_saveVerifyState(){
407
			//	summary:
408
			//		Simple test of the save API
409
			//	description:
410
			//		Simple test of the save API
411
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
412
 
413
			var deferred = new doh.Deferred();
414
			function onError(error){
415
				deferred.errback(error);
416
			}
417
			function onItem(item){
418
				store.setValue(item, "capital", "New Cairo");
419
				function onComplete() {
420
					//Check internal state.  Note:  Users should NOT do this, this is a UT verification
421
					//of internals in this case.  Ref tracker: #4394
422
					doh.assertTrue(!store._saveInProgress);
423
					deferred.callback(true);
424
				}
425
				store.save({onComplete:onComplete, onError:onError});
426
			}
427
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
428
			return deferred; //Object
429
		},
430
		function testWriteAPI_saveEverything(){
431
			//	summary:
432
			//		Simple test of the save API
433
			//	description:
434
			//		Simple test of the save API
435
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
436
			var egypt;
437
			store._saveEverything = function(saveCompleteCallback, saveFailedCallback, newFileContentString){
438
				var struct = dojo.fromJson(newFileContentString);
439
				doh.assertEqual(struct.identifier, store.getIdentityAttributes(egypt)[0]);
440
				doh.assertEqual(struct.label, store.getLabelAttributes(egypt)[0]);
441
				doh.assertEqual(struct.items.length, 7);
442
 
443
				var cloneStore = new dojo.data.ItemFileWriteStore({data:struct});
444
				function onItemClone(itemClone){
445
					var egyptClone = itemClone;
446
					doh.assertEqual(store.getIdentityAttributes(egypt)[0], cloneStore.getIdentityAttributes(egyptClone)[0]);
447
					doh.assertEqual(store.getLabelAttributes(egypt)[0], cloneStore.getLabelAttributes(egyptClone)[0]);
448
					doh.assertEqual(store.getValue(egypt, "name"), cloneStore.getValue(egyptClone, "name"));
449
				}
450
				cloneStore.fetchItemByIdentity({identity:"eg", onItem:onItemClone, onError:onError});
451
 
452
				saveCompleteCallback();
453
			};
454
 
455
			var deferred = new doh.Deferred();
456
			function onError(error){
457
				deferred.errback(error);
458
			}
459
			function onItem(item){
460
				egypt = item;
461
				function onComplete() {
462
					deferred.callback(true);
463
				}
464
				store.setValue(egypt, "capital", "New Cairo");
465
				store.save({onComplete:onComplete, onError:onError});
466
			}
467
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
468
			return deferred; //Object
469
		},
470
		function testWriteAPI_saveEverything_withDateType(){
471
			//	summary:
472
			//		Simple test of the save API	with a non-atomic type (Date) that has a type mapping.
473
			//	description:
474
			//		Simple test of the save API	with a non-atomic type (Date) that has a type mapping.
475
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
476
			store._saveEverything = function(saveCompleteCallback, saveFailedCallback, newFileContentString){
477
 
478
				//Now load the new data into a datastore and validate that it stored the date right.
479
				var dataset = dojo.fromJson(newFileContentString);
480
				var newStore = new dojo.data.ItemFileWriteStore({data: dataset});
481
 
482
				function gotItem(item){
483
					var independenceDate = newStore.getValue(item,"independence");
484
					doh.assertTrue(independenceDate instanceof Date);
485
					doh.assertTrue(dojo.date.compare(new Date(1993,04,24), independenceDate, "date") === 0);
486
					saveCompleteCallback();
487
				}
488
				function failed(error, request){
489
					deferred.errback(error);
490
					saveFailedCallback();
491
				}
492
				newStore.fetchItemByIdentity({identity:"eg", onItem:gotItem, onError:failed});
493
			};
494
 
495
			var deferred = new doh.Deferred();
496
			function onError(error){
497
				deferred.errback(error);
498
			}
499
			function onItem(item){
500
				function onComplete() {
501
					deferred.callback(true);
502
				}
503
				store.setValue(item, "independence", new Date(1993,04,24));
504
				store.save({onComplete:onComplete, onError:onError});
505
			}
506
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
507
			return deferred; //Object
508
		},
509
		function testWriteAPI_saveEverything_withCustomColorTypeSimple(){
510
			//	summary:
511
			//		Simple test of the save API	with a non-atomic type (dojo.Color) that has a type mapping.
512
			//	description:
513
			//		Simple test of the save API	with a non-atomic type (dojo.Color) that has a type mapping.
514
 
515
			//Set up the store basics:  What data it has, and what to do when save is called for saveEverything
516
			//And how to map the 'Color' type in and out of the format.
517
			//(Test of saving all to a some location...)
518
			var dataset = {
519
				identifier:'name',
520
				items: [
521
					{ name:'Kermit', species:'frog', color:{_type:'Color', _value:'green'} },
522
					{ name:'Beaker', hairColor:{_type:'Color', _value:'red'} }
523
				]
524
			};
525
 
526
			var customTypeMap = {'Color': dojo.Color };
527
 
528
			var store = new dojo.data.ItemFileWriteStore({
529
					data:dataset,
530
					typeMap: customTypeMap
531
			});
532
 
533
			store._saveEverything = function(saveCompleteCallback, saveFailedCallback, newFileContentString){
534
				//Now load the new data into a datastore and validate that it stored the Color right.
535
				var dataset = dojo.fromJson(newFileContentString);
536
				var newStore = new dojo.data.ItemFileWriteStore({data: dataset, typeMap: customTypeMap});
537
 
538
				function gotItem(item){
539
					var hairColor = newStore.getValue(item,"hairColor");
540
					doh.assertTrue(hairColor instanceof dojo.Color);
541
					doh.assertEqual("rgba(255, 255, 0, 1)", hairColor.toString());
542
					saveCompleteCallback();
543
				}
544
				function failed(error, request){
545
					deferred.errback(error);
546
					saveFailedCallback();
547
				}
548
				newStore.fetchItemByIdentity({identity:"Animal", onItem:gotItem, onError:failed});
549
			};
550
 
551
			//Add a new item with a color type, then save it.
552
			var deferred = new doh.Deferred();
553
			function onError(error){
554
				deferred.errback(error);
555
			}
556
			function onComplete() {
557
				deferred.callback(true);
558
			}
559
 
560
			var animal = store.newItem({name: "Animal", hairColor: new dojo.Color("yellow")});
561
			store.save({onComplete:onComplete, onError:onError});
562
			return deferred; //Object
563
		},
564
		function testWriteAPI_saveEverything_withCustomColorTypeGeneral(){
565
			//	summary:
566
			//		Simple test of the save API	with a non-atomic type (dojo.Color) that has a type mapping.
567
			//	description:
568
			//		Simple test of the save API	with a non-atomic type (dojo.Color) that has a type mapping.
569
 
570
			//Set up the store basics:  What data it has, and what to do when save is called for saveEverything
571
			//And how to map the 'Color' type in and out of the format.
572
			//(Test of saving all to a some location...)
573
			var dataset = {
574
				identifier:'name',
575
				items: [
576
					{ name:'Kermit', species:'frog', color:{_type:'Color', _value:'green'} },
577
					{ name:'Beaker', hairColor:{_type:'Color', _value:'red'} }
578
				]
579
			};
580
 
581
			var customTypeMap = {'Color': 	{
582
												type: dojo.Color,
583
												deserialize: function(value){
584
													return new dojo.Color(value);
585
												},
586
												serialize: function(obj){
587
													return obj.toString();
588
												}
589
											}
590
								}
591
			var store = new dojo.data.ItemFileWriteStore({
592
					data:dataset,
593
					typeMap: customTypeMap
594
			});
595
			store._saveEverything = function(saveCompleteCallback, saveFailedCallback, newFileContentString){
596
				//Now load the new data into a datastore and validate that it stored the Color right.
597
				var dataset = dojo.fromJson(newFileContentString);
598
				var newStore = new dojo.data.ItemFileWriteStore({data: dataset, typeMap: customTypeMap});
599
 
600
				function gotItem(item){
601
					var hairColor = newStore.getValue(item,"hairColor");
602
					doh.assertTrue(hairColor instanceof dojo.Color);
603
					doh.assertEqual("rgba(255, 255, 0, 1)", hairColor.toString());
604
					saveCompleteCallback();
605
				}
606
				function failed(error, request){
607
					deferred.errback(error);
608
					saveFailedCallback();
609
				}
610
				newStore.fetchItemByIdentity({identity:"Animal", onItem:gotItem, onError:failed});
611
			};
612
 
613
			//Add a new item with a color type, then save it.
614
			var deferred = new doh.Deferred();
615
			function onError(error){
616
				deferred.errback(error);
617
			}
618
			function onComplete() {
619
				deferred.callback(true);
620
			}
621
 
622
			var animal = store.newItem({name: "Animal", hairColor: new dojo.Color("yellow")});
623
			store.save({onComplete:onComplete, onError:onError});
624
			return deferred; //Object
625
		},
626
		function testWriteAPI_newItem_revert(){
627
			//	summary:
628
			//		Test for bug #5357.  Ensure that the revert properly nulls the identity position
629
			//      for a new item after revert.
630
			var args = {data: {
631
				label:"name",
632
				items:[
633
					{name:'Ecuador', capital:'Quito'},
634
					{name:'Egypt', capital:'Cairo'},
635
					{name:'El Salvador', capital:'San Salvador'},
636
					{name:'Equatorial Guinea', capital:'Malabo'},
637
					{name:'Eritrea', capital:'Asmara'},
638
					{name:'Estonia', capital:'Tallinn'},
639
					{name:'Ethiopia', capital:'Addis Ababa'}
640
				]
641
			} };
642
			var store = new dojo.data.ItemFileWriteStore(args);
643
 
644
			var newCountry = store.newItem({name: "Utopia", capitol: "Perfect"});
645
 
646
			//DO NOT ACCESS THIS WAY.  THESE ARE INTERNAL VARIABLES.  DOING THIS FOR TEST PURPOSES.
647
			var itemEntryNum = newCountry[store._itemNumPropName];
648
			doh.assertTrue(store._arrayOfAllItems[itemEntryNum] === newCountry);
649
			store.revert();
650
			doh.assertTrue(store._arrayOfAllItems[itemEntryNum] === null);
651
		},
652
		function testNotificationAPI_onSet(){
653
			//	summary:
654
			//		Simple test of the onSet API
655
			//	description:
656
			//		Simple test of the onSet API
657
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
658
 
659
			var deferred = new doh.Deferred();
660
			function onError(error){
661
				deferred.errback(error);
662
			}
663
			function onItem(fetchedItem){
664
				var egypt = fetchedItem;
665
				var connectHandle = null;
666
				function setValueHandler(item, attribute, oldValue, newValue){
667
					doh.assertTrue(store.isItem(item));
668
					doh.assertTrue(item == egypt);
669
					doh.assertTrue(attribute == "capital");
670
					doh.assertTrue(oldValue == "Cairo");
671
					doh.assertTrue(newValue == "New Cairo");
672
					deferred.callback(true);
673
					dojo.disconnect(connectHandle);
674
				}
675
				connectHandle = dojo.connect(store, "onSet", setValueHandler);
676
				store.setValue(egypt, "capital", "New Cairo");
677
			}
678
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
679
		},
680
		function testNotificationAPI_onNew(){
681
			//	summary:
682
			//		Simple test of the onNew API
683
			//	description:
684
			//		Simple test of the onNew API
685
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
686
 
687
			var deferred = new doh.Deferred();
688
			var connectHandle = null;
689
			function newItemHandler(item){
690
				doh.assertTrue(store.isItem(item));
691
				doh.assertTrue(store.getValue(item, "name") == "Canada");
692
				deferred.callback(true);
693
				dojo.disconnect(connectHandle);
694
			}
695
			connectHandle = dojo.connect(store, "onNew", newItemHandler);
696
			var canada = store.newItem({name:"Canada", abbr:"ca", capital:"Ottawa"});
697
		},
698
		function testNotificationAPI_onDelete(){
699
			//	summary:
700
			//		Simple test of the onDelete API
701
			//	description:
702
			//		Simple test of the onDelete API
703
			var store = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
704
 
705
			var deferred = new doh.Deferred();
706
			function onError(error){
707
				deferred.errback(error);
708
			}
709
			function onItem(fetchedItem){
710
				var egypt = fetchedItem;
711
				var connectHandle = null;
712
				function deleteItemHandler(item){
713
					doh.assertTrue(store.isItem(item) == false);
714
					doh.assertTrue(item == egypt);
715
					deferred.callback(true);
716
					dojo.disconnect(connectHandle);
717
				}
718
				connectHandle = dojo.connect(store, "onDelete", deleteItemHandler);
719
				store.deleteItem(egypt);
720
			}
721
			store.fetchItemByIdentity({identity:"eg", onItem:onItem, onError:onError});
722
		},
723
		function testReadAPI_functionConformanceToo(){
724
			//	summary:
725
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.
726
			//	description:
727
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.
728
			var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
729
			var readApi = new dojo.data.api.Read();
730
			var passed = true;
731
 
732
			for(var functionName in readApi){
733
				var member = readApi[functionName];
734
				//Check that all the 'Read' defined functions exist on the test store.
735
				if(typeof member === "function"){
736
					var testStoreMember = testStore[functionName];
737
					if(!(typeof testStoreMember === "function")){
738
						passed = false;
739
						break;
740
					}
741
				}
742
			}
743
			doh.assertTrue(passed);
744
		},
745
		function testWriteAPI_functionConformance(){
746
			//	summary:
747
			//		Simple test write API conformance.  Checks to see all declared functions are actual functions on the instances.
748
			//	description:
749
			//		Simple test write API conformance.  Checks to see all declared functions are actual functions on the instances.
750
			var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
751
			var writeApi = new dojo.data.api.Write();
752
			var passed = true;
753
 
754
			for(var functionName in writeApi){
755
				var member = writeApi[functionName];
756
				//Check that all the 'Write' defined functions exist on the test store.
757
				if(typeof member === "function"){
758
					var testStoreMember = testStore[functionName];
759
					if(!(typeof testStoreMember === "function")){
760
						passed = false;
761
						break;
762
					}
763
				}
764
			}
765
			doh.assertTrue(passed);
766
		},
767
		function testNotificationAPI_functionConformance(){
768
			//	summary:
769
			//		Simple test Notification API conformance.  Checks to see all declared functions are actual functions on the instances.
770
			//	description:
771
			//		Simple test Notification API conformance.  Checks to see all declared functions are actual functions on the instances.
772
			var testStore = new dojo.data.ItemFileWriteStore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
773
			var api = new dojo.data.api.Notification();
774
			var passed = true;
775
 
776
			for(var functionName in api){
777
				var member = api[functionName];
778
				//Check that all the 'Write' defined functions exist on the test store.
779
				if(typeof member === "function"){
780
					var testStoreMember = testStore[functionName];
781
					if(!(typeof testStoreMember === "function")){
782
						passed = false;
783
						break;
784
					}
785
				}
786
			}
787
			doh.assertTrue(passed);
788
		},
789
		function testIdentityAPI_noIdentifierSpecified(){
790
			//	summary:
791
			//		Test for bug #3873. Given a datafile that does not specify an
792
			//		identifier, make sure ItemFileWriteStore auto-creates identities
793
			//		that are unique even after calls to deleteItem() and newItem()
794
			var args = {data: {
795
				label:"name",
796
				items:[
797
					{name:'Ecuador', capital:'Quito'},
798
					{name:'Egypt', capital:'Cairo'},
799
					{name:'El Salvador', capital:'San Salvador'},
800
					{name:'Equatorial Guinea', capital:'Malabo'},
801
					{name:'Eritrea', capital:'Asmara'},
802
					{name:'Estonia', capital:'Tallinn'},
803
					{name:'Ethiopia', capital:'Addis Ababa'}
804
				]
805
			} };
806
			var store = new dojo.data.ItemFileWriteStore(args);
807
			var deferred = new doh.Deferred();
808
 
809
			var onError = function(error, request){
810
				deferred.errback(error);
811
			}
812
			var onComplete = function(items, request){
813
				doh.assertEqual(7, items.length);
814
 
815
				var lastItem = items[(items.length - 1)];
816
				var idOfLastItem = store.getIdentity(lastItem);
817
				store.deleteItem(lastItem);
818
				store.newItem({name:'Canada', capital:'Ottawa'});
819
 
820
				var onCompleteAgain = function(itemsAgain, requestAgain){
821
					doh.assertEqual(7, itemsAgain.length);
822
					var identitiesInUse = {};
823
					for(var i = 0; i < itemsAgain.length; ++i){
824
						var item = itemsAgain[i];
825
						var id = store.getIdentity(item);
826
						if(identitiesInUse.hasOwnProperty(id)){
827
							// there should not already be an entry for this id
828
							doh.assertTrue(false);
829
						}else{
830
							// we want to add the entry now
831
							identitiesInUse[id] = item;
832
						}
833
					}
834
					deferred.callback(true);
835
				}
836
				store.fetch({onComplete:onCompleteAgain, onError:onError});
837
			}
838
 
839
			store.fetch({onComplete:onComplete, onError:onError});
840
			return deferred;
841
		},
842
		function testIdentityAPI_noIdentifierSpecified_revert(){
843
			//	summary:
844
			//		Test for bug #4691  Given a datafile that does not specify an
845
			//		identifier, make sure ItemFileWriteStore auto-creates identities
846
			//		that are unique even after calls to deleteItem() and newItem()
847
			var args = {data: {
848
				label:"name",
849
				items:[
850
					{name:'Ecuador', capital:'Quito'},
851
					{name:'Egypt', capital:'Cairo'},
852
					{name:'El Salvador', capital:'San Salvador'},
853
					{name:'Equatorial Guinea', capital:'Malabo'},
854
					{name:'Eritrea', capital:'Asmara'},
855
					{name:'Estonia', capital:'Tallinn'},
856
					{name:'Ethiopia', capital:'Addis Ababa'}
857
				]
858
			} };
859
			var store = new dojo.data.ItemFileWriteStore(args);
860
			var deferred = new doh.Deferred();
861
 
862
			var onError = function(error, request){
863
				deferred.errback(error);
864
			}
865
			var onComplete = function(items, request){
866
				doh.assertEqual(7, items.length);
867
 
868
				var lastItem = items[(items.length - 1)];
869
				var idOfLastItem = store.getIdentity(lastItem);
870
				store.deleteItem(lastItem);
871
				store.newItem({name:'Canada', capital:'Ottawa'});
872
 
873
				var onCompleteAgain = function(itemsAgain, requestAgain){
874
					doh.assertEqual(7, itemsAgain.length);
875
					var identitiesInUse = {};
876
					for(var i = 0; i < itemsAgain.length; ++i){
877
						var item = itemsAgain[i];
878
						var id = store.getIdentity(item);
879
						if(identitiesInUse.hasOwnProperty(id)){
880
							// there should not already be an entry for this id
881
							doh.assertTrue(false);
882
						}else{
883
							// we want to add the entry now
884
							identitiesInUse[id] = item;
885
						}
886
					}
887
					//Last test, revert everything and check item sizes.
888
					store.revert();
889
 
890
					//Now call fetch again and verify store state.
891
					var revertComplete = function(itemsReverted, request){
892
						doh.assertEqual(7, itemsReverted.length);
893
						deferred.callback(true);
894
					}
895
					store.fetch({onComplete:revertComplete, onError:onError});
896
				}
897
				store.fetch({onComplete:onCompleteAgain, onError:onError});
898
			}
899
			store.fetch({onComplete:onComplete, onError:onError});
900
			return deferred;
901
		}
902
	]
903
);
904
 
905
 
906
 
907
}