/public/javascripts/dojo/release/dojo/dojox/data/tests/stores/JsonRestStore.js

http://enginey.googlecode.com/ · JavaScript · 320 lines · 255 code · 19 blank · 46 comment · 11 complexity · cd6da1d7a634e6df9be813e8e2493e21 MD5 · raw file

  1. if(!dojo._hasResource["dojox.data.tests.stores.JsonRestStore"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  2. dojo._hasResource["dojox.data.tests.stores.JsonRestStore"] = true;
  3. dojo.provide("dojox.data.tests.stores.JsonRestStore");
  4. dojo.require("dojox.rpc.Service");
  5. //dojo.require("dojox.data.ClientFilter");
  6. dojo.require("dojox.data.JsonRestStore");
  7. dojo.require("dojox.json.schema");
  8. dojo.require("dojo.data.api.Read");
  9. dojox.data.tests.stores.JsonRestStore.error = function(t, d, errData){
  10. // summary:
  11. // The error callback function to be used for all of the tests.
  12. d.errback(errData);
  13. }
  14. testServices = new dojox.rpc.Service(dojo.moduleUrl("dojox.rpc.tests.resources", "test.smd"));
  15. testServices.jsonRestStore.servicePath = "/jsonRest.Store/"; // this makes the regex more challenging
  16. jsonStore = new dojox.data.JsonRestStore({service:testServices.jsonRestStore});
  17. doh.register("dojox.data.tests.stores.JsonRestStore",
  18. [
  19. {
  20. name: "Fetch some items",
  21. timeout: 10000, //10 seconds.
  22. runTest: function(t) {
  23. // summary:
  24. // Simple test of a basic fetch on JsonRestStore of a simple query.
  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; //Object
  34. }
  35. },
  36. {
  37. name: "fetch by id",
  38. timeout: 10000, //10 seconds.
  39. runTest: function(t) {
  40. // summary:
  41. // Simple test of a basic fetch on JsonRestStore of a single item.
  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; //Object
  54. }
  55. },
  56. {
  57. name: "Modify,save, check by id",
  58. timeout: 10000, //10 seconds.
  59. runTest: function(t) {
  60. // summary:
  61. // Fetch an item from a query, modify and save it, and check to see if it was modified correctly
  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; //Object
  86. }
  87. },
  88. {
  89. name: "Revert",
  90. timeout: 10000, //10 seconds.
  91. runTest: function(t) {
  92. // summary:
  93. // append/post an item, delete it, sort the lists, resort the list, saving each time.
  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; //Object
  112. }
  113. },
  114. {
  115. name: "Lazy loading",
  116. timeout: 10000, //10 seconds.
  117. runTest: function(t) {
  118. // summary:
  119. // test lazy loading
  120. var d = new doh.Deferred();
  121. jsonStore.fetch({query:"query",
  122. onComplete: function(items, request){
  123. var item = items[2];
  124. t.f(jsonStore.isItemLoaded(item));
  125. jsonStore.getValue(item,"name"); // this should trigger the load
  126. t.is(items[2],item);
  127. t.is(item.name,'Object 3');
  128. d.callback(true);
  129. },
  130. onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)
  131. });
  132. return d; //Object
  133. }
  134. },
  135. {
  136. name: "Lazy loading 2",
  137. timeout: 10000, //10 seconds.
  138. runTest: function(t) {
  139. // summary:
  140. // test lazy loading
  141. var d = new doh.Deferred();
  142. jsonStore.fetch({query:"query",
  143. onComplete: function(items, request){
  144. t.f(jsonStore.isItemLoaded(items[3]));
  145. jsonStore.loadItem({item:items[3],onItem:function(item){
  146. t.t(jsonStore.isItemLoaded(items[3]));
  147. t.is(item,items[3]);
  148. t.is(item.name,'Object 4');
  149. d.callback(true);
  150. }});
  151. },
  152. onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)
  153. });
  154. return d; //Object
  155. }
  156. },
  157. /*{
  158. name: "Load Lazy Value",
  159. timeout: 10000, //10 seconds.
  160. runTest: function(t) {
  161. // summary:
  162. // Simple test of a basic fetch on ServiceStore of a single item.
  163. var d = new doh.Deferred();
  164. jsonStore.fetchItemByIdentity({identity:"obj1",
  165. onItem: function(item, request){
  166. t.is("Object 1", item.name);
  167. t.f(jsonStore.isItemLoaded(item.lazyValue));
  168. var lazyValue = jsonStore.getValue(item,"lazyValue");
  169. t.is("Finally loaded",lazyValue);
  170. lazyValue = jsonStore.getValue(item,"lazyValue");
  171. d.callback(true);
  172. },
  173. onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, doh, d)
  174. });
  175. return d; //Object
  176. }
  177. },*/
  178. {
  179. name: "IdentityAPI: fetchItemByIdentity and getIdentity",
  180. timeout: 30000,
  181. runTest: function(t) {
  182. // summary:
  183. // Verify the fetchItemByIdentity method works
  184. var d = new doh.Deferred();
  185. jsonStore.fetchItemByIdentity({identity:"obj3",
  186. onItem: function(item, request){
  187. t.t(jsonStore.isItemLoaded(item));
  188. t.is(jsonStore.getIdentity(item),"obj3");
  189. }
  190. });
  191. }
  192. },
  193. {
  194. name: "ReadAPI: Fetch_20_Streaming",
  195. timeout: 10000, //10 seconds. Json can sometimes be slow.
  196. runTest: function(t) {
  197. // summary:
  198. // fetching with paging
  199. var d = new doh.Deferred();
  200. var count = 0;
  201. function onItem(item, requestObj){
  202. t.assertTrue(typeof item == 'number');
  203. count++;
  204. }
  205. function onComplete(items, request){
  206. t.is(20, count);
  207. d.callback(true);
  208. }
  209. //Get everything...
  210. jsonStore.fetch({
  211. query: "bigQuery",
  212. onBegin: null,
  213. count: 20,
  214. onItem: onItem,
  215. onComplete: onComplete,
  216. onError: dojo.partial(dojox.data.tests.stores.JsonRestStore.error, t, d)
  217. });
  218. return d; //Object
  219. }
  220. },
  221. function testSchema(t){
  222. var d = new doh.Deferred();
  223. jsonStore.fetchItemByIdentity({identity:"obj3",
  224. onItem: function(item, request){
  225. var set = false;
  226. try{
  227. jsonStore.setValue(item,"name",333); // should only take a string, so it should throw an exception
  228. set = true;
  229. }catch(e){
  230. console.log("Correctly blocked invalid property change by schema:",e);
  231. }
  232. try{
  233. jsonStore.setValue(item,"name","a"); // should be at least three character, so it should throw an execption
  234. set = true;
  235. }catch(e){
  236. console.log("Correctly blocked invalid property change by schema:",e);
  237. }
  238. t.f(set);
  239. d.callback(true);
  240. }
  241. });
  242. },
  243. function testReadAPI_functionConformance(t){
  244. // summary:
  245. // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
  246. // description:
  247. // Simple test read API conformance. Checks to see all declared functions are actual functions on the instances.
  248. var readApi = new dojo.data.api.Read();
  249. var passed = true;
  250. for(i in readApi){
  251. if(i.toString().charAt(0) !== '_')
  252. {
  253. var member = readApi[i];
  254. //Check that all the 'Read' defined functions exist on the test store.
  255. if(typeof member === "function"){
  256. var testStoreMember = jsonStore [i];
  257. if(!(typeof testStoreMember === "function")){
  258. passed = false;
  259. break;
  260. }
  261. }
  262. }
  263. }
  264. }
  265. ]
  266. );
  267. performanceTest = function (){
  268. dojo.require("dojo.data.ItemFileReadStore");
  269. jsonStore.fetch({query:"obj1",
  270. onComplete: function(item){
  271. var now = new Date().getTime();
  272. var result;
  273. for(var i=0;i<100000;i++){
  274. }
  275. console.log("Just Loop",new Date().getTime()-now, result);
  276. now = new Date().getTime();
  277. for(i=0;i<100000;i++){
  278. result = item.name;
  279. }
  280. console.log("Direct Access",new Date().getTime()-now, result);
  281. now = new Date().getTime();
  282. for(i=0;i<100000;i++){
  283. result = jsonStore.getValue(item,"name");
  284. }
  285. console.log("getValue",new Date().getTime()-now);
  286. var ifrs = new dojo.data.ItemFileReadStore({data:{ identifier:'id',items: [
  287. { id:'1',name:'Fozzie Bear', wears:['hat', 'tie']},
  288. { id:'2',name:'Miss Piggy', pets:'Foo-Foo'}
  289. ]}});
  290. ifrs.fetchItemByIdentity({identity:'1',onItem:function(result){
  291. item = result;
  292. }});
  293. now = new Date().getTime();
  294. for(i=0;i<100000;i++){
  295. result = ifrs.getValue(item,"name");
  296. }
  297. console.log("ifrs.getValue",new Date().getTime()-now,result);
  298. }
  299. });
  300. }
  301. }