| 1 |
dojo.provide("dojox.data.tests.stores.JsonRestStore"); |
| 2 |
dojo.require("dojox.rpc.Service"); |
| 3 |
|
| 4 |
dojo.require("dojox.data.JsonRestStore"); |
| 5 |
dojo.require("dojox.json.schema"); |
| 6 |
dojo.require("dojo.data.api.Read"); |
| 7 |
|
| 8 |
dojox.data.tests.stores.JsonRestStore.error = function(t, d, errData){ |
| 9 |
|
| 10 |
|
| 11 |
d.errback(errData); |
| 12 |
}; |
| 13 |
testServices = new dojox.rpc.Service(require.toUrl("dojox/rpc/tests/resources/test.smd")); |
| 14 |
testServices.jsonRestStore.servicePath = "/jsonRest.Store/"; |
| 15 |
jsonStore = new dojox.data.JsonRestStore({service:testServices.jsonRestStore}); |
| 16 |
|
| 17 |
doh.register("dojox.data.tests.stores.JsonRestStore", |
| 18 |
[ |
| 19 |
{ |
| 20 |
name: "Fetch some items", |
| 21 |
timeout: 10000, |
| 22 |
runTest: function(t) { |
| 23 |
|
| 24 |
|
| 25 |
var d = new doh.Deferred(); |
| 26 |
jsonStore.fetch({query:"query", |
| 27 |
onComplete: function(items, request){ |
| 28 |
t.is(4, items.length); |
| 29 |
d.callback(true); |
| 30 |
}, |
| 31 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 32 |
}); |
| 33 |
return d; |
| 34 |
} |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "fetch by id", |
| 38 |
timeout: 10000, |
| 39 |
runTest: function(t) { |
| 40 |
|
| 41 |
|
| 42 |
var d = new doh.Deferred(); |
| 43 |
jsonStore.fetch({query:"obj1", |
| 44 |
onComplete: function(item, request){ |
| 45 |
t.is("Object 1", item.name); |
| 46 |
t.t(jsonStore.hasAttribute(item,"name")); |
| 47 |
t.is(jsonStore.getValues(item,"name").length,1); |
| 48 |
t.t(jsonStore.isItem(item)); |
| 49 |
d.callback(true); |
| 50 |
}, |
| 51 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 52 |
}); |
| 53 |
return d; |
| 54 |
} |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "Modify,save, check by id", |
| 58 |
timeout: 10000, |
| 59 |
runTest: function(t) { |
| 60 |
|
| 61 |
|
| 62 |
var d = new doh.Deferred(); |
| 63 |
jsonStore.fetch({query:"query", |
| 64 |
onComplete: function(items, request){ |
| 65 |
var now = new Date().getTime(); |
| 66 |
jsonStore.setValue(items[0],"updated",now); |
| 67 |
jsonStore.setValue(items[0],"obj",{foo:'bar'}); |
| 68 |
jsonStore.setValue(items[0],"obj dup",items[0].obj); |
| 69 |
jsonStore.setValue(items[0],"testArray",[1,2,3,4]); |
| 70 |
jsonStore.save({onComplete:function(){ |
| 71 |
jsonStore.fetchItemByIdentity({identity:"obj1", |
| 72 |
onItem: function(item, request){ |
| 73 |
t.is("Object 1", item.name); |
| 74 |
t.is(now, item.updated); |
| 75 |
t.is("bar", item.obj.foo); |
| 76 |
t.is(item.obj, item['obj dup']); |
| 77 |
d.callback(true); |
| 78 |
}, |
| 79 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 80 |
}); |
| 81 |
}}); |
| 82 |
}, |
| 83 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 84 |
}); |
| 85 |
return d; |
| 86 |
} |
| 87 |
}, |
| 88 |
{ |
| 89 |
name: "Revert", |
| 90 |
timeout: 10000, |
| 91 |
runTest: function(t) { |
| 92 |
|
| 93 |
|
| 94 |
var d = new doh.Deferred(); |
| 95 |
jsonStore.fetch({query:"obj1", |
| 96 |
onComplete: function(item, request){ |
| 97 |
jsonStore.setValue(item,"name","new name"); |
| 98 |
jsonStore.setValue(item,"newProp","new value"); |
| 99 |
jsonStore.unsetAttribute(item,"updated"); |
| 100 |
t.is(jsonStore.getValue(item,"name"),"new name"); |
| 101 |
t.is(jsonStore.getValue(item,"newProp"),"new value"); |
| 102 |
t.is(jsonStore.getValue(item,"updated"),undefined); |
| 103 |
jsonStore.revert(); |
| 104 |
t.is(jsonStore.getValue(item,"name"),"Object 1"); |
| 105 |
t.is(jsonStore.getValue(item,"newProp"),undefined); |
| 106 |
t.t(typeof jsonStore.getValue(item,"updated") == 'number'); |
| 107 |
d.callback(true); |
| 108 |
}, |
| 109 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 110 |
}); |
| 111 |
return d; |
| 112 |
} |
| 113 |
}, |
| 114 |
{ |
| 115 |
name: "Delete", |
| 116 |
timeout: 10000, |
| 117 |
runTest: function(t) { |
| 118 |
|
| 119 |
|
| 120 |
var d = new doh.Deferred(); |
| 121 |
jsonStore.fetchItemByIdentity({identity:"obj1", |
| 122 |
onItem: function(item, request){ |
| 123 |
var newItem = jsonStore.newItem({directRef: item, name:"Foo"}); |
| 124 |
jsonStore.setValue(newItem, "arrayRef", [1,{subobject:item},item]); |
| 125 |
jsonStore.deleteItem(item); |
| 126 |
t.is(jsonStore.getValue(newItem, "directRef"), undefined); |
| 127 |
t.is(jsonStore.getValue(newItem, "arrayRef").length, 2); |
| 128 |
t.is(jsonStore.getValue(newItem, "arrayRef")[1].subobject, undefined); |
| 129 |
jsonStore.revert(); |
| 130 |
d.callback(true); |
| 131 |
}, |
| 132 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 133 |
}); |
| 134 |
return d; |
| 135 |
} |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "Lazy loading", |
| 139 |
timeout: 10000, |
| 140 |
runTest: function(t) { |
| 141 |
|
| 142 |
|
| 143 |
var d = new doh.Deferred(); |
| 144 |
jsonStore.fetch({query:"query", |
| 145 |
onComplete: function(items, request){ |
| 146 |
var item = items[2]; |
| 147 |
t.f(jsonStore.isItemLoaded(item)); |
| 148 |
jsonStore.getValue(item,"name"); |
| 149 |
t.is(items[2],item); |
| 150 |
t.is(item.name,'Object 3'); |
| 151 |
d.callback(true); |
| 152 |
}, |
| 153 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 154 |
}); |
| 155 |
return d; |
| 156 |
} |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "Lazy loading 2", |
| 160 |
timeout: 10000, |
| 161 |
runTest: function(t) { |
| 162 |
|
| 163 |
|
| 164 |
var d = new doh.Deferred(); |
| 165 |
jsonStore.fetch({query:"query", |
| 166 |
onComplete: function(items, request){ |
| 167 |
t.f(jsonStore.isItemLoaded(items[3])); |
| 168 |
jsonStore.loadItem({item:items[3],onItem:function(item){ |
| 169 |
t.t(jsonStore.isItemLoaded(items[3])); |
| 170 |
t.is(item,items[3]); |
| 171 |
t.is(item.name,'Object 4'); |
| 172 |
d.callback(true); |
| 173 |
}}); |
| 174 |
|
| 175 |
}, |
| 176 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d) |
| 177 |
}); |
| 178 |
return d; |
| 179 |
} |
| 180 |
}, |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
{ |
| 204 |
name: "IdentityAPI: fetchItemByIdentity and getIdentity", |
| 205 |
timeout: 30000, |
| 206 |
runTest: function(t) { |
| 207 |
|
| 208 |
|
| 209 |
var d = new doh.Deferred(); |
| 210 |
|
| 211 |
jsonStore.fetchItemByIdentity({identity:"obj3", |
| 212 |
onItem: function(item, request){ |
| 213 |
t.t(jsonStore.isItemLoaded(item)); |
| 214 |
t.is(jsonStore.getIdentity(item),"obj3"); |
| 215 |
} |
| 216 |
}); |
| 217 |
} |
| 218 |
}, |
| 219 |
|
| 220 |
|
| 221 |
{ |
| 222 |
name: "ReadAPI: Fetch_20_Streaming", |
| 223 |
timeout: 10000, |
| 224 |
runTest: function(t) { |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
var d = new doh.Deferred(); |
| 229 |
var count = 0; |
| 230 |
|
| 231 |
function onItem(item, requestObj){ |
| 232 |
t.assertTrue(typeof item == 'number'); |
| 233 |
count++; |
| 234 |
} |
| 235 |
function onComplete(items, request){ |
| 236 |
t.is(20, count); |
| 237 |
|
| 238 |
d.callback(true); |
| 239 |
} |
| 240 |
|
| 241 |
jsonStore.fetch({ |
| 242 |
query: "bigQuery", |
| 243 |
onBegin: null, |
| 244 |
count: 20, |
| 245 |
onItem: onItem, |
| 246 |
onComplete: onComplete, |
| 247 |
onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, t, d) |
| 248 |
}); |
| 249 |
return d; |
| 250 |
} |
| 251 |
}, |
| 252 |
function testSchema(t){ |
| 253 |
var d = new doh.Deferred(); |
| 254 |
jsonStore.fetchItemByIdentity({identity:"obj3", |
| 255 |
onItem: function(item, request){ |
| 256 |
var set = false; |
| 257 |
try{ |
| 258 |
jsonStore.setValue(item,"name",333); |
| 259 |
set = true; |
| 260 |
}catch(e){ |
| 261 |
console.log("Correctly blocked invalid property change by schema:",e); |
| 262 |
} |
| 263 |
try{ |
| 264 |
jsonStore.setValue(item,"name","a"); |
| 265 |
set = true; |
| 266 |
}catch(e){ |
| 267 |
console.log("Correctly blocked invalid property change by schema:",e); |
| 268 |
} |
| 269 |
t.f(set); |
| 270 |
d.callback(true); |
| 271 |
} |
| 272 |
}); |
| 273 |
|
| 274 |
}, |
| 275 |
function testReadAPI_functionConformance(t){ |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
var readApi = new dojo.data.api.Read(); |
| 282 |
var passed = true; |
| 283 |
|
| 284 |
for(i in readApi){ |
| 285 |
if(i.toString().charAt(0) !== '_') |
| 286 |
{ |
| 287 |
var member = readApi[i]; |
| 288 |
|
| 289 |
if(typeof member === "function"){ |
| 290 |
var testStoreMember = jsonStore [i]; |
| 291 |
if(!(typeof testStoreMember === "function")){ |
| 292 |
passed = false; |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
}, |
| 299 |
function tryToLoadSameTarget(t){ |
| 300 |
t.is( |
| 301 |
dojox.data.JsonRestStore.getStore({target:"/something/"},dojox.data.JsonRestStore), |
| 302 |
dojox.data.JsonRestStore.getStore({target:"/something/"})); |
| 303 |
|
| 304 |
} |
| 305 |
] |
| 306 |
); |
| 307 |
performanceTest = function (){ |
| 308 |
dojo.require("dojo.data.ItemFileReadStore"); |
| 309 |
jsonStore.fetch({query:"obj1", |
| 310 |
onComplete: function(item){ |
| 311 |
var now = new Date().getTime(); |
| 312 |
var result; |
| 313 |
for(var i=0;i<100000;i++){ |
| 314 |
} |
| 315 |
console.log("Just Loop",new Date().getTime()-now, result); |
| 316 |
now = new Date().getTime(); |
| 317 |
for(i=0;i<100000;i++){ |
| 318 |
result = item.name; |
| 319 |
} |
| 320 |
console.log("Direct Access",new Date().getTime()-now, result); |
| 321 |
|
| 322 |
now = new Date().getTime(); |
| 323 |
for(i=0;i<100000;i++){ |
| 324 |
result = jsonStore.getValue(item,"name"); |
| 325 |
} |
| 326 |
console.log("getValue",new Date().getTime()-now); |
| 327 |
|
| 328 |
var ifrs = new dojo.data.ItemFileReadStore({data:{ identifier:'id',items: [ |
| 329 |
{ id:'1',name:'Fozzie Bear', wears:['hat', 'tie']}, |
| 330 |
{ id:'2',name:'Miss Piggy', pets:'Foo-Foo'} |
| 331 |
]}}); |
| 332 |
ifrs.fetchItemByIdentity({identity:'1',onItem:function(result){ |
| 333 |
item = result; |
| 334 |
}}); |
| 335 |
|
| 336 |
now = new Date().getTime(); |
| 337 |
for(i=0;i<100000;i++){ |
| 338 |
result = ifrs.getValue(item,"name"); |
| 339 |
} |
| 340 |
console.log("ifrs.getValue",new Date().getTime()-now,result); |
| 341 |
|
| 342 |
} |
| 343 |
}); |
| 344 |
|
| 345 |
} |