2150 |
mathias |
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");
|
|
|
6 |
|
|
|
7 |
//dojo.require("dojox.testing.DocTest");
|
|
|
8 |
|
|
|
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(),
|
|
|
12 |
doClientPaging:true // "true" is actually also the default, but make sure :-).
|
|
|
13 |
});
|
|
|
14 |
};
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
tests.register("dojox.data.tests.stores.QueryReadStore",
|
|
|
18 |
[
|
|
|
19 |
/*
|
|
|
20 |
function testDocTests(t) {
|
|
|
21 |
// summary:
|
|
|
22 |
// Run all the doc comments.
|
|
|
23 |
var doctest = new dojox.testing.DocTest();
|
|
|
24 |
doctest.run("dojox.data.QueryReadStore");
|
|
|
25 |
t.assertTrue(doctest.errors.length==0);
|
|
|
26 |
},
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
function testReadApi_getValue(t){
|
|
|
30 |
// summary:
|
|
|
31 |
// description:
|
|
|
32 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
33 |
|
|
|
34 |
var d = new doh.Deferred();
|
|
|
35 |
function onComplete(items, request){
|
|
|
36 |
var item = items[0];
|
|
|
37 |
// The good cases.
|
|
|
38 |
t.assertEqual("Alabama", store.getValue(item, "name"));
|
|
|
39 |
t.assertEqual("<img src='images/Alabama.jpg'/>Alabama", store.getValue(item, "label"));
|
|
|
40 |
t.assertEqual("AL", store.getValue(item, "abbreviation"));
|
|
|
41 |
// Test the defaultValue cases (the third paramter).
|
|
|
42 |
t.assertEqual("default value", store.getValue(item, "NAME", "default value"));
|
|
|
43 |
// TODO Test for null somehow ...
|
|
|
44 |
// Read api says: Returns null if and only if null was explicitly set as the attribute value.
|
|
|
45 |
|
|
|
46 |
// According to Read-API getValue() an exception is thrown when
|
|
|
47 |
// the item is not an item or when the attribute is not a string.
|
|
|
48 |
t.assertError(Error, store, "getValue", ["not an item", "NOT THERE"]);
|
|
|
49 |
t.assertError(Error, store, "getValue", [item, {}]);
|
|
|
50 |
|
|
|
51 |
d.callback(true);
|
|
|
52 |
}
|
|
|
53 |
store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
|
|
|
54 |
return d; //Object
|
|
|
55 |
},
|
|
|
56 |
|
|
|
57 |
function testReadApi_getValues(t){
|
|
|
58 |
// summary:
|
|
|
59 |
// description:
|
|
|
60 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
61 |
|
|
|
62 |
var d = new doh.Deferred();
|
|
|
63 |
function onComplete(items, request){
|
|
|
64 |
var item = items[0];
|
|
|
65 |
// The good cases.
|
|
|
66 |
t.assertEqual(["Alabama"], store.getValues(item, "name"));
|
|
|
67 |
t.assertEqual(["<img src='images/Alabama.jpg'/>Alabama"], store.getValues(item, "label"));
|
|
|
68 |
t.assertEqual(["AL"], store.getValues(item, "abbreviation"));
|
|
|
69 |
// TODO Test for null somehow ...
|
|
|
70 |
// Read api says: Returns null if and only if null was explicitly set as the attribute value.
|
|
|
71 |
|
|
|
72 |
// Test for not-existing attributes without defaultValues and invalid items.
|
|
|
73 |
// TODO
|
|
|
74 |
//dojox.data.tests.stores.QueryReadStore.assertError(dojox.data.QueryReadStore.InvalidAttributeError, store, "getValues", [item, "NOT THERE"]);
|
|
|
75 |
//dojox.data.tests.stores.QueryReadStore.assertError(dojox.data.QueryReadStore.InvalidItemError, store, "getValues", ["not an item", "NOT THERE"]);
|
|
|
76 |
|
|
|
77 |
d.callback(true);
|
|
|
78 |
}
|
|
|
79 |
store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
|
|
|
80 |
return d; //Object
|
|
|
81 |
},
|
|
|
82 |
|
|
|
83 |
function testReadApi_getAttributes(t){
|
|
|
84 |
// summary:
|
|
|
85 |
// description:
|
|
|
86 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
87 |
|
|
|
88 |
var d = new doh.Deferred();
|
|
|
89 |
function onComplete(items, request){
|
|
|
90 |
var item = items[0];
|
|
|
91 |
// The good case(s).
|
|
|
92 |
t.assertEqual(['name', 'label', 'abbreviation'], store.getAttributes(item));
|
|
|
93 |
t.assertError(dojox.data.QueryReadStore.InvalidItemError, store, "getAttributes", [{}]);
|
|
|
94 |
|
|
|
95 |
d.callback(true);
|
|
|
96 |
}
|
|
|
97 |
store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
|
|
|
98 |
return d; //Object
|
|
|
99 |
},
|
|
|
100 |
|
|
|
101 |
function testReadApi_hasAttribute(t){
|
|
|
102 |
// summary:
|
|
|
103 |
// description:
|
|
|
104 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
105 |
|
|
|
106 |
var d = new doh.Deferred();
|
|
|
107 |
function onComplete(items, request){
|
|
|
108 |
var item = items[0];
|
|
|
109 |
// The positive cases.
|
|
|
110 |
t.assertEqual(true, store.hasAttribute(item, "name"));
|
|
|
111 |
t.assertEqual(true, store.hasAttribute(item, "label"));
|
|
|
112 |
t.assertEqual(true, store.hasAttribute(item, "abbreviation"));
|
|
|
113 |
// Make sure attribute case doesnt matter.
|
|
|
114 |
t.assertEqual(false, store.hasAttribute(item, "NAME"));
|
|
|
115 |
t.assertEqual(false, store.hasAttribute(item, "Name"));
|
|
|
116 |
t.assertEqual(false, store.hasAttribute(item, "Label"));
|
|
|
117 |
// Pass in an invalid item.
|
|
|
118 |
t.assertEqual(false, store.hasAttribute({}, "abbreviation"));
|
|
|
119 |
// pass in something that looks like the item with the attribute.
|
|
|
120 |
t.assertEqual(false, store.hasAttribute({name:"yo"}, "name"));
|
|
|
121 |
|
|
|
122 |
d.callback(true);
|
|
|
123 |
}
|
|
|
124 |
store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
|
|
|
125 |
return d; //Object
|
|
|
126 |
},
|
|
|
127 |
|
|
|
128 |
function testReadApi_containsValue(t){
|
|
|
129 |
// summary:
|
|
|
130 |
// description:
|
|
|
131 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
132 |
|
|
|
133 |
var d = new doh.Deferred();
|
|
|
134 |
function onComplete(items, request){
|
|
|
135 |
var item = items[0];
|
|
|
136 |
t.assertTrue(store.containsValue(item, "name", "Alaska"));
|
|
|
137 |
d.callback(true);
|
|
|
138 |
}
|
|
|
139 |
store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
|
|
|
140 |
return d; //Object
|
|
|
141 |
},
|
|
|
142 |
|
|
|
143 |
function testReadApi_isItem(t){
|
|
|
144 |
// summary:
|
|
|
145 |
// description:
|
|
|
146 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
147 |
|
|
|
148 |
var d = new doh.Deferred();
|
|
|
149 |
function onComplete(items, request){
|
|
|
150 |
// The good case.
|
|
|
151 |
t.assertEqual(true, store.isItem(items[0]));
|
|
|
152 |
// Try a pure object.
|
|
|
153 |
t.assertEqual(false, store.isItem({}));
|
|
|
154 |
// Try to look like an item.
|
|
|
155 |
t.assertEqual(false, store.isItem({name:"Alaska", label:"Alaska", abbreviation:"AK"}));
|
|
|
156 |
d.callback(true);
|
|
|
157 |
}
|
|
|
158 |
store.fetch({query:{q:"Alaska"}, onComplete: onComplete});
|
|
|
159 |
return d; //Object
|
|
|
160 |
},
|
|
|
161 |
|
|
|
162 |
function testReadApi_isItemLoaded(t){
|
|
|
163 |
// summary:
|
|
|
164 |
// description:
|
|
|
165 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
166 |
|
|
|
167 |
var d = new doh.Deferred();
|
|
|
168 |
function onComplete(items, request){
|
|
|
169 |
var item = items[0];
|
|
|
170 |
// The good case(s).
|
|
|
171 |
t.assertTrue(store.isItemLoaded(item));
|
|
|
172 |
|
|
|
173 |
d.callback(true);
|
|
|
174 |
}
|
|
|
175 |
store.fetch({query:{q:"Alabama"}, onComplete: onComplete});
|
|
|
176 |
return d; //Object
|
|
|
177 |
},
|
|
|
178 |
|
|
|
179 |
//function testReadApi_loadItem(t){
|
|
|
180 |
// // summary:
|
|
|
181 |
// // description:
|
|
|
182 |
// t.assertTrue(false);
|
|
|
183 |
//},
|
|
|
184 |
|
|
|
185 |
function testReadApi_fetch_all(t){
|
|
|
186 |
// summary:
|
|
|
187 |
// Simple test of fetching all items.
|
|
|
188 |
// description:
|
|
|
189 |
// Simple test of fetching all items.
|
|
|
190 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
191 |
|
|
|
192 |
var d = new doh.Deferred();
|
|
|
193 |
function onComplete(items, request) {
|
|
|
194 |
t.assertEqual(8, items.length);
|
|
|
195 |
d.callback(true);
|
|
|
196 |
}
|
|
|
197 |
function onError(error, request) {
|
|
|
198 |
d.errback(error);
|
|
|
199 |
}
|
|
|
200 |
store.fetch({query:{q:"a"}, onComplete: onComplete, onError: onError});
|
|
|
201 |
return d; //Object
|
|
|
202 |
},
|
|
|
203 |
|
|
|
204 |
function testReadApi_fetch_one(t){
|
|
|
205 |
// summary:
|
|
|
206 |
// description:
|
|
|
207 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
208 |
|
|
|
209 |
var d = new doh.Deferred();
|
|
|
210 |
function onComplete(items, request){
|
|
|
211 |
t.assertEqual(1, items.length);
|
|
|
212 |
d.callback(true);
|
|
|
213 |
}
|
|
|
214 |
function onError(error, request) {
|
|
|
215 |
d.errback(error);
|
|
|
216 |
}
|
|
|
217 |
store.fetch({query:{q:"Alaska"}, onComplete: onComplete, onError: onError});
|
|
|
218 |
return d; //Object
|
|
|
219 |
},
|
|
|
220 |
|
|
|
221 |
function testReadApi_fetch_client_paging(t){
|
|
|
222 |
// summary:
|
|
|
223 |
// Lets test that paging on the same request does not trigger
|
|
|
224 |
// server requests.
|
|
|
225 |
// description:
|
|
|
226 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
227 |
|
|
|
228 |
var lastRequestHash = null;
|
|
|
229 |
var firstItems = [];
|
|
|
230 |
var d = new doh.Deferred();
|
|
|
231 |
function onComplete(items, request) {
|
|
|
232 |
t.assertEqual(5, items.length);
|
|
|
233 |
lastRequestHash = store.lastRequestHash;
|
|
|
234 |
firstItems = items;
|
|
|
235 |
|
|
|
236 |
// Do the next request AFTER the previous one, so we are sure its sequential.
|
|
|
237 |
// We need to be sure so we can compare to the data from the first request.
|
|
|
238 |
function onComplete1(items, request) {
|
|
|
239 |
t.assertEqual(5, items.length);
|
|
|
240 |
t.assertEqual(lastRequestHash, store.lastRequestHash);
|
|
|
241 |
t.assertEqual(firstItems[1], items[0]);
|
|
|
242 |
d.callback(true);
|
|
|
243 |
}
|
|
|
244 |
req.start = 1;
|
|
|
245 |
req.onComplete = onComplete1;
|
|
|
246 |
store.fetch(req);
|
|
|
247 |
}
|
|
|
248 |
function onError(error, request) {
|
|
|
249 |
d.errback(error);
|
|
|
250 |
}
|
|
|
251 |
var req = {query:{q:"a"}, start:0, count:5,
|
|
|
252 |
onComplete: onComplete, onError: onError};
|
|
|
253 |
store.fetch(req);
|
|
|
254 |
return d; //Object
|
|
|
255 |
},
|
|
|
256 |
|
|
|
257 |
function testReadApi_fetch_server_paging(t) {
|
|
|
258 |
// Verify that the paging on the server side does work.
|
|
|
259 |
// This is the test for http://trac.dojotoolkit.org/ticket/4761
|
|
|
260 |
//
|
|
|
261 |
// How? We request 10 items from the server, start=0, count=10.
|
|
|
262 |
// The second request requests 5 items: start=5, count=5 and those
|
|
|
263 |
// 5 items should have the same values as the last 5 of the first
|
|
|
264 |
// request.
|
|
|
265 |
// This tests if the server side paging does work.
|
|
|
266 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
267 |
|
|
|
268 |
var lastRequestHash = null;
|
|
|
269 |
var d = new doh.Deferred();
|
|
|
270 |
function onComplete(items, request) {
|
|
|
271 |
t.assertEqual(10, items.length);
|
|
|
272 |
lastRequestHash = store.lastRequestHash;
|
|
|
273 |
firstItems = items;
|
|
|
274 |
|
|
|
275 |
// Do the next request AFTER the previous one, so we are sure its sequential.
|
|
|
276 |
// We need to be sure so we can compare to the data from the first request.
|
|
|
277 |
function onComplete1(items, request) {
|
|
|
278 |
t.assertEqual(5, items.length);
|
|
|
279 |
// Compare the hash of the last request, they must be different,
|
|
|
280 |
// since another server request was issued.
|
|
|
281 |
t.assertTrue(lastRequestHash!=store.lastRequestHash);
|
|
|
282 |
t.assertEqual(store.getValue(firstItems[5], "name"), store.getValue(items[0], "name"));
|
|
|
283 |
t.assertEqual(store.getValue(firstItems[6], "name"), store.getValue(items[1], "name"));
|
|
|
284 |
t.assertEqual(store.getValue(firstItems[7], "name"), store.getValue(items[2], "name"));
|
|
|
285 |
t.assertEqual(store.getValue(firstItems[8], "name"), store.getValue(items[3], "name"));
|
|
|
286 |
t.assertEqual(store.getValue(firstItems[9], "name"), store.getValue(items[4], "name"));
|
|
|
287 |
d.callback(true);
|
|
|
288 |
}
|
|
|
289 |
// Init a new store, or it will use the old data, since the query has not changed.
|
|
|
290 |
store.doClientPaging = false;
|
|
|
291 |
store.fetch({start:5, count:5, onComplete: onComplete1, onError: onError});
|
|
|
292 |
}
|
|
|
293 |
function onError(error, request) {
|
|
|
294 |
d.errback(error);
|
|
|
295 |
}
|
|
|
296 |
store.fetch({query:{}, start:0, count:10, onComplete: onComplete, onError: onError});
|
|
|
297 |
return d; //Object
|
|
|
298 |
},
|
|
|
299 |
|
|
|
300 |
function testReadApi_getFeatures(t) {
|
|
|
301 |
var store = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
302 |
var features = store.getFeatures();
|
|
|
303 |
t.assertTrue(features["dojo.data.api.Read"]);
|
|
|
304 |
t.assertTrue(features["dojo.data.api.Identity"]);
|
|
|
305 |
var count = 0;
|
|
|
306 |
for (i in features){
|
|
|
307 |
count++;
|
|
|
308 |
}
|
|
|
309 |
t.assertEqual(2, count);
|
|
|
310 |
},
|
|
|
311 |
function testReadAPI_functionConformance(t){
|
|
|
312 |
// summary:
|
|
|
313 |
// Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
|
|
|
314 |
// description:
|
|
|
315 |
// Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
|
|
|
316 |
|
|
|
317 |
var testStore = dojox.data.tests.stores.QueryReadStore.getStore();
|
|
|
318 |
var readApi = new dojo.data.api.Read();
|
|
|
319 |
var passed = true;
|
|
|
320 |
|
|
|
321 |
for(i in readApi){
|
|
|
322 |
var member = readApi[i];
|
|
|
323 |
//Check that all the 'Read' defined functions exist on the test store.
|
|
|
324 |
if(typeof member === "function"){
|
|
|
325 |
var testStoreMember = testStore[i];
|
|
|
326 |
if(!(typeof testStoreMember === "function")){
|
|
|
327 |
console.log("Problem with function: [" + i + "]");
|
|
|
328 |
passed = false;
|
|
|
329 |
break;
|
|
|
330 |
}
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
t.assertTrue(passed);
|
|
|
334 |
}
|
|
|
335 |
]
|
|
|
336 |
);
|
|
|
337 |
|
|
|
338 |
}
|