| 1 |
dojo.provide("dojox.rpc.tests.stores.JsonRestStore"); |
| 2 |
|
| 3 |
dojo.require("dojox.data.JsonRestStore"); |
| 4 |
dojo.require("dojo.data.api.Read"); |
| 5 |
dojo.require("dojox.rpc.Service"); |
| 6 |
|
| 7 |
dojox.rpc.tests.stores.JsonRestStore.error = function(t, d, errData){ |
| 8 |
|
| 9 |
|
| 10 |
d.errback(errData); |
| 11 |
} |
| 12 |
var testServices = new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc.tests.resources", "test.smd")); |
| 13 |
var jsonStore = new dojox.data.JsonRestStore({service:testServices.jsonRestStore}); |
| 14 |
|
| 15 |
doh.register("dojox.rpc.tests.stores.JsonRestStore", |
| 16 |
[ |
| 17 |
{ |
| 18 |
name: "Fetch some items", |
| 19 |
timeout: 10000, |
| 20 |
runTest: function(t) { |
| 21 |
|
| 22 |
|
| 23 |
var d = new doh.Deferred(); |
| 24 |
jsonStore.fetch({query:"query", |
| 25 |
onComplete: function(items, request){ |
| 26 |
t.is(4, items.length); |
| 27 |
d.callback(true); |
| 28 |
}, |
| 29 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 30 |
return d; |
| 31 |
} |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "fetch by id", |
| 35 |
timeout: 10000, |
| 36 |
runTest: function(t) { |
| 37 |
|
| 38 |
|
| 39 |
var d = new doh.Deferred(); |
| 40 |
jsonStore.fetch({query:"obj1", |
| 41 |
onComplete: function(item, request){ |
| 42 |
t.is("Object 1", item.name); |
| 43 |
t.t(jsonStore.hasAttribute(item,"name")); |
| 44 |
t.is(jsonStore.getValues(item,"name").length,1); |
| 45 |
t.t(jsonStore.isItem(item)); |
| 46 |
d.callback(true); |
| 47 |
}, |
| 48 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 49 |
return d; |
| 50 |
} |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "Modify,save, check by id", |
| 54 |
timeout: 10000, |
| 55 |
runTest: function(t) { |
| 56 |
|
| 57 |
|
| 58 |
var d = new doh.Deferred(); |
| 59 |
jsonStore.fetch({query:"query", |
| 60 |
onComplete: function(items, request){ |
| 61 |
var now = new Date().getTime(); |
| 62 |
jsonStore.setValue(items[0],"updated",now); |
| 63 |
jsonStore.setValue(items[0],"obj",{foo:'bar'}); |
| 64 |
jsonStore.setValue(items[0],"obj dup",items[0].obj); |
| 65 |
jsonStore.setValue(items[0],"testArray",[1,2,3,4]); |
| 66 |
jsonStore.save(); |
| 67 |
jsonStore.fetch({query:"obj1", |
| 68 |
onComplete: function(item, request){ |
| 69 |
t.is("Object 1", item.name); |
| 70 |
t.is(now, item.updated); |
| 71 |
t.is("bar", item.obj.foo); |
| 72 |
t.is(item.obj, item['obj dup']); |
| 73 |
d.callback(true); |
| 74 |
}, |
| 75 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 76 |
}, |
| 77 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 78 |
return d; |
| 79 |
} |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "Post, delete, and put", |
| 83 |
timeout: 10000, |
| 84 |
runTest: function(t) { |
| 85 |
|
| 86 |
|
| 87 |
var d = new doh.Deferred(); |
| 88 |
jsonStore.fetch({query:"obj1", |
| 89 |
onComplete: function(item, request){ |
| 90 |
var now = new Date().getTime(); |
| 91 |
var testArray = item.testArray; |
| 92 |
var newObject = {"name":"new object"}; |
| 93 |
testArray.push(newObject); |
| 94 |
jsonStore.save(); |
| 95 |
jsonStore.deleteItem(newObject,{parent:testArray}); |
| 96 |
jsonStore.save(); |
| 97 |
testArray.sort(function(obj1,obj2) { return obj1 < obj2; }); |
| 98 |
jsonStore.save(); |
| 99 |
testArray.sort(function(obj1,obj2) { return obj1 > obj2; }); |
| 100 |
jsonStore.save(); |
| 101 |
d.callback(true); |
| 102 |
}, |
| 103 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 104 |
return d; |
| 105 |
} |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "Revert", |
| 109 |
timeout: 10000, |
| 110 |
runTest: function(t) { |
| 111 |
|
| 112 |
|
| 113 |
var d = new doh.Deferred(); |
| 114 |
jsonStore.fetch({query:"obj1", |
| 115 |
onComplete: function(item, request){ |
| 116 |
jsonStore.setValue(item,"name","new name"); |
| 117 |
jsonStore.setValue(item,"newProp","new value"); |
| 118 |
jsonStore.unsetAttribute(item,"updated"); |
| 119 |
t.is(jsonStore.getValue(item,"name"),"new name"); |
| 120 |
t.is(jsonStore.getValue(item,"newProp"),"new value"); |
| 121 |
t.is(jsonStore.getValue(item,"updated"),undefined); |
| 122 |
jsonStore.revert(); |
| 123 |
t.is(jsonStore.getValue(item,"name"),"Object 1"); |
| 124 |
t.is(jsonStore.getValue(item,"newProp"),undefined); |
| 125 |
t.t(typeof jsonStore.getValue(item,"updated") == 'number'); |
| 126 |
d.callback(true); |
| 127 |
}, |
| 128 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 129 |
return d; |
| 130 |
} |
| 131 |
}, |
| 132 |
{ |
| 133 |
name: "Lazy loading", |
| 134 |
timeout: 10000, |
| 135 |
runTest: function(t) { |
| 136 |
|
| 137 |
|
| 138 |
var d = new doh.Deferred(); |
| 139 |
jsonStore.fetch({query:"query", |
| 140 |
onComplete: function(items, request){ |
| 141 |
|
| 142 |
|
| 143 |
jsonStore.getValue(items,3,function(item) { |
| 144 |
t.is(item.name,'Object 4'); |
| 145 |
d.callback(true); |
| 146 |
}); |
| 147 |
}, |
| 148 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 149 |
return d; |
| 150 |
} |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "Array manipulation", |
| 154 |
timeout: 10000, |
| 155 |
runTest: function(t) { |
| 156 |
|
| 157 |
|
| 158 |
var d = new doh.Deferred(); |
| 159 |
jsonStore.fetch({query:"obj1", |
| 160 |
onComplete: function(item, request){ |
| 161 |
var testArray = item.testArray; |
| 162 |
testArray.reverse(); |
| 163 |
testArray.unshift(testArray.pop()); |
| 164 |
jsonStore.onSave = function(data) { |
| 165 |
t.is(data.length,1); |
| 166 |
d.callback(true); |
| 167 |
jsonStore.onSave = function(){}; |
| 168 |
}; |
| 169 |
jsonStore.save(); |
| 170 |
}, |
| 171 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, doh, d)}); |
| 172 |
return d; |
| 173 |
} |
| 174 |
}, |
| 175 |
|
| 176 |
{ |
| 177 |
name: "ReadAPI: Fetch_20_Streaming", |
| 178 |
timeout: 10000, |
| 179 |
runTest: function(t) { |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
var d = new doh.Deferred(); |
| 184 |
var count = 0; |
| 185 |
|
| 186 |
function onItem(item, requestObj){ |
| 187 |
t.assertTrue(typeof item == 'number'); |
| 188 |
count++; |
| 189 |
} |
| 190 |
function onComplete(items, request){ |
| 191 |
t.is(5, count); |
| 192 |
|
| 193 |
t.is(null, items); |
| 194 |
d.callback(true); |
| 195 |
} |
| 196 |
|
| 197 |
jsonStore.fetch({ |
| 198 |
query: "bigQuery", |
| 199 |
onBegin: null, |
| 200 |
count: 5, |
| 201 |
onItem: onItem, |
| 202 |
onComplete: onComplete, |
| 203 |
onError: dojo.partial(dojox.rpc.tests.stores.JsonRestStore.error, t, d) |
| 204 |
}); |
| 205 |
return d; |
| 206 |
} |
| 207 |
}, |
| 208 |
function testReadAPI_functionConformance(t){ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
var readApi = new dojo.data.api.Read(); |
| 215 |
var passed = true; |
| 216 |
|
| 217 |
for(i in readApi){ |
| 218 |
if(i.toString().charAt(0) !== '_') |
| 219 |
{ |
| 220 |
var member = readApi[i]; |
| 221 |
|
| 222 |
if(typeof member === "function"){ |
| 223 |
var testStoreMember = jsonStore [i]; |
| 224 |
if(!(typeof testStoreMember === "function")){ |
| 225 |
passed = false; |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
] |
| 233 |
); |
| 234 |
|