Debian Codegraph @

dojox/data/tests/stores/KeyValueStore.js

raw symbols SCIP
1 dojo.provide("dojox.data.tests.stores.KeyValueStore");
2 dojo.require("dojox.data.KeyValueStore");
3 dojo.require("dojo.data.api.Read");
4 dojo.require("dojo.data.api.Identity");
5
6 dojox.data.tests.stores.KeyValueStore.getDatasource = function(type){
7 // summary:
8 // A simple helper function for getting the sample data used in each of the tests.
9 // description:
10 // A simple helper function for getting the sample data used in each of the tests.
11
12 var dataSource = {};
13 var filepath = "stores/properties.js";
14 if(dojo.isBrowser){
15 dataSource.url = require.toUrl("dojox/data/tests/" + filepath).toString();
16 }else{
17 // When running tests in Rhino, xhrGet is not available,
18 // so we have the file data in the code below.
19 var keyData = "/*[";
20 // Properties of December 1, 2007
21 keyData += '{ "year": "2007" },';
22 keyData += '{ "nmonth": "12" },';
23 keyData += '{ "month": "December" },';
24 keyData += '{ "nday": "1" },';
25 keyData += '{ "day": "Saturday" },';
26 keyData += '{ "dayOfYear": "335" },';
27 keyData += '{ "weekOfYear": "48" }';
28 keyData += ']*/';
29 dataSource.data = keyData;
30 }
31 return dataSource; //Object
32 };
33
34 dojox.data.tests.stores.KeyValueStore.verifyItems = function(keyStore, items, attribute, compareArray){
35 // summary:
36 // A helper function for validating that the items array is ordered
37 // the same as the compareArray
38 if(items.length != compareArray.length){ return false; }
39 for(var i = 0; i < items.length; i++){
40 if(!(keyStore.getValue(items[i], attribute) === compareArray[i])){
41 return false; //Boolean
42 }
43 }
44 return true; //Boolean
45 };
46
47 dojox.data.tests.stores.KeyValueStore.error = function(t, d, errData){
48 // summary:
49 // The error callback function to be used for all of the tests.
50 for (i in errData) {
51 console.log(errData[i]);
52 }
53 d.errback(errData);
54 };
55
56 doh.register("dojox.data.tests.stores.KeyValueStore",
57 [
58 function testReadAPI_fetch_all(t){
59 // summary:
60 // Simple test of a basic fetch on KeyValueStore.
61 // description:
62 // Simple test of a basic fetch on KeyValueStore.
63
64 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/properties.js");
65 var keyStore = new dojox.data.KeyValueStore(args);
66
67 var d = new doh.Deferred();
68 function completedAll(items){
69 t.assertTrue((items.length === 7));
70 d.callback(true);
71 }
72
73 //Get everything...
74 keyStore.fetch({ onComplete: completedAll, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
75 return d; //Object
76 },
77 function testReadAPI_fetch_all_withEmptyStringField(t){
78 // summary:
79 // Simple test of a basic fetch on KeyValueStore.
80 // description:
81 // Simple test of a basic fetch on KeyValueStore.
82
83 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
84 var keyStore = new dojox.data.KeyValueStore(args);
85
86 var d = new doh.Deferred();
87 function completedAll(items){
88 t.assertTrue((items.length === 7));
89 d.callback(true);
90 }
91
92 //Get everything...
93 keyStore.fetch({ onComplete: completedAll, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
94 return d; //Object
95 },
96 function testReadAPI_fetch_one(t){
97 // summary:
98 // Simple test of a basic fetch on KeyValueStore of a single item.
99 // description:
100 // Simple test of a basic fetch on KeyValueStore of a single item.
101
102 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
103 var keyStore = new dojox.data.KeyValueStore(args);
104
105 var d = new doh.Deferred();
106 function onComplete(items, request){
107 t.is(1, items.length);
108 d.callback(true);
109 }
110 keyStore.fetch({ query: {key: "year"},
111 onComplete: onComplete,
112 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
113 });
114 return d; //Object
115 },
116 function testReadAPI_fetch_Multiple(t){
117 // summary:
118 // Simple test of a basic fetch on KeyValueStore of a single item.
119 // description:
120 // Simple test of a basic fetch on KeyValueStore of a single item.
121
122 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
123 var keyStore = new dojox.data.KeyValueStore(args);
124
125 var d = new doh.Deferred();
126
127 var done = [false, false];
128
129 function onCompleteOne(items, request){
130 done[0] = true;
131 t.is(1, items.length);
132 if(done[0] && done[1]){
133 d.callback(true);
134 }
135 }
136
137 function onCompleteTwo(items, request){
138 done[1] = true;
139 t.is(1, items.length);
140 if(done[0] && done[1]){
141 d.callback(true);
142 }
143 }
144
145 try
146 {
147 keyStore.fetch({ query: {key: "year"},
148 onComplete: onCompleteOne,
149 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
150 });
151 keyStore.fetch({ query: {key: "month"},
152 onComplete: onCompleteTwo,
153 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
154 });
155 }
156 catch(e)
157 {
158 for (i in e) {
159 console.log(e[i]);
160 }
161 }
162
163 return d; //Object
164 },
165 function testReadAPI_fetch_MultipleMixed(t){
166 // summary:
167 // Simple test of a basic fetch on KeyValueStore of a single item.
168 // description:
169 // Simple test of a basic fetch on KeyValueStore of a single item.
170
171 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
172 var keyStore = new dojox.data.KeyValueStore(args);
173
174 var d = new doh.Deferred();
175
176 var done = [false, false];
177 function onComplete(items, request){
178 done[0] = true;
179 t.is(1, items.length);
180 if(done[0] && done[1]){
181 d.callback(true);
182 }
183 }
184
185 function onItem(item){
186 done[1] = true;
187 t.assertTrue(item !== null);
188 t.is('year', keyStore.getValue(item,"key"));
189 t.is(2007, keyStore.getValue(item,"value"));
190 t.is(2007, keyStore.getValue(item,"year"));
191 if(done[0] && done[1]){
192 d.callback(true);
193 }
194 }
195
196 keyStore.fetch({ query: {key: "day"},
197 onComplete: onComplete,
198 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
199 });
200
201 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
202 return d; //Object
203 },
204 function testReadAPI_fetch_all_streaming(t){
205 // summary:
206 // Simple test of a basic fetch on KeyValueStore.
207 // description:
208 // Simple test of a basic fetch on KeyValueStore.
209
210 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
211 var keyStore = new dojox.data.KeyValueStore(args);
212
213 var d = new doh.Deferred();
214 count = 0;
215
216 function onBegin(size, requestObj){
217 t.assertTrue(size === 7);
218 }
219 function onItem(item, requestObj){
220 t.assertTrue(keyStore.isItem(item));
221 count++;
222 }
223 function onComplete(items, request){
224 t.is(7, count);
225 t.is(null, items);
226 d.callback(true);
227 }
228
229 //Get everything...
230 keyStore.fetch({ onBegin: onBegin,
231 onItem: onItem,
232 onComplete: onComplete,
233 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
234 });
235 return d; //Object
236 },
237 function testReadAPI_fetch_paging(t){
238 // summary:
239 // Test of multiple fetches on a single result. Paging, if you will.
240 // description:
241 // Test of multiple fetches on a single result. Paging, if you will.
242
243 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
244 var keyStore = new dojox.data.KeyValueStore(args);
245
246 var d = new doh.Deferred();
247 function dumpFirstFetch(items, request){
248 t.is(5, items.length);
249 request.start = 3;
250 request.count = 1;
251 request.onComplete = dumpSecondFetch;
252 keyStore.fetch(request);
253 }
254
255 function dumpSecondFetch(items, request){
256 t.is(1, items.length);
257 request.start = 0;
258 request.count = 5;
259 request.onComplete = dumpThirdFetch;
260 keyStore.fetch(request);
261 }
262
263 function dumpThirdFetch(items, request){
264 t.is(5, items.length);
265 request.start = 2;
266 request.count = 20;
267 request.onComplete = dumpFourthFetch;
268 keyStore.fetch(request);
269 }
270
271 function dumpFourthFetch(items, request){
272 t.is(5, items.length);
273 request.start = 9;
274 request.count = 100;
275 request.onComplete = dumpFifthFetch;
276 keyStore.fetch(request);
277 }
278
279 function dumpFifthFetch(items, request){
280 t.is(0, items.length);
281 request.start = 2;
282 request.count = 20;
283 request.onComplete = dumpSixthFetch;
284 keyStore.fetch(request);
285 }
286
287 function dumpSixthFetch(items, request){
288 t.is(5, items.length);
289 d.callback(true);
290 }
291
292 function completed(items, request){
293 t.is(7, items.length);
294 request.start = 1;
295 request.count = 5;
296 request.onComplete = dumpFirstFetch;
297 keyStore.fetch(request);
298 }
299
300 keyStore.fetch({onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
301 return d; //Object
302 },
303
304 function testReadAPI_getLabel(t){
305 // summary:
306 // Simple test of the getLabel function against a store set that has a label defined.
307 // description:
308 // Simple test of the getLabel function against a store set that has a label defined.
309
310 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
311 var keyStore = new dojox.data.KeyValueStore(args);
312
313 var d = new doh.Deferred();
314 function onComplete(items, request){
315 t.assertEqual(items.length, 1);
316 var label = keyStore.getLabel(items[0]);
317 t.assertTrue(label !== null);
318 t.assertEqual("year", label);
319 d.callback(true);
320 }
321 keyStore.fetch({ query: {key: "year"},
322 onComplete: onComplete,
323 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
324 });
325 return d;
326 },
327 function testReadAPI_getLabelAttributes(t){
328 // summary:
329 // Simple test of the getLabelAttributes function against a store set that has a label defined.
330 // description:
331 // Simple test of the getLabelAttributes function against a store set that has a label defined.
332
333 var args = dojox.data.tests.stores.KeyValueStore.getDatasource();
334 var keyStore = new dojox.data.KeyValueStore(args);
335
336 var d = new doh.Deferred();
337 function onComplete(items, request){
338 t.assertEqual(items.length, 1);
339 var labelList = keyStore.getLabelAttributes(items[0]);
340 t.assertTrue(dojo.isArray(labelList));
341 t.assertEqual("key", labelList[0]);
342 d.callback(true);
343 }
344 keyStore.fetch({ query: {key: "year"},
345 onComplete: onComplete,
346 onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)
347 });
348 return d;
349 },
350 function testReadAPI_getValue(t){
351 // summary:
352 // Simple test of the getValue function of the store.
353 // description:
354 // Simple test of the getValue function of the store.
355
356 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
357 var keyStore = new dojox.data.KeyValueStore(args);
358
359 var d = new doh.Deferred();
360 function onItem(item){
361 t.assertTrue(item !== null);
362 t.is("nday", keyStore.getValue(item,"key"));
363 t.is(1, keyStore.getValue(item,"value"));
364 t.is(1, keyStore.getValue(item,"nday"));
365 d.callback(true);
366 }
367 keyStore.fetchItemByIdentity({identity: "nday", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
368 return d;
369 },
370 function testReadAPI_getValue_2(t){
371 // summary:
372 // Simple test of the getValue function of the store.
373 // description:
374 // Simple test of the getValue function of the store.
375
376 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
377 var keyStore = new dojox.data.KeyValueStore(args);
378
379 var d = new doh.Deferred();
380 function onItem(item){
381 t.assertTrue(item !== null);
382 t.is("day", keyStore.getValue(item,"key"));
383 t.is("Saturday", keyStore.getValue(item,"value"));
384 t.is("Saturday", keyStore.getValue(item,"day"));
385 d.callback(true);
386 }
387 keyStore.fetchItemByIdentity({identity: "day", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
388 return d;
389 },
390 function testReadAPI_getValue_3(t){
391 // summary:
392 // Simple test of the getValue function of the store.
393 // description:
394 // Simple test of the getValue function of the store.
395
396 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
397 var keyStore = new dojox.data.KeyValueStore(args);
398
399 var d = new doh.Deferred();
400 function onItem(item){
401 t.assertTrue(item !== null);
402 t.is("dayOfYear", keyStore.getValue(item,"key"));
403 t.is(335, keyStore.getValue(item,"value"));
404 t.is(335, keyStore.getValue(item,"dayOfYear"));
405 d.callback(true);
406 }
407 keyStore.fetchItemByIdentity({identity: "dayOfYear", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
408 return d;
409 },
410 function testReadAPI_getValue_4(t){
411 // summary:
412 // Simple test of the getValue function of the store.
413 // description:
414 // Simple test of the getValue function of the store.
415
416 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
417 var keyStore = new dojox.data.KeyValueStore(args);
418
419 var d = new doh.Deferred();
420 function onItem(item){
421 t.assertTrue(item !== null);
422 t.is("weekOfYear", keyStore.getValue(item,"key"));
423 t.is(48, keyStore.getValue(item,"value"));
424 t.is(48, keyStore.getValue(item,"weekOfYear"));
425 d.callback(true);
426 }
427 keyStore.fetchItemByIdentity({identity: "weekOfYear", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
428 return d;
429 },
430 function testReadAPI_getValues(t){
431 // summary:
432 // Simple test of the getValues function of the store.
433 // description:
434 // Simple test of the getValues function of the store.
435
436 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
437 var keyStore = new dojox.data.KeyValueStore(args);
438
439 var d = new doh.Deferred();
440 function onItem(item){
441 t.assertTrue(item !== null);
442 var names = keyStore.getValues(item,"year");
443 t.assertTrue(dojo.isArray(names));
444 t.is(1, names.length);
445 t.is(2007, names[0]);
446 d.callback(true);
447 }
448 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
449 return d;
450 },
451 function testIdentityAPI_fetchItemByIdentity(t){
452 // summary:
453 // Simple test of the fetchItemByIdentity function of the store.
454 // description:
455 // Simple test of the fetchItemByIdentity function of the store.
456
457 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
458 var keyStore = new dojox.data.KeyValueStore(args);
459
460 var d = new doh.Deferred();
461 function onItem(item){
462 t.assertTrue(item !== null);
463 d.callback(true);
464 }
465 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
466 return d;
467 },
468
469 function testIdentityAPI_fetchItemByIdentity_bad1(t){
470 // summary:
471 // Simple test of the fetchItemByIdentity function of the store.
472 // description:
473 // Simple test of the fetchItemByIdentity function of the store.
474
475 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
476 var keyStore = new dojox.data.KeyValueStore(args);
477
478 var d = new doh.Deferred();
479 function onItem(item){
480 t.assertTrue(item === null);
481 d.callback(true);
482 }
483 keyStore.fetchItemByIdentity({identity: "y3ar", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
484 return d;
485 },
486 function testIdentityAPI_fetchItemByIdentity_bad2(t){
487 // summary:
488 // Simple test of the fetchItemByIdentity function of the store.
489 // description:
490 // Simple test of the fetchItemByIdentity function of the store.
491
492 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
493 var keyStore = new dojox.data.KeyValueStore(args);
494 var d = new doh.Deferred();
495 function onItem(item){
496 t.assertTrue(item === null);
497 d.callback(true);
498 }
499 keyStore.fetchItemByIdentity({identity: "-1", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
500 return d;
501 },
502 function testIdentityAPI_fetchItemByIdentity_bad3(t){
503 // summary:
504 // Simple test of the fetchItemByIdentity function of the store.
505 // description:
506 // Simple test of the fetchItemByIdentity function of the store.
507
508 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
509 var keyStore = new dojox.data.KeyValueStore(args);
510 var d = new doh.Deferred();
511 function onItem(item){
512 t.assertTrue(item === null);
513 d.callback(true);
514 }
515 keyStore.fetchItemByIdentity({identity: "999999", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
516 return d;
517 },
518 function testIdentityAPI_getIdentity(t){
519 // summary:
520 // Simple test of the fetchItemByIdentity function of the store.
521 // description:
522 // Simple test of the fetchItemByIdentity function of the store.
523
524 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
525 var keyStore = new dojox.data.KeyValueStore(args);
526
527 var d = new doh.Deferred();
528 function completed(items, request){
529 t.is(7, items.length);
530 t.is(keyStore.getIdentity(items[0]), 'year');
531 t.is(keyStore.getIdentity(items[1]), 'nmonth');
532 t.is(keyStore.getIdentity(items[2]), 'month');
533 t.is(keyStore.getIdentity(items[3]), 'nday');
534 t.is(keyStore.getIdentity(items[4]), 'day');
535 t.is(keyStore.getIdentity(items[5]), 'dayOfYear');
536 t.is(keyStore.getIdentity(items[6]), 'weekOfYear');
537 d.callback(true);
538 }
539
540 //Get everything...
541 keyStore.fetch({ onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
542 return d; //Object
543 },
544 function testIdentityAPI_getIdentityAttributes(t){
545 // summary:
546 // Simple test of the getIdentityAttributes
547 // description:
548 // Simple test of the fetchItemByIdentity function of the store.
549
550 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
551 var keyStore = new dojox.data.KeyValueStore(args);
552
553 var d = new doh.Deferred();
554 function onItem(item){
555 t.assertTrue(keyStore.isItem(item));
556 t.assertEqual("key", keyStore.getIdentityAttributes(item));
557 d.callback(true);
558 }
559 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
560 return d;
561 },
562 function testReadAPI_isItem(t){
563 // summary:
564 // Simple test of the isItem function of the store
565 // description:
566 // Simple test of the isItem function of the store
567
568 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
569 var keyStore = new dojox.data.KeyValueStore(args);
570
571 var d = new doh.Deferred();
572 function onItem(item){
573 t.assertTrue(keyStore.isItem(item));
574 t.assertTrue(!keyStore.isItem({}));
575 t.assertTrue(!keyStore.isItem({ item: "not an item" }));
576 t.assertTrue(!keyStore.isItem("not an item"));
577 t.assertTrue(!keyStore.isItem(["not an item"]));
578 d.callback(true);
579 }
580 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
581 return d;
582 },
583 function testReadAPI_hasAttribute(t){
584 // summary:
585 // Simple test of the hasAttribute function of the store
586 // description:
587 // Simple test of the hasAttribute function of the store
588
589 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
590 var keyStore = new dojox.data.KeyValueStore(args);
591
592 var d = new doh.Deferred();
593 function onItem(item){
594 t.assertTrue(item !== null);
595 t.assertTrue(keyStore.hasAttribute(item, "key"));
596 t.assertTrue(keyStore.hasAttribute(item, "value"));
597 t.assertTrue(keyStore.hasAttribute(item, "year"));
598 t.assertTrue(!keyStore.hasAttribute(item, "Year"));
599 t.assertTrue(!keyStore.hasAttribute(item, "Nothing"));
600 t.assertTrue(!keyStore.hasAttribute(item, "Title"));
601
602 //Test that null attributes throw an exception
603 var passed = false;
604 try{
605 keyStore.hasAttribute(item, null);
606 }catch (e){
607 passed = true;
608 }
609 t.assertTrue(passed);
610 d.callback(true);
611 }
612 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
613 return d;
614 },
615 function testReadAPI_containsValue(t){
616 // summary:
617 // Simple test of the containsValue function of the store
618 // description:
619 // Simple test of the containsValue function of the store
620
621 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
622 var keyStore = new dojox.data.KeyValueStore(args);
623
624 var d = new doh.Deferred();
625 function onItem(item){
626 t.assertTrue(item !== null);
627 t.assertTrue(keyStore.containsValue(item, "year", "2007"));
628 t.assertTrue(keyStore.containsValue(item, "value", "2007"));
629 t.assertTrue(keyStore.containsValue(item, "key", "year"));
630 t.assertTrue(!keyStore.containsValue(item, "Title", "Alien2"));
631 t.assertTrue(!keyStore.containsValue(item, "Year", "1979 "));
632 t.assertTrue(!keyStore.containsValue(item, "Title", null));
633
634 //Test that null attributes throw an exception
635 var passed = false;
636 try{
637 keyStore.containsValue(item, null, "foo");
638 }catch (e){
639 passed = true;
640 }
641 t.assertTrue(passed);
642 d.callback(true);
643 }
644 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
645 return d;
646 },
647 function testReadAPI_getAttributes(t){
648 // summary:
649 // Simple test of the getAttributes function of the store
650 // description:
651 // Simple test of the getAttributes function of the store
652
653 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
654 var keyStore = new dojox.data.KeyValueStore(args);
655
656 var d = new doh.Deferred();
657 function onItem(item){
658 t.assertTrue(item !== null);
659 t.assertTrue(keyStore.isItem(item));
660
661 var attributes = keyStore.getAttributes(item);
662 t.is(3, attributes.length);
663 for(var i = 0; i < attributes.length; i++){
664 t.assertTrue((attributes[i] === "year" || attributes[i] === "value" || attributes[i] === "key"));
665 }
666 d.callback(true);
667 }
668 keyStore.fetchItemByIdentity({identity: "year", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
669 return d;
670 },
671
672 function testReadAPI_getAttributes_onlyTwo(t){
673 // summary:
674 // Simple test of the getAttributes function of the store
675 // description:
676 // Simple test of the getAttributes function of the store
677
678 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
679 var keyStore = new dojox.data.KeyValueStore(args);
680
681 var d = new doh.Deferred();
682 function onItem(item){
683 // Test an item that does not have all of the attributes
684 t.assertTrue(item !== null);
685 t.assertTrue(keyStore.isItem(item));
686
687 var attributes = keyStore.getAttributes(item);
688 t.assertTrue(attributes.length === 3);
689 t.assertTrue(attributes[0] === "key");
690 t.assertTrue(attributes[1] === "value");
691 t.assertTrue(attributes[2] === "nmonth");
692 d.callback(true);
693 }
694 keyStore.fetchItemByIdentity({identity: "nmonth", onItem: onItem, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
695 return d;
696 },
697
698 function testReadAPI_getFeatures(t){
699 // summary:
700 // Simple test of the getFeatures function of the store
701 // description:
702 // Simple test of the getFeatures function of the store
703
704 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
705 var keyStore = new dojox.data.KeyValueStore(args);
706
707 var features = keyStore.getFeatures();
708 var count = 0;
709 for(i in features){
710 t.assertTrue((i === "dojo.data.api.Read" || i === "dojo.data.api.Identity"));
711 count++;
712 }
713 t.assertTrue(count === 2);
714 },
715 function testReadAPI_fetch_patternMatch0(t){
716 // summary:
717 // Function to test pattern matching of everything starting with lowercase e
718 // description:
719 // Function to test pattern matching of everything starting with lowercase e
720
721 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
722 var keyStore = new dojox.data.KeyValueStore(args);
723
724 var d = new doh.Deferred();
725 function completed(items, request){
726 t.is(2, items.length);
727 var valueArray = [ "nmonth", "month"];
728 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "key", valueArray));
729 d.callback(true);
730 }
731
732 keyStore.fetch({query: {key: "*month"}, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
733 return d; //Object
734 },
735 function testReadAPI_fetch_patternMatch1(t){
736 // summary:
737 // Function to test pattern matching of everything with $ in it.
738 // description:
739 // Function to test pattern matching of everything with $ in it.
740
741 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
742 var keyStore = new dojox.data.KeyValueStore(args);
743
744 var d = new doh.Deferred();
745 function completed(items, request){
746 t.assertTrue(items.length === 2);
747 var valueArray = [ "1", "Saturday"];
748 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "value", valueArray));
749 d.callback(true);
750 }
751
752 keyStore.fetch({query: {key: "*day"}, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
753 return d; //Object
754 },
755 function testReadAPI_fetch_patternMatch2(t){
756 // summary:
757 // Function to test exact pattern match
758 // description:
759 // Function to test exact pattern match
760
761 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
762 var keyStore = new dojox.data.KeyValueStore(args);
763
764 var d = new doh.Deferred();
765 function completed(items, request){
766 t.is(2, items.length);
767 t.assertTrue(keyStore.getValue(items[0], "value") === "12");
768 t.assertTrue(keyStore.getValue(items[0], "key") === "nmonth");
769 t.assertTrue(keyStore.getValue(items[1], "value") === "1");
770 t.assertTrue(keyStore.getValue(items[1], "key") === "nday");
771 d.callback(true);
772 }
773
774 keyStore.fetch({query: {value: "1*"}, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
775 return d; //Object
776 },
777 function testReadAPI_fetch_patternMatch_caseInsensitive(t){
778 // summary:
779 // Function to test exact pattern match with case insensitivity set.
780 // description:
781 // Function to test exact pattern match with case insensitivity set.
782
783 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
784 var keyStore = new dojox.data.KeyValueStore(args);
785
786 var d = new doh.Deferred();
787 function completed(items, request){
788 t.is(1, items.length);
789 t.assertTrue(keyStore.getValue(items[0], "value") === "December");
790 d.callback(true);
791 }
792
793 keyStore.fetch({query: {key: "MONth"}, queryOptions: {ignoreCase: true}, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
794 return d; //Object
795 },
796 function testReadAPI_fetch_patternMatch_caseSensitive(t){
797 // summary:
798 // Function to test exact pattern match with case insensitivity set.
799 // description:
800 // Function to test exact pattern match with case insensitivity set.
801
802 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
803 var keyStore = new dojox.data.KeyValueStore(args);
804
805 var d = new doh.Deferred();
806 function completed(items, request){
807 t.is(0, items.length);
808 d.callback(true);
809 }
810
811 keyStore.fetch({query: {value: "DECEMberO"}, queryOptions: {ignoreCase: false}, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
812 return d; //Object
813 },
814 function testReadAPI_fetch_sortAlphabetic(t){
815 // summary:
816 // Function to test sorting alphabetic ordering.
817 // description:
818 // Function to test sorting alphabetic ordering.
819
820 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
821 var keyStore = new dojox.data.KeyValueStore(args);
822
823 var d = new doh.Deferred();
824 function completed(items, request){
825 //Output should be in this order...
826 var orderedArray = [ "day", "dayOfYear", "month", "nday", "nmonth", "weekOfYear", "year" ];
827 t.is(7, items.length);
828 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "key", orderedArray));
829 d.callback(true);
830 }
831
832 var sortAttributes = [{attribute: "key"}];
833 keyStore.fetch({sort: sortAttributes, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
834 return d; //Object
835 },
836 function testReadAPI_fetch_sortAlphabeticDescending(t){
837 // summary:
838 // Function to test sorting alphabetic ordering in descending mode.
839 // description:
840 // Function to test sorting alphabetic ordering in descending mode.
841
842 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
843 var keyStore = new dojox.data.KeyValueStore(args);
844
845 var d = new doh.Deferred();
846 function completed(items, request){
847 //Output should be in this order...
848 var orderedArray = [ "year", "weekOfYear", "nmonth", "nday", "month", "dayOfYear", "day" ];
849 t.is(7, items.length);
850 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "key", orderedArray));
851 d.callback(true);
852 }
853
854 var sortAttributes = [{attribute: "key", descending: true}];
855 keyStore.fetch({sort: sortAttributes, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
856 return d; //Object
857 },
858 function testReadAPI_fetch_sortMultiple(t){
859 // summary:
860 // Function to test sorting on multiple attributes.
861 // description:
862 // Function to test sorting on multiple attributes.
863
864 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/patterns.csv");
865 var keyStore = new dojox.data.KeyValueStore(args);
866
867 var d = new doh.Deferred();
868 function completed(items, request){
869 var orderedArray1 = [ "123abc",
870 "123abc",
871 "123abc",
872 "123abcdefg",
873 "BaBaMaSaRa***Foo",
874 "bar*foo",
875 "bit$Bite",
876 "foo*bar",
877 "jfq4@#!$!@Rf14r14i5u",
878 undefined
879 ];
880 var orderedArray0 = [ "day", "dayOfYear", "month", "nday", "nmonth", "weekOfYear", "year" ];
881 var orderedArray1 = [ "Saturday", "335", "December", "1", "12", "48", "2007" ];
882 t.is(7, items.length);
883 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "key", orderedArray0));
884 t.assertTrue(dojox.data.tests.stores.KeyValueStore.verifyItems(keyStore, items, "value", orderedArray1));
885 d.callback(true);
886 }
887
888 var sortAttributes = [{ attribute: "key"}, { attribute: "value", descending: true}];
889 keyStore.fetch({sort: sortAttributes, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
890 return d; //Object
891 },
892 function testReadAPI_fetch_sortMultipleSpecialComparator(t){
893 // summary:
894 // Function to test sorting on multiple attributes with a custom comparator.
895 // description:
896 // Function to test sorting on multiple attributes with a custom comparator.
897
898 var args = dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv");
899 var keyStore = new dojox.data.KeyValueStore(args);
900
901 keyStore.comparatorMap = {};
902 keyStore.comparatorMap["key"] = function(a,b){
903 var ret = 0;
904 // We want to sort keys alphabetical by the last character in the string
905 function lastChar(name){
906 if(typeof name === "undefined"){ return undefined; }
907
908 return name.slice(name.length-1); // Grab the last character in the string.
909 }
910 var lastCharA = lastChar(a);
911 var lastCharB = lastChar(b);
912 if(lastCharA > lastCharB || typeof lastCharA === "undefined"){
913 ret = 1;
914 }else if(lastCharA < lastCharB || typeof lastCharB === "undefined"){
915 ret = -1;
916 }
917 return ret;
918 };
919
920 var sortAttributes = [{attribute: "key", descending: true}, { attribute: "value", descending: true}];
921
922 var d = new doh.Deferred();
923 function completed(items, findResult){
924 var orderedArray = [5,4,0,3,2,1,6];
925 var orderedArray = [ "day", "nday", "weekOfYear", "dayOfYear", "year", "month", "nmonth" ];
926 t.assertTrue(items.length === 7);
927 var passed = true;
928 for(var i = 0; i < items.length; i++){
929 if(!(keyStore.getIdentity(items[i]) === orderedArray[i])){
930 passed=false;
931 break;
932 }
933 }
934 t.assertTrue(passed);
935 d.callback(true);
936 }
937
938 keyStore.fetch({sort: sortAttributes, onComplete: completed, onError: dojo.partial(dojox.data.tests.stores.KeyValueStore.error, t, d)});
939 return d; //Object
940 },
941 function testReadAPI_functionConformance(t){
942 // summary:
943 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
944 // description:
945 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
946
947 var testStore = new dojox.data.KeyValueStore(dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv"));
948 var readApi = new dojo.data.api.Read();
949 var passed = true;
950
951 for(i in readApi){
952 if(i.toString().charAt(0) !== '_')
953 {
954 var member = readApi[i];
955 //Check that all the 'Read' defined functions exist on the test store.
956 if(typeof member === "function"){
957 console.log("Looking at function: [" + i + "]");
958 var testStoreMember = testStore[i];
959 if(!(typeof testStoreMember === "function")){
960 console.log("Problem with function: [" + i + "]. Got value: " + testStoreMember);
961 passed = false;
962 break;
963 }
964 }
965 }
966 }
967 t.assertTrue(passed);
968 },
969 function testIdentityAPI_functionConformance(t){
970 // summary:
971 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
972 // description:
973 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
974
975 var testStore = new dojox.data.KeyValueStore(dojox.data.tests.stores.KeyValueStore.getDatasource("stores/movies.csv"));
976 var identityApi = new dojo.data.api.Identity();
977 var passed = true;
978
979 for(i in identityApi){
980 if(i.toString().charAt(0) !== '_')
981 {
982 var member = identityApi[i];
983 //Check that all the 'Read' defined functions exist on the test store.
984 if(typeof member === "function"){
985 console.log("Looking at function: [" + i + "]");
986 var testStoreMember = testStore[i];
987 if(!(typeof testStoreMember === "function")){
988 passed = false;
989 break;
990 }
991 }
992 }
993 }
994 t.assertTrue(passed);
995 }
996 ]
997 );
998