1 if(!dojo._hasResource["dojox.data.tests.stores.HtmlStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.tests.stores.HtmlStore"] = true;
3 dojo.provide("dojox.data.tests.stores.HtmlStore");
4 dojo.require("dojox.data.HtmlStore");
5 dojo.require("dojo.data.api.Read");
6 dojo.require("dojo.data.api.Identity");
9 dojox.data.tests.stores.HtmlStore.getBooks3Store = function(){
10 return new dojox.data.HtmlStore({url: dojo.moduleUrl("dojox.data.tests", "stores/books3.html").toString(), dataId: "books3"});
13 dojox.data.tests.stores.HtmlStore.getBooks2Store = function(){
14 return new dojox.data.HtmlStore({url: dojo.moduleUrl("dojox.data.tests", "stores/books2.html").toString(), dataId: "books2"});
17 dojox.data.tests.stores.HtmlStore.getBooksStore = function(){
18 return new dojox.data.HtmlStore({url: dojo.moduleUrl("dojox.data.tests", "stores/books.html").toString(), dataId: "books"});
21 doh.register("dojox.data.tests.stores.HtmlStore",
23 /***************************************
24 dojo.data.api.Read API
25 ***************************************/
26 function testReadAPI_fetch_all_table(t){
28 // Simple test of fetching all xml items through an XML element called isbn
30 // Simple test of fetching all xml items through an XML element called isbn
31 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
33 var d = new doh.Deferred();
34 function onComplete(items, request) {
35 t.assertEqual(20, items.length);
38 function onError(error, request) {
41 store.fetch({query:{isbn:"*"}, onComplete: onComplete, onError: onError});
44 function testReadAPI_fetch_all_list(t){
46 // Simple test of fetching all xml items through an XML element called isbn
48 // Simple test of fetching all xml items through an XML element called isbn
49 var store = dojox.data.tests.stores.HtmlStore.getBooks3Store();
51 var d = new doh.Deferred();
52 function onComplete(items, request) {
53 t.assertEqual(5, items.length);
56 function onError(error, request) {
59 store.fetch({query:{name:"*"}, onComplete: onComplete, onError: onError});
62 function testReadAPI_fetch_one_table(t){
64 // Simple test of fetching one xml items through an XML element called isbn
66 // Simple test of fetching one xml items through an XML element called isbn
67 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
69 var d = new doh.Deferred();
70 function onComplete(items, request) {
71 t.assertEqual(1, items.length);
74 function onError(error, request) {
77 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
80 function testReadAPI_fetch_one_list(t){
82 // Simple test of fetching one xml items through an XML element called isbn
84 // Simple test of fetching one xml items through an XML element called isbn
85 var store = dojox.data.tests.stores.HtmlStore.getBooks3Store();
87 var d = new doh.Deferred();
88 function onComplete(items, request) {
89 t.assertEqual(1, items.length);
92 function onError(error, request) {
95 store.fetch({query:{name:"A9B57C - Title of 1 - Author of 1"}, onComplete: onComplete, onError: onError});
98 function testReadAPI_fetch_paging(t){
100 // Simple test of fetching one xml items through an XML element called isbn
102 // Simple test of fetching one xml items through an XML element called isbn
103 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
105 var d = new doh.Deferred();
106 function dumpFirstFetch(items, request){
107 t.assertEqual(5, items.length);
110 request.onComplete = dumpSecondFetch;
111 store.fetch(request);
114 function dumpSecondFetch(items, request){
115 t.assertEqual(1, items.length);
118 request.onComplete = dumpThirdFetch;
119 store.fetch(request);
122 function dumpThirdFetch(items, request){
123 t.assertEqual(5, items.length);
126 request.onComplete = dumpFourthFetch;
127 store.fetch(request);
130 function dumpFourthFetch(items, request){
131 t.assertEqual(18, items.length);
134 request.onComplete = dumpFifthFetch;
135 store.fetch(request);
138 function dumpFifthFetch(items, request){
139 t.assertEqual(11, items.length);
142 request.onComplete = dumpSixthFetch;
143 store.fetch(request);
146 function dumpSixthFetch(items, request){
147 t.assertEqual(18, items.length);
151 function completed(items, request){
152 t.assertEqual(20, items.length);
155 request.onComplete = dumpFirstFetch;
156 store.fetch(request);
159 function error(errData, request){
163 store.fetch({onComplete: completed, onError: error});
166 function testReadAPI_fetch_pattern0(t){
168 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match
170 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match
171 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
172 var d = new doh.Deferred();
173 function onComplete(items, request) {
174 t.assertEqual(1, items.length);
177 function onError(error, request) {
180 store.fetch({query:{isbn:"?9B574"}, onComplete: onComplete, onError: onError});
183 function testReadAPI_fetch_pattern1(t){
185 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match
187 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match
188 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
189 var d = new doh.Deferred();
190 function onComplete(items, request) {
191 t.assertEqual(4, items.length);
194 function onError(error, request) {
197 store.fetch({query:{isbn:"A9B57?"}, onComplete: onComplete, onError: onError});
200 function testReadAPI_fetch_pattern2(t){
202 // Simple test of fetching one xml items through an XML element called isbn with * pattern match
204 // Simple test of fetching one xml items through an XML element called isbn with * pattern match
205 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
206 var d = new doh.Deferred();
207 function onComplete(items, request) {
208 t.assertEqual(5, items.length);
211 function onError(error, request) {
214 store.fetch({query:{isbn:"A9*"}, onComplete: onComplete, onError: onError});
217 function testReadAPI_fetch_pattern_caseInsensitive(t){
219 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match and in case insensitive mode.
221 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match and in case insensitive mode.
222 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
223 var d = new doh.Deferred();
224 function onComplete(items, request) {
225 t.assertEqual(1, items.length);
228 function onError(error, request) {
231 store.fetch({query:{isbn:"?9b574"}, queryOptions: {ignoreCase: true}, onComplete: onComplete, onError: onError});
234 function testReadAPI_fetch_pattern_caseSensitive(t){
236 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match and in case sensitive mode.
238 // Simple test of fetching one xml items through an XML element called isbn with ? pattern match and in case sensitive mode.
239 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
240 var d = new doh.Deferred();
241 function onComplete(items, request) {
242 t.assertEqual(1, items.length);
245 function onError(error, request) {
248 store.fetch({query:{isbn:"?9B574"}, queryOptions: {ignoreCase: false}, onComplete: onComplete, onError: onError});
251 function testReadAPI_getLabel_table(t){
253 // Simple test of the getLabel function against a store set that has a label defined.
255 // Simple test of the getLabel function against a store set that has a label defined.
257 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
259 var d = new doh.Deferred();
260 function onComplete(items, request){
261 t.assertEqual(items.length, 1);
262 var label = store.getLabel(items[0]);
263 t.assertTrue(label !== null);
264 t.assertEqual("Item #4", label);
267 function onError(error, request) {
270 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
273 function testReadAPI_getLabel_list(t){
275 // Simple test of the getLabel function against a store set that has a label defined.
277 // Simple test of the getLabel function against a store set that has a label defined.
279 var store = dojox.data.tests.stores.HtmlStore.getBooks3Store();
281 var d = new doh.Deferred();
282 function onComplete(items, request){
283 t.assertEqual(items.length, 1);
284 var label = store.getLabel(items[0]);
285 t.assertTrue(label !== null);
286 t.assertEqual("A9B57C - Title of 1 - Author of 1", label);
289 function onError(error, request) {
292 store.fetch({query:{name:"A9B57C - Title of 1 - Author of 1"}, onComplete: onComplete, onError: onError});
295 function testReadAPI_getLabelAttributes(t){
297 // Simple test of the getLabelAttributes function against a store set that has a label defined.
299 // Simple test of the getLabelAttributes function against a store set that has a label defined.
301 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
303 var d = new doh.Deferred();
304 function onComplete(items, request){
305 t.assertEqual(items.length, 1);
306 var labelList = store.getLabelAttributes(items[0]);
307 t.assertTrue(labelList === null);
310 function onError(error, request) {
313 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
317 function testReadAPI_getValue(t){
319 // Simple test of the getValue API
321 // Simple test of the getValue API
322 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
324 var d = new doh.Deferred();
325 function onComplete(items, request) {
326 t.assertEqual(1, items.length);
328 t.assertTrue(store.hasAttribute(item,"isbn"));
329 t.assertEqual(store.getValue(item,"isbn"), "A9B574");
332 function onError(error, request) {
335 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
338 function testReadAPI_getValues(t){
340 // Simple test of the getValues API
342 // Simple test of the getValues API
343 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
345 var d = new doh.Deferred();
346 function onComplete(items, request) {
347 t.assertEqual(1, items.length);
349 t.assertTrue(store.hasAttribute(item,"isbn"));
350 var values = store.getValues(item,"isbn");
351 t.assertEqual(1,values.length);
352 t.assertEqual("A9B574", values[0]);
355 function onError(error, request) {
358 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
361 function testReadAPI_isItem(t){
363 // Simple test of the isItem API
365 // Simple test of the isItem API
366 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
368 var d = new doh.Deferred();
369 function onComplete(items, request) {
370 t.assertEqual(1, items.length);
372 t.assertTrue(store.isItem(item));
373 t.assertTrue(!store.isItem({}));
374 t.assertTrue(!store.isItem("Foo"));
375 t.assertTrue(!store.isItem(1));
378 function onError(error, request) {
381 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
384 function testReadAPI_isItem_multistore(t){
386 // Simple test of the isItem API across multiple store instances.
388 // Simple test of the isItem API across multiple store instances.
389 var store1 = dojox.data.tests.stores.HtmlStore.getBooksStore();
390 var store2 = dojox.data.tests.stores.HtmlStore.getBooks2Store();
392 var d = new doh.Deferred();
393 function onComplete1(items, request) {
394 t.assertEqual(1, items.length);
395 var item1 = items[0];
396 t.assertTrue(store1.isItem(item1));
398 function onComplete2(items, request) {
399 t.assertEqual(1, items.length);
400 var item2 = items[0];
401 t.assertTrue(store2.isItem(item2));
402 t.assertTrue(!store1.isItem(item2));
403 t.assertTrue(!store2.isItem(item1));
406 store2.fetch({query:{isbn:"A9B574"}, onComplete: onComplete2, onError: onError});
408 function onError(error, request) {
411 store1.fetch({query:{isbn:"1"}, onComplete: onComplete1, onError: onError});
414 function testReadAPI_hasAttribute(t){
416 // Simple test of the hasAttribute API
418 // Simple test of the hasAttribute API
419 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
421 var d = new doh.Deferred();
422 function onComplete(items, request) {
423 t.assertEqual(1, items.length);
425 t.assertTrue(store.hasAttribute(item,"isbn"));
426 t.assertTrue(!store.hasAttribute(item,"bob"));
429 function onError(error, request) {
432 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
435 function testReadAPI_containsValue(t){
437 // Simple test of the containsValue API
439 // Simple test of the containsValue API
440 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
442 var d = new doh.Deferred();
443 function onComplete(items, request) {
444 t.assertEqual(1, items.length);
446 t.assertTrue(store.containsValue(item,"isbn", "A9B574"));
447 t.assertTrue(!store.containsValue(item,"isbn", "bob"));
450 function onError(error, request) {
453 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
456 function testReadAPI_sortDescending(t){
458 // Simple test of the sorting API in descending order.
460 // Simple test of the sorting API in descending order.
461 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
463 //Comparison is done as a string type (toString comparison), so the order won't be numeric
464 //So have to compare in 'alphabetic' order.
465 var order = [9,8,7,6,5,4,3,20,2,19,18,17,16,15,14,13,12,11,10,1];
467 var d = new doh.Deferred();
468 function onComplete(items, request) {
469 t.assertEqual(20, items.length);
471 for(var i = 0; i < items.length; i++){
472 t.assertEqual(order[i], store.getValue(items[i],"isbn").toString());
476 function onError(error, request) {
480 var sortAttributes = [{attribute: "isbn", descending: true}];
481 store.fetch({query:{isbn:"*"}, sort: sortAttributes, onComplete: onComplete, onError: onError});
484 function testReadAPI_sortAscending(t){
486 // Simple test of the sorting API in ascending order.
488 // Simple test of the sorting API in ascending order.
489 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
491 //Comparison is done as a string type (toString comparison), so the order won't be numeric
492 //So have to compare in 'alphabetic' order.
493 var order = [1,10,11,12,13,14,15,16,17,18,19,2,20,3,4,5,6,7,8,9];
495 var d = new doh.Deferred();
496 function onComplete(items, request) {
497 t.assertEqual(20, items.length);
499 for(var i = 0; i < items.length; i++){
500 t.assertEqual(order[i], store.getValue(items[i],"isbn").toString());
504 function onError(error, request) {
508 var sortAttributes = [{attribute: "isbn"}];
509 store.fetch({query:{isbn:"*"}, sort: sortAttributes, onComplete: onComplete, onError: onError});
512 function testReadAPI_sortDescendingNumeric(t){
514 // Simple test of the sorting API in descending order using a numeric comparator.
516 // Simple test of the sorting API in descending order using a numeric comparator.
517 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
519 //isbn should be treated as a numeric, not as a string comparison
520 store.comparatorMap = {};
521 store.comparatorMap["isbn"] = function(a, b){
523 if(parseInt(a.toString()) > parseInt(b.toString())){
525 }else if(parseInt(a.toString()) < parseInt(b.toString())){
528 return ret; //int, {-1,0,1}
531 var d = new doh.Deferred();
532 function onComplete(items, request) {
533 t.assertEqual(20, items.length);
535 for(var i = 0; i < items.length; i++){
536 t.assertEqual(itemId, store.getValue(items[i],"isbn").toString());
541 function onError(error, request) {
545 var sortAttributes = [{attribute: "isbn", descending: true}];
546 store.fetch({query:{isbn:"*"}, sort: sortAttributes, onComplete: onComplete, onError: onError});
549 function testReadAPI_sortAscendingNumeric(t){
551 // Simple test of the sorting API in ascending order using a numeric comparator.
553 // Simple test of the sorting API in ascending order using a numeric comparator.
554 var store = dojox.data.tests.stores.HtmlStore.getBooksStore();
556 //isbn should be treated as a numeric, not as a string comparison
557 store.comparatorMap = {};
558 store.comparatorMap["isbn"] = function(a, b){
560 if(parseInt(a.toString()) > parseInt(b.toString())){
562 }else if(parseInt(a.toString()) < parseInt(b.toString())){
565 return ret; //int, {-1,0,1}
568 var d = new doh.Deferred();
569 function onComplete(items, request) {
570 t.assertEqual(20, items.length);
572 for(var i = 0; i < items.length; i++){
573 t.assertEqual(itemId, store.getValue(items[i],"isbn").toString());
578 function onError(error, request) {
582 var sortAttributes = [{attribute: "isbn"}];
583 store.fetch({query:{isbn:"*"}, sort: sortAttributes, onComplete: onComplete, onError: onError});
586 function testReadAPI_isItemLoaded(t){
588 // Simple test of the isItemLoaded API
590 // Simple test of the isItemLoaded API
591 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
593 var d = new doh.Deferred();
594 function onComplete(items, request) {
595 t.assertEqual(1, items.length);
597 t.assertTrue(store.isItemLoaded(item));
600 function onError(error, request) {
603 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
606 function testReadAPI_getFeatures(t){
608 // Simple test of the getFeatures function of the store
610 // Simple test of the getFeatures function of the store
612 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
613 var features = store.getFeatures();
616 t.assertTrue((i === "dojo.data.api.Read" || i === "dojo.data.api.Identity"));
619 t.assertEqual(2, count);
621 function testReadAPI_getAttributes(t){
623 // Simple test of the getAttributes API
625 // Simple test of the getAttributes API
626 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
628 var d = new doh.Deferred();
629 function onComplete(items, request) {
630 t.assertEqual(1, items.length);
632 var attributes = store.getAttributes(item);
633 t.assertEqual(3,attributes.length);
634 for(var i=0; i<attributes.length; i++){
635 t.assertTrue((attributes[i] === "isbn" || attributes[i] === "title" || attributes[i] === "author"));
639 function onError(error, request) {
642 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
645 function testReadAPI_functionConformance(t){
647 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
649 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
651 var testStore = dojox.data.tests.stores.HtmlStore.getBooksStore();
652 var readApi = new dojo.data.api.Read();
656 var member = readApi[i];
657 //Check that all the 'Read' defined functions exist on the test store.
658 if(typeof member === "function"){
659 var testStoreMember = testStore[i];
660 if(!(typeof testStoreMember === "function")){
661 console.log("Problem with function: [" + i + "]");
667 t.assertTrue(passed);
669 /***************************************
670 dojo.data.api.Identity API
671 ***************************************/
672 function testIdentityAPI_getIdentity_table(t){
674 // Simple test of the getAttributes API
676 // Simple test of the getAttributes API
677 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
679 var d = new doh.Deferred();
680 function onComplete(items, request) {
681 t.assertEqual(1, items.length);
683 t.assertEqual(4,store.getIdentity(item));
686 function onError(error, request) {
689 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
692 function testIdentityAPI_getIdentity_list(t){
694 // Simple test of the getAttributes API
696 // Simple test of the getAttributes API
697 var store = dojox.data.tests.stores.HtmlStore.getBooks3Store();
699 var d = new doh.Deferred();
700 function onComplete(items, request) {
701 t.assertEqual(1, items.length);
703 t.assertEqual("A9B57C - Title of 1 - Author of 1",store.getIdentity(item));
706 function onError(error, request) {
709 store.fetch({query:{name:"A9B57C - Title of 1 - Author of 1"}, onComplete: onComplete, onError: onError});
712 function testIdentityAPI_getIdentityAttributes(t){
714 // Simple test of the getAttributes API
716 // Simple test of the getAttributes API
717 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
719 var d = new doh.Deferred();
720 function onComplete(items, request) {
721 t.assertEqual(1, items.length);
723 //Should have none, as it's not a public attribute.
724 var attributes = store.getIdentityAttributes(item);
725 t.assertEqual(null, attributes);
728 function onError(error, request) {
731 store.fetch({query:{isbn:"A9B574"}, onComplete: onComplete, onError: onError});
734 function testIdentityAPI_fetchItemByIdentity_table(t){
736 // Simple test of the fetchItemByIdentity API
738 // Simple test of the fetchItemByIdentity API
739 var store = dojox.data.tests.stores.HtmlStore.getBooks2Store();
741 var d = new doh.Deferred();
742 function onItem(item, request) {
743 t.assertTrue(item !== null);
744 t.assertTrue(store.isItem(item));
745 t.assertEqual("A9B574", store.getValue(item, "isbn"));
748 function onError(error, request) {
751 store.fetchItemByIdentity({identity: 4, onItem: onItem, onError: onError});
754 function testIdentityAPI_fetchItemByIdentity_list(t){
756 // Simple test of the fetchItemByIdentity API
758 // Simple test of the fetchItemByIdentity API
759 var store = dojox.data.tests.stores.HtmlStore.getBooks3Store();
761 var d = new doh.Deferred();
762 function onItem(item, request) {
763 t.assertTrue(item !== null);
764 t.assertTrue(store.isItem(item));
765 t.assertEqual("A9B57C - Title of 1 - Author of 1", store.getValue(item, "name"));
768 function onError(error, request) {
771 store.fetchItemByIdentity({identity: "A9B57C - Title of 1 - Author of 1", onItem: onItem, onError: onError});
774 function testIdentityAPI_functionConformance(t){
776 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
778 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
780 var testStore = dojox.data.tests.stores.HtmlStore.getBooksStore();
781 var identityApi = new dojo.data.api.Identity();
784 for(i in identityApi){
785 var member = identityApi[i];
786 //Check that all the 'Read' defined functions exist on the test store.
787 if(typeof member === "function"){
788 var testStoreMember = testStore[i];
789 if(!(typeof testStoreMember === "function")){
790 console.log("Problem with function: [" + i + "]");
796 t.assertTrue(passed);
801 //Register the remote tests ... when they work.
802 //doh.registerUrl("dojox.data.tests.stores.HtmlStore.remote", dojo.moduleUrl("dojox.data.tests", "ml/test_HtmlStore_declaratively.html"));