1 if(!dojo._hasResource["tests.data.readOnlyItemFileTestTemplates"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["tests.data.readOnlyItemFileTestTemplates"] = true;
3 dojo.provide("tests.data.readOnlyItemFileTestTemplates");
4 dojo.require("dojo.data.api.Read");
5 dojo.require("dojo.data.api.Identity");
6 dojo.require("dojo.date");
7 dojo.require("dojo.date.stamp");
10 tests.data.readOnlyItemFileTestTemplates.registerTestsForDatastore = function(/* String */ datastoreClassName){
12 // Given the name of a datastore class to use, this function creates
13 // a set of unit tests for that datastore, and registers the new test
14 // group with the doh test framework. The new unit tests are based
15 // on a set of "template" unit tests.
16 var datastoreClass = dojo.getObject(datastoreClassName);
17 var testTemplates = tests.data.readOnlyItemFileTestTemplates.testTemplates;
18 var testsForDatastore = [];
19 var makeNewTestFunction = function(template){
20 return function(t){return template.runTest(datastoreClass, t);};
22 for(var i = 0; i < testTemplates.length; ++i) {
23 var testTemplate = testTemplates[i];
25 test.name = testTemplate.name;
26 test.runTest = makeNewTestFunction(testTemplate);
27 testsForDatastore.push(test);
29 var testGroupName = "tests.data.readOnlyItemFileTestTemplates, with datastore " + datastoreClassName;
30 doh.register(testGroupName, testsForDatastore);
34 //-----------------------------------------------------
36 tests.data.readOnlyItemFileTestTemplates.getTestData = function(name){
38 if(name === "countries"){
40 data = {url: dojo.moduleUrl("tests", "data/countries.json").toString() };
46 {abbr:"ec", name:"Ecuador", capital:"Quito"},
47 {abbr:'eg', name:'Egypt', capital:'Cairo'},
48 {abbr:'sv', name:'El Salvador', capital:'San Salvador'},
49 {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'},
50 {abbr:'er', name:'Eritrea', capital:'Asmara'},
51 {abbr:'ee', name:'Estonia', capital:'Tallinn'},
52 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'}
56 }else if(name === "countries_withNull"){
58 data = {url: dojo.moduleUrl("tests", "data/countries_withNull.json").toString() };
63 {abbr:"ec", name:null, capital:"Quito"},
64 {abbr:'eg', name:null, capital:'Cairo'},
65 {abbr:'sv', name:'El Salvador', capital:'San Salvador'},
66 {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'},
67 {abbr:'er', name:'Eritrea', capital:'Asmara'},
68 {abbr:'ee', name:null, capital:'Tallinn'},
69 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'}
73 }else if(name === "countries_withoutid"){
75 data = {url: dojo.moduleUrl("tests", "data/countries_withoutid.json").toString() };
80 {abbr:"ec", name:null, capital:"Quito"},
81 {abbr:'eg', name:null, capital:'Cairo'},
82 {abbr:'sv', name:'El Salvador', capital:'San Salvador'},
83 {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'},
84 {abbr:'er', name:'Eritrea', capital:'Asmara'},
85 {abbr:'ee', name:null, capital:'Tallinn'},
86 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'}
90 }else if (name === "countries_withBoolean"){
92 data = {url: dojo.moduleUrl("tests", "data/countries_withBoolean.json").toString() };
97 {abbr:"ec", name:"Ecuador", capital:"Quito", real:true},
98 {abbr:'eg', name:'Egypt', capital:'Cairo', real:true},
99 {abbr:'sv', name:'El Salvador', capital:'San Salvador', real:true},
100 {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo', real:true},
101 {abbr:'er', name:'Eritrea', capital:'Asmara', real:true},
102 {abbr:'ee', name:'Estonia', capital:'Tallinn', real:true},
103 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa', real:true},
104 {abbr:'ut', name:'Utopia', capital:'Paradise', real:false}
108 }else if (name === "countries_withDates"){
110 data = {url: dojo.moduleUrl("tests", "data/countries_withDates.json").toString() };
115 {abbr:"ec", name:"Ecuador", capital:"Quito"},
116 {abbr:'eg', name:'Egypt', capital:'Cairo'},
117 {abbr:'sv', name:'El Salvador', capital:'San Salvador'},
118 {abbr:'gq', name:'Equatorial Guinea', capital:'Malabo'},
119 {abbr:'er', name:'Eritrea', capital:'Asmara', independence:{_type:'Date', _value:"1993-05-24T00:00:00Z"}}, // May 24, 1993,
120 {abbr:'ee', name:'Estonia', capital:'Tallinn', independence:{_type:'Date', _value:"1991-08-20T00:00:00Z"}}, // August 20, 1991
121 {abbr:'et', name:'Ethiopia', capital:'Addis Ababa'}
125 }else if (name === "geography_hierarchy_small"){
127 data = {url: dojo.moduleUrl("tests", "data/geography_hierarchy_small.json").toString() };
131 { name:'Africa', countries:[
132 { name:'Egypt', capital:'Cairo' },
133 { name:'Kenya', capital:'Nairobi' },
134 { name:'Sudan', capital:'Khartoum' }]},
135 { name:'Australia', capital:'Canberra' },
136 { name:'North America', countries:[
137 { name:'Canada', population:'33 million', cities:[
138 { name:'Toronto', population:'2.5 million' },
139 { name:'Alberta', population:'1 million' }
141 { name: 'United States of America', capital: 'Washington DC', states:[
149 }else if (name === "data_multitype"){
151 data = {url: dojo.moduleUrl("tests", "data/data_multitype.json").toString() };
154 "identifier": "count",
157 { count: 1, value: "true" },
158 { count: 2, value: true },
159 { count: 3, value: "false"},
160 { count: 4, value: false },
161 { count: 5, value: true },
162 { count: 6, value: true },
163 { count: 7, value: "true" },
164 { count: 8, value: "true" },
165 { count: 9, value: "false"},
166 { count: 10, value: false },
167 { count: 11, value: [false, false]},
168 { count: "12", value: [false, "true"]}
173 }else if (name === "countries_references"){
175 data = {url: dojo.moduleUrl("tests", "data/countries_references.json").toString() };
177 data = {data: { identifier: 'name',
180 { name:'Africa', type:'continent',
181 children:[{_reference:'Egypt'}, {_reference:'Kenya'}, {_reference:'Sudan'}] },
182 { name:'Egypt', type:'country' },
183 { name:'Kenya', type:'country',
184 children:[{_reference:'Nairobi'}, {_reference:'Mombasa'}] },
185 { name:'Nairobi', type:'city' },
186 { name:'Mombasa', type:'city' },
187 { name:'Sudan', type:'country',
188 children:{_reference:'Khartoum'} },
189 { name:'Khartoum', type:'city' },
190 { name:'Asia', type:'continent',
191 children:[{_reference:'China'}, {_reference:'India'}, {_reference:'Russia'}, {_reference:'Mongolia'}] },
192 { name:'China', type:'country' },
193 { name:'India', type:'country' },
194 { name:'Russia', type:'country' },
195 { name:'Mongolia', type:'country' },
196 { name:'Australia', type:'continent', population:'21 million',
197 children:{_reference:'Commonwealth of Australia'}},
198 { name:'Commonwealth of Australia', type:'country', population:'21 million'},
199 { name:'Europe', type:'continent',
200 children:[{_reference:'Germany'}, {_reference:'France'}, {_reference:'Spain'}, {_reference:'Italy'}] },
201 { name:'Germany', type:'country' },
202 { name:'France', type:'country' },
203 { name:'Spain', type:'country' },
204 { name:'Italy', type:'country' },
205 { name:'North America', type:'continent',
206 children:[{_reference:'Mexico'}, {_reference:'Canada'}, {_reference:'United States of America'}] },
207 { name:'Mexico', type:'country', population:'108 million', area:'1,972,550 sq km',
208 children:[{_reference:'Mexico City'}, {_reference:'Guadalajara'}] },
209 { name:'Mexico City', type:'city', population:'19 million', timezone:'-6 UTC'},
210 { name:'Guadalajara', type:'city', population:'4 million', timezone:'-6 UTC' },
211 { name:'Canada', type:'country', population:'33 million', area:'9,984,670 sq km',
212 children:[{_reference:'Ottawa'}, {_reference:'Toronto'}] },
213 { name:'Ottawa', type:'city', population:'0.9 million', timezone:'-5 UTC'},
214 { name:'Toronto', type:'city', population:'2.5 million', timezone:'-5 UTC' },
215 { name:'United States of America', type:'country' },
216 { name:'South America', type:'continent',
217 children:[{_reference:'Brazil'}, {_reference:'Argentina'}] },
218 { name:'Brazil', type:'country', population:'186 million' },
219 { name:'Argentina', type:'country', population:'40 million' }
228 //-----------------------------------------------------
230 tests.data.readOnlyItemFileTestTemplates.testTemplates = [
232 name: "Identity API: fetchItemByIdentity()",
233 runTest: function(datastore, t){
235 // Simple test of the fetchItemByIdentity function of the store.
236 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
238 var d = new doh.Deferred();
239 function onItem(item){
240 t.assertTrue(item !== null);
242 var name = store.getValue(item,"name");
243 t.assertEqual(name, "El Salvador");
247 function onError(errData){
251 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
252 return d; // Deferred
256 name: "Identity API: fetchItemByIdentity() notFound",
257 runTest: function(datastore, t){
259 // Simple test of the fetchItemByIdentity function of the store.
261 // Simple test of the fetchItemByIdentity function of the store.
262 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
264 var d = new doh.Deferred();
265 function onItem(item){
266 t.assertTrue(item === null);
269 function onError(errData){
273 store.fetchItemByIdentity({identity: "sv_not", onItem: onItem, onError: onError});
274 return d; // Deferred
278 name: "Identity API: getIdentityAttributes()",
279 runTest: function(datastore, t){
281 // Simple test of the getIdentityAttributes function.
283 // Simple test of the getIdentityAttributes function.
284 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
286 var d = new doh.Deferred();
287 function onItem(item){
288 t.assertTrue(item !== null)
289 var identifiers = store.getIdentityAttributes(item);
290 t.assertTrue(dojo.isArray(identifiers));
291 t.assertEqual(1, identifiers.length);
292 t.assertEqual("abbr", identifiers[0]);
295 function onError(errData){
299 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
300 return d; // Deferred
304 name: "Identity API: fetchItemByIdentity() commentFilteredJson",
305 runTest: function(datastore, t){
307 // Simple test of the fetchItemByIdentity function of the store.
309 // Simple test of the fetchItemByIdentity function of the store.
310 // This tests loading a comment-filtered json file so that people using secure
311 // data with this store can bypass the JavaSceipt hijack noted in Fortify's
315 var store = new datastore({url: dojo.moduleUrl("tests", "data/countries_commentFiltered.json").toString()});
317 var d = new doh.Deferred();
318 function onItem(item){
319 t.assertTrue(item !== null);
320 var name = store.getValue(item,"name");
321 t.assertEqual(name, "El Salvador");
324 function onError(errData){
328 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
329 return d; // Deferred
334 name: "Identity API: fetchItemByIdentity() nullValue",
335 runTest: function(datastore, t){
337 // Simple test of the fetchItemByIdentity function of the store, checling a null value.
339 // Simple test of the fetchItemByIdentity function of the store, checking a null value.
340 // This tests handling attributes in json that were defined as null properly.
341 // Introduced because of tracker: #3153
342 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withNull"));
344 var d = new doh.Deferred();
345 function onItem(item){
346 t.assertTrue(item !== null);
347 var name = store.getValue(item,"name");
348 t.assertEqual(name, null);
351 function onError(errData){
355 store.fetchItemByIdentity({identity: "ec", onItem: onItem, onError: onError});
356 return d; // Deferred
360 name: "Identity API: fetchItemByIdentity() booleanValue",
361 runTest: function(datastore, t){
363 // Simple test of the fetchItemByIdentity function of the store, checking a boolean value.
365 // Simple test of the fetchItemByIdentity function of the store, checking a boolean value.
366 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withBoolean"));
368 var d = new doh.Deferred();
369 function onItem(item){
370 t.assertTrue(item !== null);
371 var name = store.getValue(item,"name");
372 t.assertEqual(name, "Utopia");
373 var real = store.getValue(item,"real");
374 t.assertEqual(real, false);
377 function onError(errData){
381 store.fetchItemByIdentity({identity: "ut", onItem: onItem, onError: onError});
382 return d; // Deferred
386 name: "Identity API: fetchItemByIdentity() withoutSpecifiedIdInData",
387 runTest: function(datastore, t){
389 // Simple test of bug #4691, looking up something by assigned id, not one specified in the JSON data.
391 // Simple test of bug #4691, looking up something by assigned id, not one specified in the JSON data.
392 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withoutid"));
394 var d = new doh.Deferred();
395 function onItem(item){
396 t.assertTrue(item !== null);
397 var name = store.getValue(item,"name");
398 t.assertEqual(name, "El Salvador");
401 function onError(errData){
405 store.fetchItemByIdentity({identity: "2", onItem: onItem, onError: onError});
406 return d; // Deferred
410 name: "Identity API: getIdentity()",
411 runTest: function(datastore, t){
413 // Simple test of the getIdentity function of the store.
415 // Simple test of the getIdentity function of the store.
416 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
418 var d = new doh.Deferred();
419 function onItem(item){
420 t.assertTrue(item !== null);
421 t.assertTrue(store.getIdentity(item) === "sv");
424 function onError(errData){
428 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
429 return d; // Deferred
433 name: "Identity API: getIdentity() withoutSpecifiedId",
434 runTest: function(datastore, t){
436 // Simple test of the #4691 bug
438 // Simple test of the #4691 bug
439 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withoutid"));
441 var d = new doh.Deferred();
442 function onItem(item, request){
443 t.assertTrue(item !== null);
444 t.assertTrue(store.getIdentity(item) === 2);
447 function onError(errData, request){
451 store.fetch({ query:{abbr: "sv"}, onItem: onItem, onError: onError});
452 return d; // Deferred
456 name: "Read API: fetch() all",
457 runTest: function(datastore, t){
459 // Simple test of a basic fetch on ItemFileReadStore.
461 // Simple test of a basic fetch on ItemFileReadStore.
462 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
464 var d = new doh.Deferred();
465 function completedAll(items, request){
466 t.is(7, items.length);
469 function error(errData, request){
475 store.fetch({ onComplete: completedAll, onError: error});
480 name: "Read API: fetch() one",
481 runTest: function(datastore, t){
483 // Simple test of a basic fetch on ItemFileReadStore of a single item.
485 // Simple test of a basic fetch on ItemFileReadStore of a single item.
486 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
488 var d = new doh.Deferred();
489 function onComplete(items, request){
490 t.assertEqual(items.length, 1);
493 function onError(errData, request){
497 store.fetch({ query: {abbr: "ec"},
498 onComplete: onComplete,
505 name: "Read API: fetch() shallow",
506 runTest: function(datastore, t){
508 // Simple test of a basic fetch on ItemFileReadStore of only toplevel items
510 // Simple test of a basic fetch on ItemFileReadStore of only toplevel items.
511 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("geography_hierarchy_small"));
513 var d = new doh.Deferred();
514 function onComplete(items, request){
515 t.assertEqual(items.length, 2);
518 function onError(errData, request){
522 //Find all items starting with A, only toplevel (root) items.
523 store.fetch({ query: {name: "A*"},
524 onComplete: onComplete,
531 name: "Read API: fetch() Multiple",
532 runTest: function(datastore, t){
534 // Tests that multiple fetches at the same time queue up properly and do not clobber each other on initial load.
536 // Tests that multiple fetches at the same time queue up properly and do not clobber each other on initial load.
537 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("geography_hierarchy_small"));
539 var d = new doh.Deferred();
540 var done = [false, false];
542 function onCompleteOne(items, request){
544 t.assertEqual(items.length, 2);
545 if(done[0] && done[1]){
549 function onCompleteTwo(items, request){
551 if(done[0] && done[1]){
555 function onError(errData, request){
559 //Find all items starting with A, only toplevel (root) items.
560 store.fetch({ query: {name: "A*"},
561 onComplete: onCompleteOne,
565 //Find all items starting with A, only toplevel (root) items.
566 store.fetch({ query: {name: "N*"},
567 onComplete: onCompleteTwo,
575 name: "Read API: fetch() MultipleMixedFetch",
576 runTest: function(datastore, t){
578 // Tests that multiple fetches at the same time queue up properly and do not clobber each other on initial load.
580 // Tests that multiple fetches at the same time queue up properly and do not clobber each other on initial load.
581 // Tests an item fetch and an identity fetch.
582 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
584 var d = new doh.Deferred();
585 var done = [false, false];
587 function onComplete(items, request){
589 t.assertEqual(items.length, 1);
590 if(done[0] && done[1]){
594 function onItem(item){
596 t.assertTrue(item !== null);
597 var name = store.getValue(item,"name");
598 t.assertEqual(name, "El Salvador");
600 if(done[0] && done[1]){
604 function onError(errData){
609 //Find all items starting with A, only toplevel (root) items.
610 store.fetch({ query: {name: "El*"},
611 onComplete: onComplete,
615 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
620 name: "Read API: fetch() deep",
621 runTest: function(datastore, t){
623 // Simple test of a basic fetch on ItemFileReadStore of all items (including children (nested))
625 // Simple test of a basic fetch on ItemFileReadStore of all items (including children (nested))
626 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("geography_hierarchy_small"));
628 var d = new doh.Deferred();
629 function onComplete(items, request){
630 t.assertEqual(items.length, 4);
633 function onError(errData, request){
637 //Find all items starting with A, including child (nested) items.
638 store.fetch({ query: {name: "A*"},
639 onComplete: onComplete,
641 queryOptions: {deep:true}
647 name: "Read API: fetch() one_commentFilteredJson",
648 runTest: function(datastore, t){
650 // Simple test of a basic fetch on ItemFileReadStore of a single item.
652 // Simple test of a basic fetch on ItemFileReadStore of a single item.
653 // This tests loading a comment-filtered json file so that people using secure
654 // data with this store can bypass the JavaSceipt hijack noted in Fortify's
657 var store = new datastore({url: dojo.moduleUrl("tests", "data/countries_commentFiltered.json").toString()});
659 var d = new doh.Deferred();
660 function onComplete(items, request){
661 t.assertEqual(items.length, 1);
664 function onError(errData, request){
668 store.fetch({ query: {abbr: "ec"},
669 onComplete: onComplete,
677 name: "Read API: fetch() withNull",
678 runTest: function(datastore, t){
680 // Simple test of a basic fetch on ItemFileReadStore of a single item where some attributes are null.
682 // Simple test of a basic fetch on ItemFileReadStore of a single item where some attributes are null.
683 // Introduced because of tracker: #3153
684 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withNull"));
686 var d = new doh.Deferred();
687 function onComplete(items, request){
688 t.assertEqual(4, items.length);
691 function onError(errData, request){
695 store.fetch({ query: {name: "E*"},
696 onComplete: onComplete,
703 name: "Read API: fetch() all_streaming",
704 runTest: function(datastore, t){
706 // Simple test of a basic fetch on ItemFileReadStore.
708 // Simple test of a basic fetch on ItemFileReadStore.
709 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
711 var d = new doh.Deferred();
714 function onBegin(size, requestObj){
715 t.assertEqual(size, 7);
717 function onItem(item, requestObj){
718 t.assertTrue(store.isItem(item));
721 function onComplete(items, request){
722 t.assertEqual(count, 7);
723 t.assertTrue(items === null);
726 function onError(errData, request){
732 store.fetch({ onBegin: onBegin,
734 onComplete: onComplete,
741 name: "Read API: fetch() paging",
742 runTest: function(datastore, t){
744 // Test of multiple fetches on a single result. Paging, if you will.
746 // Test of multiple fetches on a single result. Paging, if you will.
747 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
749 var d = new doh.Deferred();
750 function dumpFirstFetch(items, request){
751 t.assertEqual(items.length, 5);
754 request.onComplete = dumpSecondFetch;
755 store.fetch(request);
758 function dumpSecondFetch(items, request){
759 t.assertEqual(items.length, 1);
762 request.onComplete = dumpThirdFetch;
763 store.fetch(request);
766 function dumpThirdFetch(items, request){
767 t.assertEqual(items.length, 5);
770 request.onComplete = dumpFourthFetch;
771 store.fetch(request);
774 function dumpFourthFetch(items, request){
775 t.assertEqual(items.length, 5);
778 request.onComplete = dumpFifthFetch;
779 store.fetch(request);
782 function dumpFifthFetch(items, request){
783 t.assertEqual(items.length, 0);
786 request.onComplete = dumpSixthFetch;
787 store.fetch(request);
790 function dumpSixthFetch(items, request){
791 t.assertEqual(items.length, 5);
795 function completed(items, request){
796 t.assertEqual(items.length, 7);
799 request.onComplete = dumpFirstFetch;
800 store.fetch(request);
803 function error(errData, request){
807 store.fetch({onComplete: completed, onError: error});
812 name: "Read API: fetch() with MultiType Match",
813 runTest: function(datastore, t){
815 // Simple test of a basic fetch againct an attribute that has different types for the value across items
817 // Simple test of a basic fetch againct an attribute that has different types for the value across items
818 // Introduced because of tracker: #4931
819 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("data_multitype"));
821 var d = new doh.Deferred();
822 function onComplete(items, request){
823 t.assertEqual(4, items.length);
826 function onError(errData, request){
830 store.fetch({ query: {count: "1*"},
831 onComplete: onComplete,
838 name: "Read API: fetch() with MultiType, MultiValue Match",
839 runTest: function(datastore, t){
841 // Simple test of a basic fetch againct an attribute that has different types for the value across items
843 // Simple test of a basic fetch againct an attribute that has different types for the value across items
844 // Introduced because of tracker: #4931
845 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("data_multitype"));
847 var d = new doh.Deferred();
848 function onComplete(items, request){
849 t.assertEqual(7, items.length);
852 function onError(errData, request){
856 store.fetch({ query: {value: "true"},
857 onComplete: onComplete,
864 name: "Read API: getLabel()",
865 runTest: function(datastore, t){
867 // Simple test of the getLabel function against a store set that has a label defined.
869 // Simple test of the getLabel function against a store set that has a label defined.
870 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
872 var d = new doh.Deferred();
873 function onComplete(items, request){
874 t.assertEqual(items.length, 1);
875 var label = store.getLabel(items[0]);
876 t.assertTrue(label !== null);
877 t.assertEqual("Ecuador", label);
880 function onError(errData, request){
884 store.fetch({ query: {abbr: "ec"},
885 onComplete: onComplete,
892 name: "Read API: getLabelAttributes()",
893 runTest: function(datastore, t){
895 // Simple test of the getLabelAttributes function against a store set that has a label defined.
897 // Simple test of the getLabelAttributes function against a store set that has a label defined.
898 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
900 var d = new doh.Deferred();
901 function onComplete(items, request){
902 t.assertEqual(items.length, 1);
903 var labelList = store.getLabelAttributes(items[0]);
904 t.assertTrue(dojo.isArray(labelList));
905 t.assertEqual("name", labelList[0]);
908 function onError(errData, request){
912 store.fetch({ query: {abbr: "ec"},
913 onComplete: onComplete,
920 name: "Read API: getValue()",
921 runTest: function(datastore, t){
923 // Simple test of the getValue function of the store.
925 // Simple test of the getValue function of the store.
926 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
928 var d = new doh.Deferred();
929 function onItem(item){
930 t.assertTrue(item !== null);
931 var name = store.getValue(item,"name");
932 t.assertTrue(name === "El Salvador");
935 function onError(errData){
939 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
940 return d; // Deferred
944 name: "Read API: getValues()",
945 runTest: function(datastore, t){
947 // Simple test of the getValues function of the store.
949 // Simple test of the getValues function of the store.
950 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
952 var d = new doh.Deferred();
953 function onItem(item){
954 t.assertTrue(item !== null);
955 var names = store.getValues(item,"name");
956 t.assertTrue(dojo.isArray(names));
957 t.assertEqual(names.length, 1);
958 t.assertEqual(names[0], "El Salvador");
961 function onError(errData){
965 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
966 return d; // Deferred
970 name: "Read API: isItem()",
971 runTest: function(datastore, t){
973 // Simple test of the isItem function of the store
975 // Simple test of the isItem function of the store
976 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
978 var d = new doh.Deferred();
979 function onItem(item){
980 t.assertTrue(item !== null);
981 t.assertTrue(store.isItem(item));
982 t.assertTrue(!store.isItem({}));
985 function onError(errData){
989 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
990 return d; // Deferred
994 name: "Read API: isItem() multistore",
995 runTest: function(datastore, t){
997 // Simple test of the isItem function of the store
998 // to verify two different store instances do not accept
999 // items from each other.
1001 // Simple test of the isItem function of the store
1002 // to verify two different store instances do not accept
1003 // items from each other.
1005 // Two different instances, even if they read from the same URL
1006 // should not accept items between each other!
1007 var store1 = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1008 var store2 = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1010 var d = new doh.Deferred();
1011 function onItem1(item1){
1012 t.assertTrue(item1 !== null);
1014 function onItem2(item2){
1015 t.assertTrue(item1 !== null);
1016 t.assertTrue(item2 !== null);
1017 t.assertTrue(store1.isItem(item1));
1018 t.assertTrue(store2.isItem(item2));
1019 t.assertTrue(!store1.isItem(item2));
1020 t.assertTrue(!store2.isItem(item1));
1023 store2.fetchItemByIdentity({identity: "sv", onItem: onItem2, onError: onError});
1026 function onError(errData){
1027 t.assertTrue(false);
1030 store1.fetchItemByIdentity({identity: "sv", onItem: onItem1, onError: onError});
1031 return d; // Deferred
1035 name: "Read API: hasAttribute()",
1036 runTest: function(datastore, t){
1038 // Simple test of the hasAttribute function of the store
1040 // Simple test of the hasAttribute function of the store
1041 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1043 var d = new doh.Deferred();
1044 function onItem(item){
1045 t.assertTrue(item !== null);
1046 t.assertTrue(store.hasAttribute(item, "abbr"));
1047 t.assertTrue(!store.hasAttribute(item, "abbr_not"));
1049 //Test that null attributes throw an exception
1052 store.hasAttribute(item, null);
1056 t.assertTrue(passed);
1059 function onError(errData){
1060 t.assertTrue(false);
1063 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
1064 return d; // Deferred
1068 name: "Read API: containsValue()",
1069 runTest: function(datastore, t){
1071 // Simple test of the containsValue function of the store
1073 // Simple test of the containsValue function of the store
1074 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1076 var d = new doh.Deferred();
1077 function onItem(item){
1078 t.assertTrue(item !== null);
1079 t.assertTrue(store.containsValue(item, "abbr", "sv"));
1080 t.assertTrue(!store.containsValue(item, "abbr", "sv1"));
1081 t.assertTrue(!store.containsValue(item, "abbr", null));
1083 //Test that null attributes throw an exception
1086 store.containsValue(item, null, "foo");
1090 t.assertTrue(passed);
1093 function onError(errData){
1094 t.assertTrue(false);
1097 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
1098 return d; // Deferred
1102 name: "Read API: getAttributes()",
1103 runTest: function(datastore, t){
1105 // Simple test of the getAttributes function of the store
1107 // Simple test of the getAttributes function of the store
1108 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1110 var d = new doh.Deferred();
1111 function onItem(item){
1112 t.assertTrue(item !== null);
1113 t.assertTrue(store.isItem(item));
1115 var attributes = store.getAttributes(item);
1116 t.assertEqual(attributes.length, 3);
1117 for(var i = 0; i < attributes.length; i++){
1118 t.assertTrue((attributes[i] === "name" || attributes[i] === "abbr" || attributes[i] === "capital"));
1122 function onError(errData){
1123 t.assertTrue(false);
1126 store.fetchItemByIdentity({identity: "sv", onItem: onItem, onError: onError});
1127 return d; // Deferred
1131 name: "Read API: getFeatures()",
1132 runTest: function(datastore, t){
1134 // Simple test of the getFeatures function of the store
1136 // Simple test of the getFeatures function of the store
1137 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1139 var features = store.getFeatures();
1140 t.assertTrue(features["dojo.data.api.Read"] != null);
1141 t.assertTrue(features["dojo.data.api.Identity"] != null);
1145 name: "Read API: fetch() patternMatch0",
1146 runTest: function(datastore, t){
1148 // Function to test pattern matching of everything starting with lowercase e
1150 // Function to test pattern matching of everything starting with lowercase e
1151 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
1153 var d = new doh.Deferred();
1154 function completed(items, request) {
1155 t.assertEqual(items.length, 5);
1157 for(var i = 0; i < items.length; i++){
1158 var value = store.getValue(items[i], "abbr");
1159 if(!(value === "ec" || value === "eg" || value === "er" || value === "ee" || value === "et")){
1164 t.assertTrue(passed);
1168 d.errback(new Error("Unexpected abbreviation found, match failure."));
1171 function error(error, request) {
1172 t.assertTrue(false);
1175 store.fetch({query: {abbr: "e*"}, onComplete: completed, onError: error});
1180 name: "Read API: fetch() patternMatch1",
1181 runTest: function(datastore, t){
1183 // Function to test pattern matching of everything with $ in it.
1185 // Function to test pattern matching of everything with $ in it.
1187 var store = new datastore({data: { identifier: "uniqueId",
1188 items: [ {uniqueId: 1, value:"foo*bar"},
1189 {uniqueId: 2, value:"bar*foo"},
1190 {uniqueId: 3, value:"boomBam"},
1191 {uniqueId: 4, value:"bit$Bite"},
1192 {uniqueId: 5, value:"ouagadogou"},
1193 {uniqueId: 6, value:"BaBaMaSaRa***Foo"},
1194 {uniqueId: 7, value:"squawl"},
1195 {uniqueId: 8, value:"seaweed"},
1196 {uniqueId: 9, value:"jfq4@#!$!@Rf14r14i5u"}
1201 var d = new doh.Deferred();
1202 function completed(items, request){
1203 t.assertEqual(items.length, 2);
1205 for(var i = 0; i < items.length; i++){
1206 var value = store.getValue(items[i], "value");
1207 if(!(value === "bit$Bite" || value === "jfq4@#!$!@Rf14r14i5u")){
1212 t.assertTrue(passed);
1216 d.errback(new Error("Unexpected pattern matched. Filter failure."));
1219 function error(error, request){
1220 t.assertTrue(false);
1223 store.fetch({query: {value: "*$*"}, onComplete: completed, onError: error});
1228 name: "Read API: fetch() patternMatch2",
1229 runTest: function(datastore, t){
1231 // Function to test exact pattern match
1233 // Function to test exact pattern match
1235 var store = new datastore({data: { identifier: "uniqueId",
1236 items: [ {uniqueId: 1, value:"foo*bar"},
1237 {uniqueId: 2, value:"bar*foo"},
1238 {uniqueId: 3, value:"boomBam"},
1239 {uniqueId: 4, value:"bit$Bite"},
1240 {uniqueId: 5, value:"ouagadogou"},
1241 {uniqueId: 6, value:"BaBaMaSaRa***Foo"},
1242 {uniqueId: 7, value:"squawl"},
1243 {uniqueId: 8, value:"seaweed"},
1244 {uniqueId: 9, value:"jfq4@#!$!@Rf14r14i5u"}
1249 var d = new doh.Deferred();
1250 function completed(items, request){
1251 t.assertEqual(items.length, 1);
1253 for(var i = 0; i < items.length; i++){
1254 var value = store.getValue(items[i], "value");
1255 if(!(value === "bar*foo")){
1260 t.assertTrue(passed);
1264 d.errback(new Error("Unexpected abbreviation found, match failure."));
1267 function error(error, request){
1268 t.assertTrue(false);
1271 store.fetch({query: {value: "bar\*foo"}, onComplete: completed, onError: error});
1276 name: "Read API: fetch() patternMatch_caseSensitive",
1277 runTest: function(datastore, t){
1279 // Function to test pattern matching of a pattern case-sensitively
1281 // Function to test pattern matching of a pattern case-sensitively
1283 var store = new datastore({data: { identifier: "uniqueId",
1284 items: [ {uniqueId: 1, value:"foo*bar"},
1285 {uniqueId: 2, value:"bar*foo"},
1286 {uniqueId: 3, value:"BAR*foo"},
1287 {uniqueId: 4, value:"BARBananafoo"}
1292 var d = new doh.Deferred();
1293 function completed(items, request){
1294 t.assertEqual(1, items.length);
1296 for(var i = 0; i < items.length; i++){
1297 var value = store.getValue(items[i], "value");
1298 if(!(value === "bar*foo")){
1303 t.assertTrue(passed);
1307 d.errback(new Error("Unexpected pattern matched. Filter failure."));
1310 function error(error, request){
1311 t.assertTrue(false);
1314 store.fetch({query: {value: "bar\\*foo"}, queryOptions: {ignoreCase: false} , onComplete: completed, onError: error});
1319 name: "Read API: fetch() patternMatch_caseInsensitive",
1320 runTest: function(datastore, t){
1322 // Function to test pattern matching of a pattern case-insensitively
1324 // Function to test pattern matching of a pattern case-insensitively
1326 var store = new datastore({data: { identifier: "uniqueId",
1327 items: [ {uniqueId: 1, value:"foo*bar"},
1328 {uniqueId: 2, value:"bar*foo"},
1329 {uniqueId: 3, value:"BAR*foo"},
1330 {uniqueId: 4, value:"BARBananafoo"}
1335 var d = new doh.Deferred();
1336 function completed(items, request){
1337 t.assertEqual(items.length, 2);
1339 for(var i = 0; i < items.length; i++){
1340 var value = store.getValue(items[i], "value");
1341 if(!(value === "BAR*foo" || value === "bar*foo")){
1346 t.assertTrue(passed);
1350 d.errback(new Error("Unexpected pattern matched. Filter failure."));
1353 function error(error, request){
1354 t.assertTrue(false);
1357 store.fetch({query: {value: "bar\\*foo"}, queryOptions: {ignoreCase: true}, onComplete: completed, onError: error});
1362 name: "Read API: fetch() sortNumeric",
1363 runTest: function(datastore, t){
1365 // Function to test sorting numerically.
1367 // Function to test sorting numerically.
1369 var store = new datastore({data: { identifier: "uniqueId",
1370 items: [ {uniqueId: 0, value:"fo|o*b.ar"},
1371 {uniqueId: 1, value:"ba|r*foo"},
1372 {uniqueId: 2, value:"boomBam"},
1373 {uniqueId: 3, value:"bit$Bite"},
1374 {uniqueId: 4, value:"ouagadogou"},
1375 {uniqueId: 5, value:"jfq4@#!$!@|f1.$4r14i5u"},
1376 {uniqueId: 6, value:"BaB{aMa|SaRa***F}oo"},
1377 {uniqueId: 7, value:"squawl"},
1378 {uniqueId: 9, value:"seaweed"},
1379 {uniqueId: 10, value:"zulu"},
1380 {uniqueId: 8, value:"seaweed"}
1385 var d = new doh.Deferred();
1386 function completed(items, request){
1387 t.assertEqual(items.length, 11);
1389 for(var i = 0; i < items.length; i++){
1390 var value = store.getValue(items[i], "value");
1391 if(!(store.getValue(items[i], "uniqueId") === i)){
1396 t.assertTrue(passed);
1400 d.errback(new Error("Unexpected sorting order found, sort failure."));
1404 function error(error, request){
1405 t.assertTrue(false);
1409 var sortAttributes = [{attribute: "uniqueId"}];
1410 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1415 name: "Read API: fetch() sortNumericDescending",
1416 runTest: function(datastore, t){
1418 // Function to test sorting numerically.
1420 // Function to test sorting numerically.
1422 var store = new datastore({data: { identifier: "uniqueId",
1423 items: [ {uniqueId: 0, value:"fo|o*b.ar"},
1424 {uniqueId: 1, value:"ba|r*foo"},
1425 {uniqueId: 2, value:"boomBam"},
1426 {uniqueId: 3, value:"bit$Bite"},
1427 {uniqueId: 4, value:"ouagadogou"},
1428 {uniqueId: 5, value:"jfq4@#!$!@|f1.$4r14i5u"},
1429 {uniqueId: 6, value:"BaB{aMa|SaRa***F}oo"},
1430 {uniqueId: 7, value:"squawl"},
1431 {uniqueId: 9, value:"seaweed"},
1432 {uniqueId: 10, value:"zulu"},
1433 {uniqueId: 8, value:"seaweed"}
1437 var d = new doh.Deferred();
1438 function completed(items, request){
1439 t.assertEqual(items.length, 11);
1441 for(var i = 0; i < items.length; i++){
1442 var value = store.getValue(items[i], "value");
1443 if(!((items.length - (store.getValue(items[i], "uniqueId") + 1)) === i)){
1448 t.assertTrue(passed);
1452 d.errback(new Error("Unexpected sorting order found, sort failure."));
1456 function error(error, request){
1457 t.assertTrue(false);
1461 var sortAttributes = [{attribute: "uniqueId", descending: true}];
1462 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1467 name: "Read API: fetch() sortNumericWithCount",
1468 runTest: function(datastore, t){
1470 // Function to test sorting numerically in descending order, returning only a specified number of them.
1472 // Function to test sorting numerically in descending order, returning only a specified number of them.
1474 var store = new datastore({data: { identifier: "uniqueId",
1475 items: [ {uniqueId: 0, value:"fo|o*b.ar"},
1476 {uniqueId: 1, value:"ba|r*foo"},
1477 {uniqueId: 2, value:"boomBam"},
1478 {uniqueId: 3, value:"bit$Bite"},
1479 {uniqueId: 4, value:"ouagadogou"},
1480 {uniqueId: 5, value:"jfq4@#!$!@|f1.$4r14i5u"},
1481 {uniqueId: 6, value:"BaB{aMa|SaRa***F}oo"},
1482 {uniqueId: 7, value:"squawl"},
1483 {uniqueId: 9, value:"seaweed"},
1484 {uniqueId: 10, value:"zulu"},
1485 {uniqueId: 8, value:"seaweed"}
1490 var d = new doh.Deferred();
1491 function completed(items, request){
1492 t.assertEqual(items.length, 5);
1495 for(var i = 0; i < items.length; i++){
1496 var value = store.getValue(items[i], "value");
1497 if(!(store.getValue(items[i], "uniqueId") === itemId)){
1501 itemId--; // Decrement the item id. We are descending sorted, so it should go 10, 9, 8, etc.
1503 t.assertTrue(passed);
1507 d.errback(new Error("Unexpected sorting order found, sort failure."));
1511 function error(error, request){
1512 t.assertTrue(false);
1516 var sortAttributes = [{attribute: "uniqueId", descending: true}];
1517 store.fetch({onComplete: completed, onError: error, sort: sortAttributes, count: 5});
1522 name: "Read API: fetch() sortAlphabetic",
1523 runTest: function(datastore, t){
1525 // Function to test sorting alphabetic ordering.
1527 // Function to test sorting alphabetic ordering.
1529 var store = new datastore({data: { identifier: "uniqueId",
1530 items: [ {uniqueId: 0, value:"abc"},
1531 {uniqueId: 1, value:"bca"},
1532 {uniqueId: 2, value:"abcd"},
1533 {uniqueId: 3, value:"abcdefg"},
1534 {uniqueId: 4, value:"lmnop"},
1535 {uniqueId: 5, value:"foghorn"},
1536 {uniqueId: 6, value:"qberty"},
1537 {uniqueId: 7, value:"qwerty"},
1538 {uniqueId: 8, value:""},
1539 {uniqueId: 9, value:"seaweed"},
1540 {uniqueId: 10, value:"123abc"}
1546 var d = new doh.Deferred();
1547 function completed(items, request){
1548 //Output should be in this order...
1549 var orderedArray = [ "",
1561 t.assertEqual(items.length, 11);
1563 for(var i = 0; i < items.length; i++){
1564 var value = store.getValue(items[i], "value");
1565 if(!(store.getValue(items[i], "value") === orderedArray[i])){
1570 t.assertTrue(passed);
1574 d.errback(new Error("Unexpected sorting order found, sort failure."));
1578 function error(error, request) {
1579 t.assertTrue(false);
1583 var sortAttributes = [{attribute: "value"}];
1584 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1589 name: "Read API: fetch() sortAlphabeticDescending",
1590 runTest: function(datastore, t){
1592 // Function to test sorting alphabetic ordering in descending mode.
1594 // Function to test sorting alphabetic ordering in descending mode.
1596 var store = new datastore({data: { identifier: "uniqueId",
1597 items: [ {uniqueId: 0, value:"abc"},
1598 {uniqueId: 1, value:"bca"},
1599 {uniqueId: 2, value:"abcd"},
1600 {uniqueId: 3, value:"abcdefg"},
1601 {uniqueId: 4, value:"lmnop"},
1602 {uniqueId: 5, value:"foghorn"},
1603 {uniqueId: 6, value:"qberty"},
1604 {uniqueId: 7, value:"qwerty"},
1605 {uniqueId: 8, value:""},
1606 {uniqueId: 9, value:"seaweed"},
1607 {uniqueId: 10, value:"123abc"}
1612 var d = new doh.Deferred();
1613 function completed(items, request){
1614 //Output should be in this order...
1615 var orderedArray = [ "",
1627 orderedArray = orderedArray.reverse();
1628 t.assertEqual(items.length, 11);
1631 for(var i = 0; i < items.length; i++){
1632 var value = store.getValue(items[i], "value");
1633 if(!(store.getValue(items[i], "value") === orderedArray[i])){
1638 t.assertTrue(passed);
1642 d.errback(new Error("Unexpected sorting order found, sort failure."));
1646 function error(error, request) {
1647 t.assertTrue(false);
1651 var sortAttributes = [{attribute: "value", descending: true}];
1652 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1657 name: "Read API: fetch() sortDate",
1658 runTest: function(datastore, t){
1660 // Function to test sorting date.
1662 // Function to test sorting date.
1664 var store = new datastore({data: { identifier: "uniqueId",
1665 items: [ {uniqueId: 0, value: new Date(0)},
1666 {uniqueId: 1, value: new Date(100)},
1667 {uniqueId: 2, value:new Date(1000)},
1668 {uniqueId: 3, value:new Date(2000)},
1669 {uniqueId: 4, value:new Date(3000)},
1670 {uniqueId: 5, value:new Date(4000)},
1671 {uniqueId: 6, value:new Date(5000)},
1672 {uniqueId: 7, value:new Date(6000)},
1673 {uniqueId: 8, value:new Date(7000)},
1674 {uniqueId: 9, value:new Date(8000)},
1675 {uniqueId: 10, value:new Date(9000)}
1681 var d = new doh.Deferred();
1682 function completed(items,request){
1683 var orderedArray = [0,100,1000,2000,3000,4000,5000,6000,7000,8000,9000];
1684 t.assertEqual(items.length, 11);
1686 for(var i = 0; i < items.length; i++){
1687 var value = store.getValue(items[i], "value");
1688 if(!(store.getValue(items[i], "value").getTime() === orderedArray[i])){
1693 t.assertTrue(passed);
1697 d.errback(new Error("Unexpected sorting order found, sort failure."));
1701 function error(error, request){
1702 t.assertTrue(false);
1706 var sortAttributes = [{attribute: "value"}];
1707 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1712 name: "Read API: fetch() sortDateDescending",
1713 runTest: function(datastore, t){
1715 // Function to test sorting date in descending order.
1717 // Function to test sorting date in descending order.
1719 var store = new datastore({data: { identifier: "uniqueId",
1720 items: [ {uniqueId: 0, value: new Date(0)},
1721 {uniqueId: 1, value: new Date(100)},
1722 {uniqueId: 2, value:new Date(1000)},
1723 {uniqueId: 3, value:new Date(2000)},
1724 {uniqueId: 4, value:new Date(3000)},
1725 {uniqueId: 5, value:new Date(4000)},
1726 {uniqueId: 6, value:new Date(5000)},
1727 {uniqueId: 7, value:new Date(6000)},
1728 {uniqueId: 8, value:new Date(7000)},
1729 {uniqueId: 9, value:new Date(8000)},
1730 {uniqueId: 10, value:new Date(9000)}
1736 var d = new doh.Deferred();
1737 function completed(items,request){
1738 var orderedArray = [0,100,1000,2000,3000,4000,5000,6000,7000,8000,9000];
1739 orderedArray = orderedArray.reverse();
1740 t.assertEqual(items.length, 11);
1742 for(var i = 0; i < items.length; i++){
1743 var value = store.getValue(items[i], "value");
1744 if(!(store.getValue(items[i], "value").getTime() === orderedArray[i])){
1749 t.assertTrue(passed);
1753 d.errback(new Error("Unexpected sorting order found, sort failure."));
1757 function error(error, request){
1758 t.assertTrue(false);
1762 var sortAttributes = [{attribute: "value", descending: true}];
1763 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1768 name: "Read API: fetch() sortMultiple",
1769 runTest: function(datastore, t){
1771 // Function to test sorting on multiple attributes.
1773 // Function to test sorting on multiple attributes.
1775 var store = new datastore({data: { identifier: "uniqueId",
1776 items: [ {uniqueId: 1, value:"fo|o*b.ar"},
1777 {uniqueId: 2, value:"ba|r*foo"},
1778 {uniqueId: 3, value:"boomBam"},
1779 {uniqueId: 4, value:"bit$Bite"},
1780 {uniqueId: 5, value:"ouagadogou"},
1781 {uniqueId: 6, value:"jfq4@#!$!@|f1.$4r14i5u"},
1782 {uniqueId: 7, value:"BaB{aMa|SaRa***F}oo"},
1783 {uniqueId: 8, value:"squawl"},
1784 {uniqueId: 10, value:"seaweed"},
1785 {uniqueId: 12, value:"seaweed"},
1786 {uniqueId: 11, value:"zulu"},
1787 {uniqueId: 9, value:"seaweed"}
1792 var d = new doh.Deferred();
1793 function completed(items, request){
1794 var orderedArray0 = [7,2,4,3,1,6,5,12,10,9,8,11];
1795 var orderedArray1 = [ "BaB{aMa|SaRa***F}oo",
1800 "jfq4@#!$!@|f1.$4r14i5u",
1809 for(var i = 0; i < items.length; i++){
1810 var value = store.getValue(items[i], "value");
1811 if(!( (store.getValue(items[i], "uniqueId") === orderedArray0[i])&&
1812 (store.getValue(items[i], "value") === orderedArray1[i]))
1818 t.assertTrue(passed);
1822 d.errback(new Error("Unexpected sorting order found, sort failure."));
1826 function error(error, request){
1827 t.assertTrue(false);
1831 var sortAttributes = [{ attribute: "value"}, { attribute: "uniqueId", descending: true}];
1832 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1837 name: "Read API: fetch() sortMultipleSpecialComparator",
1838 runTest: function(datastore, t){
1840 // Function to test sorting on multiple attributes with a custom comparator.
1842 // Function to test sorting on multiple attributes with a custom comparator.
1844 var store = new datastore({data: { identifier: "uniqueId",
1845 items: [ {uniqueId: 1, status:"CLOSED"},
1846 {uniqueId: 2, status:"OPEN"},
1847 {uniqueId: 3, status:"PENDING"},
1848 {uniqueId: 4, status:"BLOCKED"},
1849 {uniqueId: 5, status:"CLOSED"},
1850 {uniqueId: 6, status:"OPEN"},
1851 {uniqueId: 7, status:"PENDING"},
1852 {uniqueId: 8, status:"PENDING"},
1853 {uniqueId: 10, status:"BLOCKED"},
1854 {uniqueId: 12, status:"BLOCKED"},
1855 {uniqueId: 11, status:"OPEN"},
1856 {uniqueId: 9, status:"CLOSED"}
1862 store.comparatorMap = {};
1863 store.comparatorMap["status"] = function(a,b) {
1865 // We want to map these by what the priority of these items are, not by alphabetical.
1866 // So, custom comparator.
1867 var enumMap = { OPEN: 3, BLOCKED: 2, PENDING: 1, CLOSED: 0};
1868 if (enumMap[a] > enumMap[b]) {
1871 if (enumMap[a] < enumMap[b]) {
1877 var sortAttributes = [{attribute: "status", descending: true}, { attribute: "uniqueId", descending: true}];
1879 var d = new doh.Deferred();
1880 function completed(items, findResult){
1881 var orderedArray = [11,6,2,12,10,4,8,7,3,9,5,1];
1883 for(var i = 0; i < items.length; i++){
1884 var value = store.getValue(items[i], "value");
1885 if(!(store.getValue(items[i], "uniqueId") === orderedArray[i])){
1890 t.assertTrue(passed);
1894 d.errback(new Error("Unexpected sorting order found, sort failure."));
1898 function error(errData, request){
1899 t.assertTrue(false);
1902 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1907 name: "Read API: fetch() sortAlphabeticWithUndefined",
1908 runTest: function(datastore, t){
1910 // Function to test sorting alphabetic ordering.
1912 // Function to test sorting alphabetic ordering.
1914 var store = new datastore({data: { identifier: "uniqueId",
1915 items: [ {uniqueId: 0, value:"abc"},
1916 {uniqueId: 1, value:"bca"},
1917 {uniqueId: 2, value:"abcd"},
1918 {uniqueId: 3, value:"abcdefg"},
1919 {uniqueId: 4, value:"lmnop"},
1920 {uniqueId: 5, value:"foghorn"},
1921 {uniqueId: 6, value:"qberty"},
1922 {uniqueId: 7, value:"qwerty"},
1923 {uniqueId: 8 }, //Deliberate undefined value
1924 {uniqueId: 9, value:"seaweed"},
1925 {uniqueId: 10, value:"123abc"}
1931 var d = new doh.Deferred();
1932 function completed(items, request){
1933 //Output should be in this order...
1934 var orderedArray = [10,0,2,3,1,5,4,6,7,9,8];
1935 t.assertEqual(items.length, 11);
1937 for(var i = 0; i < items.length; i++){
1938 if(!(store.getValue(items[i], "uniqueId") === orderedArray[i])){
1943 t.assertTrue(passed);
1947 d.errback(new Error("Unexpected sorting order found, sort failure."));
1951 function error(error, request) {
1952 t.assertTrue(false);
1956 var sortAttributes = [{attribute: "value"}];
1957 store.fetch({onComplete: completed, onError: error, sort: sortAttributes});
1962 name: "Read API: errorCondition_idCollision_inMemory",
1963 runTest: function(datastore, t){
1965 // Simple test of the errors thrown when there is an id collision in the data.
1966 // Added because of tracker: #2546
1968 // Simple test of the errors thrown when there is an id collision in the data.
1969 // Added because of tracker: #2546
1971 var store = new datastore({ data: { identifier: "uniqueId",
1972 items: [{uniqueId: 12345, value:"foo"},
1973 {uniqueId: 123456, value:"bar"},
1974 {uniqueId: 12345, value:"boom"},
1975 {uniqueId: 123457, value:"bit"}
1979 var d = new doh.Deferred();
1980 function onComplete(items, request){
1981 //This is bad if this fires, this case should fail and not call onComplete.
1982 t.assertTrue(false);
1986 function reportError(errData, request){
1987 //This is good if this fires, it is expected.
1991 store.fetch({onComplete: onComplete, onError: reportError});
1996 name: "Read API: errorCondition_idCollision_xhr",
1997 runTest: function(datastore, t){
1999 // Simple test of the errors thrown when there is an id collision in the data.
2000 // Added because of tracker: #2546
2002 // Simple test of the errors thrown when there is an id collision in the data.
2003 // Added because of tracker: #2546
2006 var store = new datastore({url: dojo.moduleUrl("tests", "data/countries_idcollision.json").toString() });
2007 var d = new doh.Deferred();
2008 function onComplete(items, request){
2009 //This is bad if this fires, this case should fail and not call onComplete.
2010 t.assertTrue(false);
2014 function reportError(errData, request){
2015 //This is good if this fires, it is expected.
2019 store.fetch({onComplete: onComplete, onError: reportError});
2025 name: "Read API: Date_datatype",
2026 runTest: function(datastore, t){
2027 //var store = new datastore(tests.data.readOnlyItemFileTestTemplates.testFile["countries_withDates"]);
2028 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries_withDates"));
2030 var d = new doh.Deferred();
2031 function onItem(item){
2032 t.assertTrue(item !== null);
2033 var independenceDate = store.getValue(item, "independence");
2034 t.assertTrue(independenceDate instanceof Date);
2035 //Check to see if the value was deserialized properly. Since the store stores in UTC/GMT, it
2036 //should also be compared in the UTC/GMT mode
2037 t.assertTrue(dojo.date.stamp.toISOString(independenceDate, {zulu:true}) === "1993-05-24T00:00:00Z");
2040 function onError(errData){
2041 t.assertTrue(false);
2044 store.fetchItemByIdentity({identity:"er", onItem:onItem, onError:onError});
2045 return d; // Deferred
2049 name: "Read API: custom_datatype_Color_SimpleMapping",
2050 runTest: function(datastore, t){
2052 // Function to test using literal values with custom datatypes
2056 { name:'Kermit', species:'frog', color:{_type:'Color', _value:'green'} },
2057 { name:'Beaker', hairColor:{_type:'Color', _value:'red'} }
2060 var store = new datastore({
2062 typeMap:{'Color': dojo.Color}
2064 var d = new doh.Deferred();
2065 function onItem(item){
2066 t.assertTrue(item !== null);
2068 var hairColor = store.getValue(beaker, "hairColor");
2069 t.assertTrue(hairColor instanceof dojo.Color);
2070 t.assertTrue(hairColor.toHex() == "#ff0000");
2073 function onError(errData){
2076 store.fetchItemByIdentity({identity:"Beaker", onItem:onItem, onError:onError});
2077 return d; // Deferred
2081 name: "Read API: custom_datatype_Color_GeneralMapping",
2082 runTest: function(datastore, t){
2084 // Function to test using literal values with custom datatypes
2088 { name:'Kermit', species:'frog', color:{_type:'Color', _value:'green'} },
2089 { name:'Beaker', hairColor:{_type:'Color', _value:'red'} }
2092 var store = new datastore({
2096 deserialize: function(value){
2097 return new dojo.Color(value);
2102 var d = new doh.Deferred();
2103 function onItem(item){
2104 t.assertTrue(item !== null);
2106 var hairColor = store.getValue(beaker, "hairColor");
2107 t.assertTrue(hairColor instanceof dojo.Color);
2108 t.assertTrue(hairColor.toHex() == "#ff0000");
2111 function onError(errData){
2114 store.fetchItemByIdentity({identity:"Beaker", onItem:onItem, onError:onError});
2115 return d; // Deferred
2119 name: "Read API: hierarchical_data",
2120 runTest: function(datastore, t){
2121 //var store = new datastore(tests.data.readOnlyItemFileTestTemplates.testFile["geography_hierarchy_small"]);
2122 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("geography_hierarchy_small"));
2123 var d = new doh.Deferred();
2124 function onComplete(items, request){
2125 t.assertEqual(items.length, 1);
2126 var northAmerica = items[0];
2127 var canada = store.getValue(northAmerica, "countries");
2128 var toronto = store.getValue(canada, "cities");
2129 t.assertEqual(store.getValue(canada, "name"), "Canada");
2130 t.assertEqual(store.getValue(toronto, "name"), "Toronto");
2133 function onError(errData){
2137 query: {name: "North America"},
2138 onComplete: onComplete,
2142 return d; // Deferred
2146 name: "Identity API: no_identifier_specified",
2147 runTest: function(datastore, t){
2148 var arrayOfItems = [
2149 {name:"Kermit", color:"green"},
2150 {name:"Miss Piggy", likes:"Kermit"},
2151 {name:"Beaker", hairColor:"red"}
2153 var store = new datastore({data:{items:arrayOfItems}});
2154 var d = new doh.Deferred();
2155 function onComplete(items, request){
2156 var features = store.getFeatures();
2157 var hasIdentityFeature = Boolean(features['dojo.data.api.Identity']);
2158 t.assertTrue(hasIdentityFeature);
2159 for(var i = 0; i < items.length; ++i){
2160 var item = items[i];
2161 var identifier = store.getIdentityAttributes(item);
2162 t.assertTrue(identifier === null);
2163 var identity = store.getIdentity(item);
2164 t.assertTrue(typeof identity == "number");
2168 function reportError(errData, request){
2171 store.fetch({onComplete: onComplete, onError: reportError});
2172 return d; // Deferred
2176 name: "Identity API: hierarchical_data",
2177 runTest: function(datastore, t){
2178 var store = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("geography_hierarchy_small"));
2179 var d = new doh.Deferred();
2180 function onComplete(items, request){
2181 var features = store.getFeatures();
2182 var hasIdentityFeature = Boolean(features['dojo.data.api.Identity']);
2183 t.assertTrue(hasIdentityFeature);
2184 for(var i = 0; i < items.length; ++i){
2185 var item = items[i];
2186 var identifier = store.getIdentityAttributes(item);
2187 t.assertTrue(identifier === null);
2188 var identity = store.getIdentity(item);
2189 t.assertTrue(typeof identity == "number");
2193 function reportError(errData, request){
2196 store.fetch({onComplete: onComplete, onError: reportError});
2197 return d; // Deferred
2201 name: "Read API: functionConformance",
2202 runTest: function(datastore, t){
2204 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
2206 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
2207 var testStore = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
2208 var readApi = new dojo.data.api.Read();
2212 if(i.toString().charAt(0) !== '_')
2214 var member = readApi[i];
2215 //Check that all the 'Read' defined functions exist on the test store.
2216 if(typeof member === "function"){
2217 var testStoreMember = testStore[i];
2218 if(!(typeof testStoreMember === "function")){
2225 t.assertTrue(passed);
2229 name: "Identity API: functionConformance",
2230 runTest: function(datastore, t){
2232 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
2234 // Simple test identity API conformance. Checks to see all declared functions are actual functions on the instances.
2235 var testStore = new datastore(tests.data.readOnlyItemFileTestTemplates.getTestData("countries"));
2236 var identityApi = new dojo.data.api.Identity();
2239 for(i in identityApi){
2241 if(i.toString().charAt(0) !== '_')
2243 var member = identityApi[i];
2244 //Check that all the 'Read' defined functions exist on the test store.
2245 if(typeof member === "function"){
2246 var testStoreMember = testStore[i];
2247 if(!(typeof testStoreMember === "function")){
2254 t.assertTrue(passed);