Debian Codegraph @

dojox/rpc/tests/stores/JsonRestStore.js

raw symbols SCIP
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 // summary:
9 // The error callback function to be used for all of the tests.
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, //10 seconds.
20 runTest: function(t) {
21 // summary:
22 // Simple test of a basic fetch on JsonRestStore of a simple query.
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; //Object
31 }
32 },
33 {
34 name: "fetch by id",
35 timeout: 10000, //10 seconds.
36 runTest: function(t) {
37 // summary:
38 // Simple test of a basic fetch on JsonRestStore of a single item.
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; //Object
50 }
51 },
52 {
53 name: "Modify,save, check by id",
54 timeout: 10000, //10 seconds.
55 runTest: function(t) {
56 // summary:
57 // Fetch an item from a query, modify and save it, and check to see if it was modified correctly
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; //Object
79 }
80 },
81 {
82 name: "Post, delete, and put",
83 timeout: 10000, //10 seconds.
84 runTest: function(t) {
85 // summary:
86 // append/post an item, delete it, sort the lists, resort the list, saving each time.
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; //Object
105 }
106 },
107 {
108 name: "Revert",
109 timeout: 10000, //10 seconds.
110 runTest: function(t) {
111 // summary:
112 // append/post an item, delete it, sort the lists, resort the list, saving each time.
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; //Object
130 }
131 },
132 {
133 name: "Lazy loading",
134 timeout: 10000, //10 seconds.
135 runTest: function(t) {
136 // summary:
137 // test lazy loading
138 var d = new doh.Deferred();
139 jsonStore.fetch({query:"query",
140 onComplete: function(items, request){
141 /*var item = jsonStore.getValue(items,2); // sync lazy loading
142 t.is(item.name == 'Object 3');*/
143 jsonStore.getValue(items,3,function(item) { // async lazy loading
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; //Object
150 }
151 },
152 {
153 name: "Array manipulation",
154 timeout: 10000, //10 seconds.
155 runTest: function(t) {
156 // summary:
157 // test array manipulation
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; //Object
173 }
174 },
175
176 {
177 name: "ReadAPI: Fetch_20_Streaming",
178 timeout: 10000, //10 seconds. Json can sometimes be slow.
179 runTest: function(t) {
180 // summary:
181 // fetching with paging
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 //Get everything...
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; //Object
206 }
207 },
208 function testReadAPI_functionConformance(t){
209 // summary:
210 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
211 // description:
212 // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
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 //Check that all the 'Read' defined functions exist on the test store.
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