aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/rpc/tests/stores/JsonRestStore.js
blob: 25e0fc7193d30fc3fcade82ebb95824650844d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
if(!dojo._hasResource["dojox.data.tests.stores.JsonRestStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.data.tests.stores.JsonRestStore"] = true;
dojo.provide("dojox.data.tests.stores.JsonRestStore");
dojo.require("dojox.data.JsonRestStore");
dojo.require("dojo.data.api.Read");
dojo.require("dojox.rpc.Service");

dojox.data.tests.stores.JsonRestStore.error = function(t, d, errData){
	//  summary:
	//		The error callback function to be used for all of the tests.
	d.errback(errData);	
}
var testServices = new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc.tests.resources", "test.smd"));
var jsonStore = new dojox.data.JsonRestStore({service:testServices.jsonRestStore});

doh.register("dojox.data.tests.stores.JsonRestStore", 
	[
		{
			name: "Fetch some items",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		Simple test of a basic fetch on JsonRestStore of a simple query.
				var d = new doh.Deferred();
				jsonStore.fetch({query:"query", 
					onComplete: function(items, request){
						t.is(4, items.length);
						d.callback(true);
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "fetch by id",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		Simple test of a basic fetch on JsonRestStore of a single item.
				var d = new doh.Deferred();
				jsonStore.fetch({query:"obj1", 
					onComplete: function(item, request){
						t.is("Object 1", item.name);
						t.t(jsonStore.hasAttribute(item,"name"));
						t.is(jsonStore.getValues(item,"name").length,1);
						t.t(jsonStore.isItem(item));
						d.callback(true);
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "Modify,save, check by id",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		Fetch an item from a query, modify and save it, and check to see if it was modified correctly
				var d = new doh.Deferred();
				jsonStore.fetch({query:"query", 
					onComplete: function(items, request){
						var now = new Date().getTime();
						jsonStore.setValue(items[0],"updated",now);
						jsonStore.setValue(items[0],"obj",{foo:'bar'});
						jsonStore.setValue(items[0],"obj dup",items[0].obj);
						jsonStore.setValue(items[0],"testArray",[1,2,3,4]);
						jsonStore.save();
						jsonStore.fetch({query:"obj1",
							onComplete: function(item, request){
								t.is("Object 1", item.name);
								t.is(now, item.updated);
								t.is("bar", item.obj.foo);
								t.is(item.obj, item['obj dup']);
								d.callback(true);
							},
							onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "Post, delete, and put",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		append/post an item, delete it, sort the lists, resort the list, saving each time.
				var d = new doh.Deferred();
				jsonStore.fetch({query:"obj1", 
					onComplete: function(item, request){
						var now = new Date().getTime();
						var testArray = item.testArray;
						var newObject = {"name":"new object"};
						testArray.push(newObject);
						jsonStore.save();
						jsonStore.deleteItem(newObject,{parent:testArray});
						jsonStore.save();
						testArray.sort(function(obj1,obj2) { return obj1 < obj2; });
						jsonStore.save();
						testArray.sort(function(obj1,obj2) { return obj1 > obj2; });
						jsonStore.save();
						d.callback(true);
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "Revert",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		append/post an item, delete it, sort the lists, resort the list, saving each time.
				var d = new doh.Deferred();
				jsonStore.fetch({query:"obj1", 
					onComplete: function(item, request){
						jsonStore.setValue(item,"name","new name");
						jsonStore.setValue(item,"newProp","new value");
						jsonStore.unsetAttribute(item,"updated");
						t.is(jsonStore.getValue(item,"name"),"new name");
						t.is(jsonStore.getValue(item,"newProp"),"new value");
						t.is(jsonStore.getValue(item,"updated"),undefined);
						jsonStore.revert();
						t.is(jsonStore.getValue(item,"name"),"Object 1");
						t.is(jsonStore.getValue(item,"newProp"),undefined);
						t.t(typeof jsonStore.getValue(item,"updated") == 'number');
						d.callback(true);
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "Lazy loading",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		test lazy loading
				var d = new doh.Deferred();
				jsonStore.fetch({query:"query", 
					onComplete: function(items, request){
						/*var item = jsonStore.getValue(items,2); // sync lazy loading
						t.is(item.name == 'Object 3');*/
						jsonStore.getValue(items,3,function(item) { // async lazy loading
							t.is(item.name,'Object 4');
							d.callback(true);
						});
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		{
			name: "Array manipulation",
			timeout:	10000, //10 seconds.
			runTest: function(t) {
				//	summary: 
				//		test array manipulation
				var d = new doh.Deferred();
				jsonStore.fetch({query:"obj1", 
					onComplete: function(item, request){
						var testArray = item.testArray;
						testArray.reverse();
						testArray.unshift(testArray.pop());
						jsonStore.onSave = function(data) {
							t.is(data.length,1);						
							d.callback(true);
							jsonStore.onSave = function(){};
						};
						jsonStore.save();
					},
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)});
				return d; //Object
			}
		},
		
		{
			name: "ReadAPI:  Fetch_20_Streaming",
			timeout:	10000, //10 seconds.  Json can sometimes be slow.
			runTest: function(t) {
				//	summary: 
				//		fetching with paging

				var d = new doh.Deferred();
				var count = 0;

				function onItem(item, requestObj){
				  t.assertTrue(typeof item == 'number');
				  count++;
				}
				function onComplete(items, request){
					t.is(5, count);
					
					t.is(null, items);
					d.callback(true);
				}
				//Get everything...
				jsonStore.fetch({	
					query: "bigQuery",
					onBegin: null,
					count: 5,
					onItem: onItem,
					onComplete: onComplete,
					onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, t, d)
				});
				return d; //Object
			}
		},
		function testReadAPI_functionConformance(t){
			//	summary: 
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.
			//	description:
			//		Simple test read API conformance.  Checks to see all declared functions are actual functions on the instances.

			var readApi = new dojo.data.api.Read();
			var passed = true;

			for(i in readApi){
				if(i.toString().charAt(0) !== '_')
				{
					var member = readApi[i];
					//Check that all the 'Read' defined functions exist on the test store.
					if(typeof member === "function"){
						var testStoreMember = jsonStore	[i];
						if(!(typeof testStoreMember === "function")){
							passed = false;
							break;
						}
					}
				}
			}
		}
	]
);


}