aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/data/tests/stores/QueryReadStore.js
blob: f725f066f70091b4ba8d4a8f238f5f7c7c3d4cda (plain)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
if(!dojo._hasResource["dojox.data.tests.stores.QueryReadStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.data.tests.stores.QueryReadStore"] = true;
dojo.provide("dojox.data.tests.stores.QueryReadStore");
dojo.require("dojox.data.QueryReadStore");
dojo.require("dojo.data.api.Read");

//dojo.require("dojox.testing.DocTest");

dojox.data.tests.stores.QueryReadStore.getStore = function(){
	return new dojox.data.QueryReadStore({
			url: dojo.moduleUrl("dojox.data.tests", "stores/QueryReadStore.php").toString()
		});
};


tests.register("dojox.data.tests.stores.QueryReadStore", 
	[
		/*
		function testDocTests(t) {
			//	summary:
			// 		Run all the doc comments.
			var doctest = new dojox.testing.DocTest();
			doctest.run("dojox.data.QueryReadStore");
			t.assertTrue(doctest.errors.length==0);
		},
		*/
		
		function testReadApi_getValue(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The good cases.
				t.assertEqual("Alabama", store.getValue(item, "name"));
				t.assertEqual("<img src='images/Alabama.jpg'/>Alabama", store.getValue(item, "label"));
				t.assertEqual("AL", store.getValue(item, "abbreviation"));
				// Test the defaultValue cases (the third paramter).
				t.assertEqual("default value", store.getValue(item, "NAME", "default value"));
				// TODO Test for null somehow ...
				// Read api says: Returns null if and only if null was explicitly set as the attribute value.
				
				// According to Read-API getValue() an exception is thrown when
				// the item is not an item or when the attribute is not a string.
				t.assertError(Error, store, "getValue", ["not an item", "NOT THERE"]);
				t.assertError(Error, store, "getValue", [item, {}]);
				
				d.callback(true);
			}
			store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_getValues(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The good cases.
				t.assertEqual(["Alabama"], store.getValues(item, "name"));
				t.assertEqual(["<img src='images/Alabama.jpg'/>Alabama"], store.getValues(item, "label"));
				t.assertEqual(["AL"], store.getValues(item, "abbreviation"));
				// TODO Test for null somehow ...
				// Read api says: Returns null if and only if null was explicitly set as the attribute value.

				// Test for not-existing attributes without defaultValues and invalid items.
				// TODO
				t.assertEqual([], store.getValues(item, "NOT THERE"));
				var errThrown = false;
				try{
					//Should throw an exception.
					var values = store.getValues("not an item", "NOT THERE");
				}catch (e){
					errThrown = true;
				}
				t.assertTrue(errThrown);
				d.callback(true);
			}
			store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
			return d; //Object
		},
		
		function testReadApi_getAttributes(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The good case(s).
				t.assertEqual(['id', 'name', 'label', 'abbreviation', 'capital'], store.getAttributes(item));
				t.assertError(Error, store, "getAttributes", [{}]);
				
				d.callback(true);
			}
			store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_getLabel(t){
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The good cases.
				t.assertEqual(["<img src='images/Alabama.jpg'/>Alabama"], store.getLabel(item));
				d.callback(true);
			}
			store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_hasAttribute(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The positive cases.
				t.assertEqual(true, store.hasAttribute(item, "name"));
				t.assertEqual(true, store.hasAttribute(item, "label"));
				t.assertEqual(true, store.hasAttribute(item, "abbreviation"));
				// Make sure attribute case doesnt matter.
				t.assertEqual(false, store.hasAttribute(item, "NAME"));
				t.assertEqual(false, store.hasAttribute(item, "Name"));
				t.assertEqual(false, store.hasAttribute(item, "Label"));
				// Pass in an invalid item.
				t.assertEqual(false, store.hasAttribute({}, "abbreviation"));
				// pass in something that looks like the item with the attribute.
				t.assertEqual(false, store.hasAttribute({name:"yo"}, "name"));
				
				d.callback(true);
			}
			store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_containsValue(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();

			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				t.assertTrue(store.containsValue(item, "name", "Alaska"));
				d.callback(true);
			}
			store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_isItem(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				// The good case.
				t.assertEqual(true, store.isItem(items[0]));
				// Try a pure object.
				t.assertEqual(false, store.isItem({}));
				// Try to look like an item.
				t.assertEqual(false, store.isItem({name:"Alaska", label:"Alaska", abbreviation:"AK"}));
				d.callback(true);
			}
			store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
			return d; //Object
		},

		function testReadApi_isItemLoaded(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				var item = items[0];
				// The good case(s).
				t.assertTrue(store.isItemLoaded(item));
				
				d.callback(true);
			}
			store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
			return d; //Object
		},

		//function testReadApi_loadItem(t){
		//	//	summary: 
		//	//	description:
		//	t.assertTrue(false);
		//},

		function testReadApi_fetch_all(t){
			//	summary: 
			//		Simple test of fetching all items.
			//	description:
			//		Simple test of fetching all items.
			var store = dojox.data.tests.stores.QueryReadStore.getStore();

			var d = new doh.Deferred();
			function onComplete(items, request) {
				t.assertEqual(8, items.length);
				d.callback(true);
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{q:"m"}, onComplete: onComplete, onError: onError});
			return d; //Object
		},
		
		function testReadApi_fetch_onBegin(t){
			//	summary: 
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			//	description:
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			var store = dojox.data.tests.stores.QueryReadStore.getStore();

			var d = new doh.Deferred();
			var passed = false;
			function onBegin(size, request){
				t.assertEqual(8, size);
				passed = true;
			}
			function onComplete(items, request) {
				t.assertEqual(5, items.length);
				if(passed){
					d.callback(true);
				}else{
					d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
				}
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{q:"m"}, start: 0, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
			return d; //Object
		},

		function testReadApi_fetch_onBegin_ServersidePaging(t){
			//	summary: 
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			//	description:
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			var store = dojox.data.tests.stores.QueryReadStore.getStore();

			var d = new doh.Deferred();
			var passed = false;
			function onBegin(size, request){
				t.assertEqual(8, size);
				passed = true;
			}
			function onComplete(items, request) {
				t.assertEqual(3, items.length);
				if(passed){
					d.callback(true);
				}else{
					d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
				}
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{q:"m"}, start: 5, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
			return d; //Object
		},

		function testReadApi_fetch_onBegin_ClientsidePaging(t){
			//	summary: 
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			//	description:
			//		Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			store.doClientPaging = true;

			var d = new doh.Deferred();
			var passed = false;
			function onBegin(size, request){
				t.assertEqual(8, size);
				passed = true;
			}
			function onComplete(items, request) {
				t.assertEqual(5, items.length);
				if(passed){
					d.callback(true);
				}else{
					d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
				}
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{q:"m"}, start: 0, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
			return d; //Object
		},

		function testReadApi_fetch_one(t){
			//	summary: 
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			
			var d = new doh.Deferred();
			function onComplete(items, request){
				t.assertEqual(1, items.length);
				d.callback(true);
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{q:"Alaska"}, onComplete: onComplete, onError: onError});
			return d; //Object
		},

		function testReadApi_fetch_client_paging(t){
			//	summary:
			//		Lets test that paging on the same request does not trigger
			//		server requests.
			//	description:
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			store.doClientPaging = true;

			var lastRequestHash = null;
			var firstItems = [];
			var d = new doh.Deferred();
			function onComplete(items, request) {
				t.assertEqual(5, items.length);
				lastRequestHash = store.lastRequestHash;
				firstItems = items;
				
				// Do the next request AFTER the previous one, so we are sure its sequential.
				// We need to be sure so we can compare to the data from the first request.
				function onComplete1(items, request) {
					t.assertEqual(5, items.length);
					t.assertEqual(lastRequestHash, store.lastRequestHash);
					t.assertEqual(firstItems[1], items[0]);
					d.callback(true);
				}
				req.start = 1;
				req.onComplete = onComplete1;
				store.fetch(req);
			}
			function onError(error, request) {
				d.errback(error);
			}
			var req = {query:{q:"m"}, start:0, count:5,
						onComplete: onComplete, onError: onError};
			store.fetch(req);
			return d; //Object
		},
		
		function testReadApi_fetch_server_paging(t) {
			// Verify that the paging on the server side does work.
			// This is the test for http://trac.dojotoolkit.org/ticket/4761
			//
			// How? We request 10 items from the server, start=0, count=10.
			// The second request requests 5 items: start=5, count=5 and those
			// 5 items should have the same values as the last 5 of the first
			// request.
			// This tests if the server side paging does work.
			var store = dojox.data.tests.stores.QueryReadStore.getStore();

			var lastRequestHash = null;
			var d = new doh.Deferred();
			function onComplete(items, request) {
				t.assertEqual(10, items.length);
				lastRequestHash = store.lastRequestHash;
				firstItems = items;
				
				// Do the next request AFTER the previous one, so we are sure its sequential.
				// We need to be sure so we can compare to the data from the first request.
				function onComplete1(items, request) {
					t.assertEqual(5, items.length);
					// Compare the hash of the last request, they must be different,
					// since another server request was issued.
					t.assertTrue(lastRequestHash!=store.lastRequestHash);
					t.assertEqual(store.getValue(firstItems[5], "name"), store.getValue(items[0], "name"));
					t.assertEqual(store.getValue(firstItems[6], "name"), store.getValue(items[1], "name"));
					t.assertEqual(store.getValue(firstItems[7], "name"), store.getValue(items[2], "name"));
					t.assertEqual(store.getValue(firstItems[8], "name"), store.getValue(items[3], "name"));
					t.assertEqual(store.getValue(firstItems[9], "name"), store.getValue(items[4], "name"));
					d.callback(true);
				}
				// Init a new store, or it will use the old data, since the query has not changed.
				store.doClientPaging = false;
				store.fetch({start:5, count:5, onComplete: onComplete1, onError: onError});
			}
			function onError(error, request) {
				d.errback(error);
			}
			store.fetch({query:{}, start:0, count:10, onComplete: onComplete, onError: onError});
			return d; //Object
		},
		
		function testReadApi_getFeatures(t) {
			var store = dojox.data.tests.stores.QueryReadStore.getStore();
			var features = store.getFeatures();
			t.assertTrue(features["dojo.data.api.Read"]);
			t.assertTrue(features["dojo.data.api.Identity"]);
			var count = 0;
			for (i in features){
				count++;
			}
			t.assertEqual(2, count);
		},
		function testReadAPI_functionConformance(t){
			//	summary:
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.
			//	description:
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.

			var testStore = dojox.data.tests.stores.QueryReadStore.getStore();
			var readApi = new dojo.data.api.Read();
			var passed = true;

			for(i in readApi){
				var member = readApi[i];
				//Check that all the 'Read' defined functions exist on the test store.
				if(typeof member === "function"){
					var testStoreMember = testStore[i];
					if(!(typeof testStoreMember === "function")){
						console.log("Problem with function: [" + i + "]");
						passed = false;
						break;
					}
				}
			}
			t.assertTrue(passed);
		}
	]
);

}