1 | dojo.provide("dojox.json.tests.query"); |
---|
2 | dojo.require("dojox.json.query"); |
---|
3 | |
---|
4 | |
---|
5 | dojox.json.tests.error = function(t, d, errData){ |
---|
6 | // summary: |
---|
7 | // The error callback function to be used for all of the tests. |
---|
8 | d.errback(errData); |
---|
9 | }; |
---|
10 | |
---|
11 | dojox.json.tests.testData= { |
---|
12 | store: { |
---|
13 | "book": [ |
---|
14 | { |
---|
15 | "category":"reference", |
---|
16 | "author":"Nigel Rees", |
---|
17 | "title":"Sayings of the Century", |
---|
18 | "price":8.95 |
---|
19 | }, |
---|
20 | { |
---|
21 | "category":"fiction", |
---|
22 | "author":"Evelyn Waugh", |
---|
23 | "title":"Sword of Honour", |
---|
24 | "price":12.99 |
---|
25 | }, |
---|
26 | { |
---|
27 | "category":"fiction", |
---|
28 | "author":"Herman Melville", |
---|
29 | "title":"Moby Dick", |
---|
30 | "isbn":"0-553-21311-3", |
---|
31 | "price":8.99 |
---|
32 | }, |
---|
33 | { |
---|
34 | "category":"fiction", |
---|
35 | "author":"J. R. R. Tolkien", |
---|
36 | "title":"The Lord of the\nRings", |
---|
37 | "isbn":"0-395-19395-8", |
---|
38 | "price":22.99 |
---|
39 | } |
---|
40 | ], |
---|
41 | "bicycle": { |
---|
42 | "color":"red", |
---|
43 | "price":19.95 |
---|
44 | } |
---|
45 | }, |
---|
46 | "symbols":{"@.$;":5} |
---|
47 | }; |
---|
48 | |
---|
49 | doh.register("dojox.json.tests.query", |
---|
50 | [ |
---|
51 | { |
---|
52 | name: "$.store.book[=author]", |
---|
53 | runTest: function(t) { |
---|
54 | var result = dojox.json.query(this.name,dojox.json.tests.testData); |
---|
55 | console.log("result",result); |
---|
56 | result = dojo.toJson(result); |
---|
57 | var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]'; |
---|
58 | doh.assertEqual(success,result); |
---|
59 | } |
---|
60 | }, |
---|
61 | { |
---|
62 | name: "$..author", |
---|
63 | runTest: function(t) { |
---|
64 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
65 | var success = '["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]'; |
---|
66 | doh.assertEqual(success,result); |
---|
67 | |
---|
68 | } |
---|
69 | }, |
---|
70 | { |
---|
71 | name: "$.store.*", |
---|
72 | runTest: function(t) { |
---|
73 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
74 | var success = '[[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95}]'; |
---|
75 | doh.assertEqual(success,result); |
---|
76 | } |
---|
77 | }, |
---|
78 | { |
---|
79 | name: "$.store..price", |
---|
80 | runTest: function(t) { |
---|
81 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
82 | var success = '[8.95,12.99,8.99,22.99,19.95]'; |
---|
83 | doh.assertEqual(success,result); |
---|
84 | } |
---|
85 | }, |
---|
86 | { |
---|
87 | name: "$..book[0]?price=22.99", |
---|
88 | runTest: function(t) { |
---|
89 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
90 | var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
91 | doh.assertEqual(success,result); |
---|
92 | } |
---|
93 | }, |
---|
94 | { |
---|
95 | name: "$..book[0]?price>=20", |
---|
96 | runTest: function(t) { |
---|
97 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
98 | var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
99 | doh.assertEqual(success,result); |
---|
100 | } |
---|
101 | }, |
---|
102 | { |
---|
103 | name: "$..book[0][-1:]", |
---|
104 | runTest: function(t) { |
---|
105 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
106 | var success = '[{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
107 | doh.assertEqual(success,result); |
---|
108 | } |
---|
109 | }, |
---|
110 | { |
---|
111 | name: "$..book[0][0,1]", |
---|
112 | runTest: function(t) { |
---|
113 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
114 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]'; |
---|
115 | doh.assertEqual(success,result); |
---|
116 | } |
---|
117 | }, |
---|
118 | { |
---|
119 | name: "$..book[0][:2]", |
---|
120 | runTest: function(t) { |
---|
121 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
122 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}]'; |
---|
123 | doh.assertEqual(success,result); |
---|
124 | } |
---|
125 | }, |
---|
126 | { |
---|
127 | name: "$.store.book[=category][^?true]", |
---|
128 | runTest: function(t) { |
---|
129 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
130 | var success = '["reference","fiction"]'; |
---|
131 | doh.assertEqual(success,result); |
---|
132 | } |
---|
133 | }, |
---|
134 | { |
---|
135 | name: "$..[^?author~'herman melville']", |
---|
136 | runTest: function(t) { |
---|
137 | var result = dojo.toJson(dojox.json.query(this.name,[dojox.json.tests.testData,dojox.json.tests.testData])); |
---|
138 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
139 | doh.assertEqual(success,result); |
---|
140 | } |
---|
141 | }, |
---|
142 | { |
---|
143 | name: "$..[^?author='Herman*']", |
---|
144 | runTest: function(t) { |
---|
145 | var result = dojo.toJson(dojox.json.query(this.name,[dojox.json.tests.testData,dojox.json.tests.testData])); |
---|
146 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
147 | doh.assertEqual(success,result); |
---|
148 | } |
---|
149 | }, |
---|
150 | { |
---|
151 | name: "$..[^?@['author']='Herman*']", |
---|
152 | runTest: function(t) { |
---|
153 | var result = dojo.toJson(dojox.json.query(this.name,[dojox.json.tests.testData,dojox.json.tests.testData])); |
---|
154 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
155 | doh.assertEqual(success,result); |
---|
156 | } |
---|
157 | }, |
---|
158 | { |
---|
159 | name: "$..book[0][?(@.isbn)]", |
---|
160 | runTest: function(t) { |
---|
161 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
162 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
163 | doh.assertEqual(success,result); |
---|
164 | } |
---|
165 | }, |
---|
166 | { |
---|
167 | name: "$..book[0][?(@.price<10)]", |
---|
168 | runTest: function(t) { |
---|
169 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
170 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
171 | doh.assertEqual(success,result); |
---|
172 | } |
---|
173 | }, |
---|
174 | { |
---|
175 | name: "$..book[0]?author=$1&price=$2", |
---|
176 | runTest: function(t) { |
---|
177 | var query = dojox.json.query(this.name); |
---|
178 | var result = dojo.toJson(query(dojox.json.tests.testData,"Nigel Rees",8.95)); |
---|
179 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95}]'; |
---|
180 | doh.assertEqual(success,result); |
---|
181 | } |
---|
182 | }, |
---|
183 | { |
---|
184 | name: "$..book[0]?author=$1&price=$2", |
---|
185 | runTest: function(t) { |
---|
186 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData,"Herman Melville",8.99)); |
---|
187 | var success = '[{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
188 | doh.assertEqual(success,result); |
---|
189 | } |
---|
190 | }, |
---|
191 | { |
---|
192 | name: "$..book[0][?(@['price']<10)]", |
---|
193 | runTest: function(t) { |
---|
194 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
195 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
196 | doh.assertEqual(success,result); |
---|
197 | } |
---|
198 | }, |
---|
199 | { |
---|
200 | name: "$..[?price<10]", |
---|
201 | runTest: function(t) { |
---|
202 | var query = dojox.json.query(this.name); |
---|
203 | console.log("recursive object search",query.toString()); |
---|
204 | var result = dojo.toJson(query(dojox.json.tests.testData)); |
---|
205 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
206 | doh.assertEqual(success,result); |
---|
207 | } |
---|
208 | }, |
---|
209 | { |
---|
210 | name: "$.store..[?price<10]", |
---|
211 | runTest: function(t) { |
---|
212 | var query = dojox.json.query(this.name); |
---|
213 | console.log("recursive object search",query.toString()); |
---|
214 | var result = dojo.toJson(query(dojox.json.tests.testData)); |
---|
215 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
216 | doh.assertEqual(success,result); |
---|
217 | } |
---|
218 | }, |
---|
219 | { |
---|
220 | name: "$.store.book[/category][/price][=price]", |
---|
221 | runTest: function(t) { |
---|
222 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
223 | var success = '[8.95,8.99,12.99,22.99]'; |
---|
224 | doh.assertEqual(success,result); |
---|
225 | } |
---|
226 | }, |
---|
227 | { |
---|
228 | name: "$.store.book[\\category,\\price][=price]", |
---|
229 | runTest: function(t) { |
---|
230 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
231 | var success = '[8.95,22.99,12.99,8.99]'; |
---|
232 | doh.assertEqual(success,result); |
---|
233 | } |
---|
234 | }, |
---|
235 | { |
---|
236 | name: "$.store.book?title='*of the*'", |
---|
237 | runTest: function(t) { |
---|
238 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
239 | var success = '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the\\nRings","isbn":"0-395-19395-8","price":22.99}]'; |
---|
240 | doh.assertEqual(success,result); |
---|
241 | } |
---|
242 | }, |
---|
243 | { |
---|
244 | name: "$.store.book[?'?iction'=category][=price]", |
---|
245 | runTest: function(t) { |
---|
246 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
247 | var success = '[12.99,8.99,22.99]'; |
---|
248 | doh.assertEqual(success,result); |
---|
249 | } |
---|
250 | }, |
---|
251 | { |
---|
252 | name: "$.store.book[?'?ICTion'~category][=price]", |
---|
253 | runTest: function(t) { |
---|
254 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
255 | var success = '[12.99,8.99,22.99]'; |
---|
256 | doh.assertEqual(success,result); |
---|
257 | } |
---|
258 | }, |
---|
259 | { |
---|
260 | name: "$.store.book[\\price][0].price - $.store.book[/price][0].price", |
---|
261 | runTest: function(t) { |
---|
262 | var result = dojox.json.query(this.name,dojox.json.tests.testData); |
---|
263 | var success = 14; |
---|
264 | doh.assertEqual(success,Math.round(result)); |
---|
265 | } |
---|
266 | }, |
---|
267 | { |
---|
268 | name: "$.symbols[*]", |
---|
269 | runTest: function(t) { |
---|
270 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
271 | var success = '[5]'; |
---|
272 | doh.assertEqual(success,result); |
---|
273 | } |
---|
274 | }, |
---|
275 | { |
---|
276 | name: "$.symbols['@.$;']", |
---|
277 | runTest: function(t) { |
---|
278 | var result = dojo.toJson(dojox.json.query(this.name,dojox.json.tests.testData)); |
---|
279 | var success = '5'; |
---|
280 | doh.assertEqual(success,result); |
---|
281 | } |
---|
282 | }, |
---|
283 | { |
---|
284 | name: "$.store.book[?(@.price<15)][1:3]", |
---|
285 | runTest: function(t) { |
---|
286 | var result = dojo.toJson(dojox.json.query("$.store.book[?(@.price<15)][1:3]",dojox.json.tests.testData)); |
---|
287 | var success = '[{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99}]'; |
---|
288 | doh.assertEqual(success,result); |
---|
289 | } |
---|
290 | }, |
---|
291 | { |
---|
292 | name: "$.store.book[?(@.price<15)][=author]", |
---|
293 | runTest: function(t) { |
---|
294 | var result = dojo.toJson(dojox.json.query("$.store.book[?(@.price<15)][=author]",dojox.json.tests.testData)); |
---|
295 | var success = '["Nigel Rees","Evelyn Waugh","Herman Melville"]'; |
---|
296 | doh.assertEqual(success,result); |
---|
297 | } |
---|
298 | }, |
---|
299 | { |
---|
300 | name: "$.store.book[1].category", |
---|
301 | runTest: function(t) { |
---|
302 | var result = dojo.toJson(dojox.json.query("$.store.book[1].category",dojox.json.tests.testData)); |
---|
303 | var success = '"fiction"'; |
---|
304 | doh.assertEqual(success,result); |
---|
305 | } |
---|
306 | }, |
---|
307 | { |
---|
308 | name: "test $.store.bicycle", |
---|
309 | runTest: function(t) { |
---|
310 | var result = dojo.toJson(dojox.json.query("$.store.bicycle",dojox.json.tests.testData)); |
---|
311 | var success = '{"color":"red","price":19.95}'; |
---|
312 | doh.assertEqual(success,result); |
---|
313 | } |
---|
314 | }, |
---|
315 | { |
---|
316 | name: "test $.store.book[=category]", |
---|
317 | runTest: function(t) { |
---|
318 | var result = dojo.toJson(dojox.json.query("$.store.book[=category]",dojox.json.tests.testData)); |
---|
319 | var success = '["reference","fiction","fiction","fiction"]'; |
---|
320 | doh.assertEqual(success,result); |
---|
321 | } |
---|
322 | }, |
---|
323 | { |
---|
324 | name: "safeEval: Illegal Eval", |
---|
325 | runTest: function(t) { |
---|
326 | try { |
---|
327 | var result = dojo.toJson(dojox.json.query("$.store.book[?(push(5))]",dojox.json.tests.testData)); |
---|
328 | console.log("Illegal eval permitted"); |
---|
329 | doh.e("Illegal eval was permitted"); |
---|
330 | } catch(e) { |
---|
331 | console.log("Eval properly blocked", e); |
---|
332 | } |
---|
333 | } |
---|
334 | }, |
---|
335 | { |
---|
336 | name: "safeEval: Illegal Eval 2", |
---|
337 | runTest: function(t) { |
---|
338 | try { |
---|
339 | var result = dojo.toJson(dojox.json.query("$.store.book[?(new Danger)]",dojox.json.tests.testData )); |
---|
340 | console.log("Illegal eval permitted"); |
---|
341 | doh.e("Illegal eval was permitted"); |
---|
342 | } catch(e) { |
---|
343 | console.log("Eval properly blocked", e); |
---|
344 | } |
---|
345 | } |
---|
346 | }, |
---|
347 | { |
---|
348 | name: "safeEval: Illegal Eval 3", |
---|
349 | runTest: function(t) { |
---|
350 | try { |
---|
351 | var result = dojo.toJson(dojox.json.query("$.store.book[?(@+=2)]",dojox.json.tests.testData)); |
---|
352 | console.log("Illegal eval permitted"); |
---|
353 | doh.e("Illegal eval was permitted"); |
---|
354 | } catch(e) { |
---|
355 | console.log("Eval properly blocked", e); |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | ] |
---|
361 | ); |
---|
362 | function performanceTest(){ |
---|
363 | dojo.require("dojox.jsonPath"); |
---|
364 | var data = []; |
---|
365 | for(var i = 0; i < 20000;i++){ |
---|
366 | data.push({foo:Math.random()}); |
---|
367 | } |
---|
368 | var now = new Date().getTime(); |
---|
369 | var results = dojox.jsonPath.query(data,"$[?(@.foo<0.01)]"); |
---|
370 | alert("JSONPath" + (new Date().getTime()-now) + " " + results.length); |
---|
371 | now = new Date().getTime(); |
---|
372 | results = dojox.json.query("$[?(@.foo<0.01)]",data); |
---|
373 | alert("JSONQuery" + (new Date().getTime()-now) + " " + results.length); |
---|
374 | } |
---|
375 | |
---|