1 if(!dojo._hasResource["dojox.data.tests.stores.QueryReadStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.data.tests.stores.QueryReadStore"] = true;
3 dojo.provide("dojox.data.tests.stores.QueryReadStore");
4 dojo.require("dojox.data.QueryReadStore");
5 dojo.require("dojo.data.api.Read");
7 //dojo.require("dojox.testing.DocTest");
9 dojox.data.tests.stores.QueryReadStore.getStore = function(){
10 return new dojox.data.QueryReadStore({
11 url: dojo.moduleUrl("dojox.data.tests", "stores/QueryReadStore.php").toString()
16 tests.register("dojox.data.tests.stores.QueryReadStore",
19 function testDocTests(t) {
21 // Run all the doc comments.
22 var doctest = new dojox.testing.DocTest();
23 doctest.run("dojox.data.QueryReadStore");
24 t.assertTrue(doctest.errors.length==0);
28 function testReadApi_getValue(t){
31 var store = dojox.data.tests.stores.QueryReadStore.getStore();
33 var d = new doh.Deferred();
34 function onComplete(items, request){
37 t.assertEqual("Alabama", store.getValue(item, "name"));
38 t.assertEqual("<img src='images/Alabama.jpg'/>Alabama", store.getValue(item, "label"));
39 t.assertEqual("AL", store.getValue(item, "abbreviation"));
40 // Test the defaultValue cases (the third paramter).
41 t.assertEqual("default value", store.getValue(item, "NAME", "default value"));
42 // TODO Test for null somehow ...
43 // Read api says: Returns null if and only if null was explicitly set as the attribute value.
45 // According to Read-API getValue() an exception is thrown when
46 // the item is not an item or when the attribute is not a string.
47 t.assertError(Error, store, "getValue", ["not an item", "NOT THERE"]);
48 t.assertError(Error, store, "getValue", [item, {}]);
52 store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
56 function testReadApi_getValues(t){
59 var store = dojox.data.tests.stores.QueryReadStore.getStore();
61 var d = new doh.Deferred();
62 function onComplete(items, request){
65 t.assertEqual(["Alabama"], store.getValues(item, "name"));
66 t.assertEqual(["<img src='images/Alabama.jpg'/>Alabama"], store.getValues(item, "label"));
67 t.assertEqual(["AL"], store.getValues(item, "abbreviation"));
68 // TODO Test for null somehow ...
69 // Read api says: Returns null if and only if null was explicitly set as the attribute value.
71 // Test for not-existing attributes without defaultValues and invalid items.
73 t.assertEqual([], store.getValues(item, "NOT THERE"));
74 var errThrown = false;
76 //Should throw an exception.
77 var values = store.getValues("not an item", "NOT THERE");
81 t.assertTrue(errThrown);
84 store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
88 function testReadApi_getAttributes(t){
91 var store = dojox.data.tests.stores.QueryReadStore.getStore();
93 var d = new doh.Deferred();
94 function onComplete(items, request){
97 t.assertEqual(['id', 'name', 'label', 'abbreviation', 'capital'], store.getAttributes(item));
98 t.assertError(Error, store, "getAttributes", [{}]);
102 store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
106 function testReadApi_getLabel(t){
107 var store = dojox.data.tests.stores.QueryReadStore.getStore();
108 var d = new doh.Deferred();
109 function onComplete(items, request){
112 t.assertEqual(["<img src='images/Alabama.jpg'/>Alabama"], store.getLabel(item));
115 store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
119 function testReadApi_hasAttribute(t){
122 var store = dojox.data.tests.stores.QueryReadStore.getStore();
124 var d = new doh.Deferred();
125 function onComplete(items, request){
127 // The positive cases.
128 t.assertEqual(true, store.hasAttribute(item, "name"));
129 t.assertEqual(true, store.hasAttribute(item, "label"));
130 t.assertEqual(true, store.hasAttribute(item, "abbreviation"));
131 // Make sure attribute case doesnt matter.
132 t.assertEqual(false, store.hasAttribute(item, "NAME"));
133 t.assertEqual(false, store.hasAttribute(item, "Name"));
134 t.assertEqual(false, store.hasAttribute(item, "Label"));
135 // Pass in an invalid item.
136 t.assertEqual(false, store.hasAttribute({}, "abbreviation"));
137 // pass in something that looks like the item with the attribute.
138 t.assertEqual(false, store.hasAttribute({name:"yo"}, "name"));
142 store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
146 function testReadApi_containsValue(t){
149 var store = dojox.data.tests.stores.QueryReadStore.getStore();
151 var d = new doh.Deferred();
152 function onComplete(items, request){
154 t.assertTrue(store.containsValue(item, "name", "Alaska"));
157 store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
161 function testReadApi_isItem(t){
164 var store = dojox.data.tests.stores.QueryReadStore.getStore();
166 var d = new doh.Deferred();
167 function onComplete(items, request){
169 t.assertEqual(true, store.isItem(items[0]));
170 // Try a pure object.
171 t.assertEqual(false, store.isItem({}));
172 // Try to look like an item.
173 t.assertEqual(false, store.isItem({name:"Alaska", label:"Alaska", abbreviation:"AK"}));
176 store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
180 function testReadApi_isItemLoaded(t){
183 var store = dojox.data.tests.stores.QueryReadStore.getStore();
185 var d = new doh.Deferred();
186 function onComplete(items, request){
189 t.assertTrue(store.isItemLoaded(item));
193 store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
197 //function testReadApi_loadItem(t){
200 // t.assertTrue(false);
203 function testReadApi_fetch_all(t){
205 // Simple test of fetching all items.
207 // Simple test of fetching all items.
208 var store = dojox.data.tests.stores.QueryReadStore.getStore();
210 var d = new doh.Deferred();
211 function onComplete(items, request) {
212 t.assertEqual(8, items.length);
215 function onError(error, request) {
218 store.fetch({query:{q:"m"}, onComplete: onComplete, onError: onError});
222 function testReadApi_fetch_onBegin(t){
224 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
226 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
227 var store = dojox.data.tests.stores.QueryReadStore.getStore();
229 var d = new doh.Deferred();
231 function onBegin(size, request){
232 t.assertEqual(8, size);
235 function onComplete(items, request) {
236 t.assertEqual(5, items.length);
240 d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
243 function onError(error, request) {
246 store.fetch({query:{q:"m"}, start: 0, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
250 function testReadApi_fetch_onBegin_ServersidePaging(t){
252 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
254 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
255 var store = dojox.data.tests.stores.QueryReadStore.getStore();
257 var d = new doh.Deferred();
259 function onBegin(size, request){
260 t.assertEqual(8, size);
263 function onComplete(items, request) {
264 t.assertEqual(3, items.length);
268 d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
271 function onError(error, request) {
274 store.fetch({query:{q:"m"}, start: 5, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
278 function testReadApi_fetch_onBegin_ClientsidePaging(t){
280 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
282 // Simple test of fetching items, checking that onBegin size is all items matched, and page is just the items asked for.
283 var store = dojox.data.tests.stores.QueryReadStore.getStore();
284 store.doClientPaging = true;
286 var d = new doh.Deferred();
288 function onBegin(size, request){
289 t.assertEqual(8, size);
292 function onComplete(items, request) {
293 t.assertEqual(5, items.length);
297 d.errback(new Error("Store did not return proper number of rows, regardless of page size"));
300 function onError(error, request) {
303 store.fetch({query:{q:"m"}, start: 0, count: 5, onBegin: onBegin, onComplete: onComplete, onError: onError});
307 function testReadApi_fetch_one(t){
310 var store = dojox.data.tests.stores.QueryReadStore.getStore();
312 var d = new doh.Deferred();
313 function onComplete(items, request){
314 t.assertEqual(1, items.length);
317 function onError(error, request) {
320 store.fetch({query:{q:"Alaska"}, onComplete: onComplete, onError: onError});
324 function testReadApi_fetch_client_paging(t){
326 // Lets test that paging on the same request does not trigger
329 var store = dojox.data.tests.stores.QueryReadStore.getStore();
330 store.doClientPaging = true;
332 var lastRequestHash = null;
334 var d = new doh.Deferred();
335 function onComplete(items, request) {
336 t.assertEqual(5, items.length);
337 lastRequestHash = store.lastRequestHash;
340 // Do the next request AFTER the previous one, so we are sure its sequential.
341 // We need to be sure so we can compare to the data from the first request.
342 function onComplete1(items, request) {
343 t.assertEqual(5, items.length);
344 t.assertEqual(lastRequestHash, store.lastRequestHash);
345 t.assertEqual(firstItems[1], items[0]);
349 req.onComplete = onComplete1;
352 function onError(error, request) {
355 var req = {query:{q:"m"}, start:0, count:5,
356 onComplete: onComplete, onError: onError};
361 function testReadApi_fetch_server_paging(t) {
362 // Verify that the paging on the server side does work.
363 // This is the test for http://trac.dojotoolkit.org/ticket/4761
365 // How? We request 10 items from the server, start=0, count=10.
366 // The second request requests 5 items: start=5, count=5 and those
367 // 5 items should have the same values as the last 5 of the first
369 // This tests if the server side paging does work.
370 var store = dojox.data.tests.stores.QueryReadStore.getStore();
372 var lastRequestHash = null;
373 var d = new doh.Deferred();
374 function onComplete(items, request) {
375 t.assertEqual(10, items.length);
376 lastRequestHash = store.lastRequestHash;
379 // Do the next request AFTER the previous one, so we are sure its sequential.
380 // We need to be sure so we can compare to the data from the first request.
381 function onComplete1(items, request) {
382 t.assertEqual(5, items.length);
383 // Compare the hash of the last request, they must be different,
384 // since another server request was issued.
385 t.assertTrue(lastRequestHash!=store.lastRequestHash);
386 t.assertEqual(store.getValue(firstItems[5], "name"), store.getValue(items[0], "name"));
387 t.assertEqual(store.getValue(firstItems[6], "name"), store.getValue(items[1], "name"));
388 t.assertEqual(store.getValue(firstItems[7], "name"), store.getValue(items[2], "name"));
389 t.assertEqual(store.getValue(firstItems[8], "name"), store.getValue(items[3], "name"));
390 t.assertEqual(store.getValue(firstItems[9], "name"), store.getValue(items[4], "name"));
393 // Init a new store, or it will use the old data, since the query has not changed.
394 store.doClientPaging = false;
395 store.fetch({start:5, count:5, onComplete: onComplete1, onError: onError});
397 function onError(error, request) {
400 store.fetch({query:{}, start:0, count:10, onComplete: onComplete, onError: onError});
404 function testReadApi_getFeatures(t) {
405 var store = dojox.data.tests.stores.QueryReadStore.getStore();
406 var features = store.getFeatures();
407 t.assertTrue(features["dojo.data.api.Read"]);
408 t.assertTrue(features["dojo.data.api.Identity"]);
413 t.assertEqual(2, count);
415 function testReadAPI_functionConformance(t){
417 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
419 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
421 var testStore = dojox.data.tests.stores.QueryReadStore.getStore();
422 var readApi = new dojo.data.api.Read();
426 var member = readApi[i];
427 //Check that all the 'Read' defined functions exist on the test store.
428 if(typeof member === "function"){
429 var testStoreMember = testStore[i];
430 if(!(typeof testStoreMember === "function")){
431 console.log("Problem with function: [" + i + "]");
437 t.assertTrue(passed);