| 1 |
dojo.provide("dojox.data.tests.stores.JsonQueryRestStore"); |
| 2 |
dojo.require("dojox.data.ClientFilter"); |
| 3 |
dojo.require("dojox.data.JsonQueryRestStore"); |
| 4 |
|
| 5 |
dojox.data.tests.stores.JsonQueryRestStore.error = function(t, d, errData){ |
| 6 |
|
| 7 |
|
| 8 |
d.errback(errData); |
| 9 |
}; |
| 10 |
testService = function(query){ |
| 11 |
lastQuery = query; |
| 12 |
var deferred = new dojo.Deferred(); |
| 13 |
deferred.callback([ |
| 14 |
{id:1, name:"Ball", price: 5}, |
| 15 |
{id:2, name:"Car", price: 15}, |
| 16 |
{id:3, name:"Truck", price: 25}, |
| 17 |
{id:4, name:"Hula Hoop", price: 55} |
| 18 |
]); |
| 19 |
return deferred; |
| 20 |
|
| 21 |
}; |
| 22 |
testService.servicePath = "/testing"; |
| 23 |
jsonStore = new dojox.data.JsonQueryRestStore({service:testService}); |
| 24 |
|
| 25 |
doh.register("dojox.data.tests.stores.JsonQueryRestStore", |
| 26 |
[ |
| 27 |
{ |
| 28 |
name: "Fetch using a query object", |
| 29 |
timeout: 10000, |
| 30 |
runTest: function(t) { |
| 31 |
|
| 32 |
|
| 33 |
var d = new doh.Deferred(); |
| 34 |
jsonStore.fetch({query:{name:"Car"}, |
| 35 |
onComplete: function(items, request){ |
| 36 |
t.is(lastQuery,"[?(@.name='Car')]"); |
| 37 |
d.callback(true); |
| 38 |
}, |
| 39 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 40 |
}); |
| 41 |
return d; |
| 42 |
} |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "Fetch+Sorting using a query object", |
| 46 |
timeout: 10000, |
| 47 |
runTest: function(t) { |
| 48 |
|
| 49 |
|
| 50 |
var d = new doh.Deferred(); |
| 51 |
jsonStore.fetch({query:{name:"Car"}, |
| 52 |
sort:[{attribute:"price"}], |
| 53 |
onComplete: function(items, request){ |
| 54 |
t.is(lastQuery,"[?(@.name='Car')][/@['price']]"); |
| 55 |
d.callback(true); |
| 56 |
}, |
| 57 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 58 |
}); |
| 59 |
return d; |
| 60 |
} |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "Fetch all items (and cache for the next tests)", |
| 64 |
timeout: 10000, |
| 65 |
runTest: function(t) { |
| 66 |
|
| 67 |
|
| 68 |
var d = new doh.Deferred(); |
| 69 |
jsonStore.fetch({query:"", |
| 70 |
onComplete: function(items, request){ |
| 71 |
testItems = items; |
| 72 |
console.log(items.length, items); |
| 73 |
t.is(4, items.length); |
| 74 |
d.callback(true); |
| 75 |
}, |
| 76 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d), |
| 77 |
queryOptions:{cache:true} |
| 78 |
}); |
| 79 |
return d; |
| 80 |
} |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "Fetch using a query object", |
| 84 |
timeout: 10000, |
| 85 |
runTest: function(t) { |
| 86 |
|
| 87 |
|
| 88 |
var d = new doh.Deferred(); |
| 89 |
lastQuery = null; |
| 90 |
jsonStore.fetch({query:{name:"Car"}, |
| 91 |
onComplete: function(items, request){ |
| 92 |
t.is(1, items.length); |
| 93 |
t.is(lastQuery, null); |
| 94 |
d.callback(true); |
| 95 |
}, |
| 96 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 97 |
}); |
| 98 |
return d; |
| 99 |
} |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "Fetch using a JSONQuery", |
| 103 |
timeout: 10000, |
| 104 |
runTest: function(t) { |
| 105 |
|
| 106 |
|
| 107 |
var d = new doh.Deferred(); |
| 108 |
lastQuery = null; |
| 109 |
jsonStore.fetch({query:"?name='Car'", |
| 110 |
onComplete: function(items, request){ |
| 111 |
t.is(1, items.length); |
| 112 |
t.is(lastQuery, null); |
| 113 |
d.callback(true); |
| 114 |
}, |
| 115 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 116 |
}); |
| 117 |
return d; |
| 118 |
} |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "Fetch using a JSONQuery with operator", |
| 122 |
timeout: 10000, |
| 123 |
runTest: function(t) { |
| 124 |
|
| 125 |
|
| 126 |
var d = new doh.Deferred(); |
| 127 |
lastQuery = null; |
| 128 |
jsonStore.fetch({query:"[?price<20]", |
| 129 |
onComplete: function(items, request){ |
| 130 |
t.is(2, items.length); |
| 131 |
t.is(lastQuery, null); |
| 132 |
d.callback(true); |
| 133 |
}, |
| 134 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 135 |
}); |
| 136 |
return d; |
| 137 |
} |
| 138 |
}, |
| 139 |
{ |
| 140 |
name: "Fetch using a JSONQuery with operator and paging", |
| 141 |
timeout: 10000, |
| 142 |
runTest: function(t) { |
| 143 |
|
| 144 |
|
| 145 |
var d = new doh.Deferred(); |
| 146 |
lastQuery = null; |
| 147 |
jsonStore.fetch({query:"?price<20",start:1, count:1, |
| 148 |
onComplete: function(items, request){ |
| 149 |
t.is(1, items.length); |
| 150 |
t.is(lastQuery, null); |
| 151 |
d.callback(true); |
| 152 |
}, |
| 153 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d), |
| 154 |
queryOptions:{cache:true} |
| 155 |
}); |
| 156 |
return d; |
| 157 |
} |
| 158 |
}, |
| 159 |
{ |
| 160 |
name: "Sorting", |
| 161 |
timeout: 10000, |
| 162 |
runTest: function(t) { |
| 163 |
|
| 164 |
|
| 165 |
var d = new doh.Deferred(); |
| 166 |
lastQuery = null; |
| 167 |
jsonStore.fetch({sort:[{attribute:"name", descending: true}], |
| 168 |
onComplete: function(items, request){ |
| 169 |
t.is("Truck", items[0].name); |
| 170 |
t.is("Ball", items[3].name); |
| 171 |
t.is(lastQuery, null); |
| 172 |
d.callback(true); |
| 173 |
}, |
| 174 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 175 |
}); |
| 176 |
return d; |
| 177 |
} |
| 178 |
}, |
| 179 |
{ |
| 180 |
name: "Sorting + Paging", |
| 181 |
timeout: 10000, |
| 182 |
runTest: function(t) { |
| 183 |
|
| 184 |
|
| 185 |
jsonStore.jsonQueryPagination = true; |
| 186 |
var d = new doh.Deferred(); |
| 187 |
lastQuery = null; |
| 188 |
jsonStore.fetch({sort:[{attribute:"name", descending: true}],start:1, count:2, |
| 189 |
onBegin: function(count){ |
| 190 |
t.is(count,4); |
| 191 |
}, |
| 192 |
onComplete: function(items, request){ |
| 193 |
t.is("Hula Hoop", items[0].name); |
| 194 |
t.is("Car", items[1].name); |
| 195 |
t.is(items.length, 2); |
| 196 |
t.is(lastQuery, null); |
| 197 |
d.callback(true); |
| 198 |
}, |
| 199 |
onError: dojo.partial(dojox.data.tests.stores.JsonQueryRestStore.error, doh, d) |
| 200 |
}); |
| 201 |
return d; |
| 202 |
} |
| 203 |
} |
| 204 |
] |
| 205 |
); |