/static/recline/test/backend.memory.test.js

https://github.com/SEL-Columbia/formhub · JavaScript · 370 lines · 316 code · 45 blank · 9 comment · 0 complexity · 10fb07e758f259177ee85a4dd4a6a08e MD5 · raw file

  1. (function ($) {
  2. module("Backend Memory - Store");
  3. var memoryData = [
  4. {id: 0, date: '2011-01-01', x: 1, y: 2, z: 3, country: 'DE', label: 'first'}
  5. , {id: 1, date: '2011-02-03', x: 2, y: 4, z: 6, country: 'UK', label: 'second'}
  6. , {id: 2, date: '2011-04-05', x: 3, y: 6, z: 9, country: 'US', label: 'third'}
  7. , {id: 3, date: '2011-06-07', x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'}
  8. , {id: 4, date: '2011-08-09', x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'}
  9. , {id: 5, date: '2011-10-11', x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'}
  10. ];
  11. var memoryFields = [
  12. {id: 'id'},
  13. {id: 'date', type: 'date'},
  14. {id: 'x', type: 'integer'},
  15. {id: 'y', type: 'integer'},
  16. {id: 'z', type: 'integer'},
  17. {id: 'country'},
  18. {id: 'label'}
  19. ];
  20. var _wrapData = function() {
  21. var dataCopy = $.extend(true, [], memoryData);
  22. // return new recline.Backend.Memory.Store(dataCopy, fields);
  23. return new recline.Backend.Memory.Store(dataCopy, memoryFields);
  24. }
  25. test('basics', function () {
  26. var data = _wrapData();
  27. equal(data.fields.length, 7);
  28. deepEqual(['id', 'date', 'x', 'y', 'z', 'country', 'label'], _.pluck(data.fields, 'id'));
  29. equal(memoryData.length, data.data.length);
  30. });
  31. test('query', function () {
  32. var data = _wrapData();
  33. var queryObj = {
  34. size: 4
  35. , from: 2
  36. };
  37. data.query(queryObj).then(function(out) {
  38. deepEqual(out.hits[0], memoryData[2]);
  39. equal(out.hits.length, 4);
  40. equal(out.total, 6);
  41. });
  42. });
  43. test('query sort', function () {
  44. var data = _wrapData();
  45. var queryObj = {
  46. sort: [
  47. {field: 'y', order: 'desc'}
  48. ]
  49. };
  50. data.query(queryObj).then(function(out) {
  51. equal(out.hits[0].x, 6);
  52. });
  53. var queryObj = {
  54. sort: [
  55. {field: 'country', order: 'desc'}
  56. ]
  57. };
  58. data.query(queryObj).then(function(out) {
  59. equal(out.hits[0].country, 'US');
  60. });
  61. var queryObj = {
  62. sort: [
  63. {field: 'country', order: 'asc'}
  64. ]
  65. };
  66. data.query(queryObj).then(function(out) {
  67. equal(out.hits[0].country, 'DE');
  68. });
  69. });
  70. test('query string', function () {
  71. var data = _wrapData();
  72. data.query({q: 'UK'}).then(function(out) {
  73. equal(out.total, 3);
  74. deepEqual(_.pluck(out.hits, 'country'), ['UK', 'UK', 'UK']);
  75. });
  76. data.query({q: 'UK 6'}).then(function(out) {
  77. equal(out.total, 2); // the new regex support will find 2 hits
  78. deepEqual(out.hits[0].id, 1);
  79. });
  80. });
  81. test('filters', function () {
  82. var data = _wrapData();
  83. var query = new recline.Model.Query();
  84. query.addFilter({type: 'term', field: 'country', term: 'UK'});
  85. data.query(query.toJSON()).then(function(out) {
  86. equal(out.total, 3);
  87. deepEqual(_.pluck(out.hits, 'country'), ['UK','UK','UK']);
  88. });
  89. query = new recline.Model.Query();
  90. query.addFilter({type: 'range', field: 'date', start: '2011-01-01', stop: '2011-05-01'});
  91. data.query(query.toJSON()).then(function(out) {
  92. equal(out.total, 3);
  93. deepEqual(_.pluck(out.hits, 'date'), ['2011-01-01','2011-02-03','2011-04-05']);
  94. });
  95. query = new recline.Model.Query();
  96. query.addFilter({type: 'range', field: 'z', start: '0', stop: '10'});
  97. data.query(query.toJSON()).then(function(out) {
  98. equal(out.total, 3);
  99. deepEqual(_.pluck(out.hits, 'z'), [3,6,9]);
  100. });
  101. });
  102. test('filters with nulls', function () {
  103. var data = _wrapData();
  104. query = new recline.Model.Query();
  105. query.addFilter({type: 'range', field: 'z', start: '', stop: null});
  106. data.query(query.toJSON()).then(function(out) {
  107. equal(out.total, 6);
  108. });
  109. query = new recline.Model.Query();
  110. query.addFilter({type: 'range', field: 'x', start: '', stop: '3'});
  111. data.query(query.toJSON()).then(function(out) {
  112. equal(out.total, 3);
  113. });
  114. query = new recline.Model.Query();
  115. query.addFilter({type: 'range', field: 'x', start: '3', stop: ''});
  116. data.query(query.toJSON()).then(function(out) {
  117. equal(out.total, 4);
  118. });
  119. data.data[5].country = '';
  120. query = new recline.Model.Query();
  121. query.addFilter({type: 'range', field: 'country', start: '', stop: 'Z'});
  122. data.query(query.toJSON()).then(function(out) {
  123. equal(out.total, 5);
  124. });
  125. query = new recline.Model.Query();
  126. query.addFilter({type: 'range', field: 'x', start: '', stop: ''});
  127. data.query(query.toJSON()).then(function(out) {
  128. equal(out.total, 6);
  129. });
  130. });
  131. test('facet', function () {
  132. var data = _wrapData();
  133. var query = new recline.Model.Query();
  134. query.addFacet('country');
  135. var out = data.computeFacets(data.data, query.toJSON());
  136. var exp = [
  137. {
  138. term: 'UK',
  139. count: 3
  140. },
  141. {
  142. term: 'DE',
  143. count: 2
  144. },
  145. {
  146. term: 'US',
  147. count: 1
  148. }
  149. ];
  150. deepEqual(out['country'].terms, exp);
  151. });
  152. test('update and delete', function () {
  153. var data = _wrapData();
  154. // Test UPDATE
  155. var newVal = 10;
  156. doc1 = $.extend(true, {}, memoryData[0]);
  157. doc1.x = newVal;
  158. data.update(doc1);
  159. equal(data.data[0].x, newVal);
  160. // Test Delete
  161. data.remove(doc1);
  162. equal(data.data.length, 5);
  163. equal(data.data[0].x, memoryData[1].x);
  164. });
  165. })(this.jQuery);
  166. // ======================================
  167. (function ($) {
  168. module("Backend Memory - Model Integration");
  169. var memoryFields = [
  170. {id: 'id'},
  171. {id: 'date', type: 'date'},
  172. {id: 'x', type: 'integer'},
  173. {id: 'y', type: 'integer'},
  174. {id: 'z', type: 'integer'},
  175. {id: 'country'},
  176. {id: 'label'}
  177. ];
  178. var memoryData = {
  179. metadata: {
  180. title: 'My Test Dataset'
  181. , name: '1-my-test-dataset'
  182. , id: 'test-dataset'
  183. },
  184. fields: memoryFields,
  185. records: [
  186. {id: 0, x: 1, y: 2, z: 3, country: 'DE', label: 'first'}
  187. , {id: 1, x: 2, y: 4, z: 6, country: 'UK', label: 'second'}
  188. , {id: 2, x: 3, y: 6, z: 9, country: 'US', label: 'third'}
  189. , {id: 3, x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'}
  190. , {id: 4, x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'}
  191. , {id: 5, x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'}
  192. ]
  193. };
  194. function makeBackendDataset() {
  195. var dataset = new recline.Model.Dataset({
  196. id: 'test-dataset',
  197. title: 'My Test Dataset',
  198. name: '1-my-test-dataset',
  199. fields: memoryFields,
  200. records: [
  201. {id: 0, date: '2011-01-01', x: 1, y: 2, z: 3, country: 'DE', label: 'first'}
  202. , {id: 1, date: '2011-02-03', x: 2, y: 4, z: 6, country: 'UK', label: 'second'}
  203. , {id: 2, date: '2011-04-05', x: 3, y: 6, z: 9, country: 'US', label: 'third'}
  204. , {id: 3, date: '2011-06-07', x: 4, y: 8, z: 12, country: 'UK', label: 'fourth'}
  205. , {id: 4, date: '2011-08-09', x: 5, y: 10, z: 15, country: 'UK', label: 'fifth'}
  206. , {id: 5, date: '2011-10-11', x: 6, y: 12, z: 18, country: 'DE', label: 'sixth'}
  207. ]
  208. });
  209. dataset.fetch();
  210. return dataset;
  211. }
  212. test('basics', function () {
  213. var dataset = makeBackendDataset();
  214. expect(3);
  215. // convenience for tests - get the data that should get changed
  216. var data = dataset._store;
  217. dataset.fetch().then(function(datasetAgain) {
  218. equal(dataset.get('name'), memoryData.metadata.name);
  219. deepEqual(_.pluck(dataset.fields.toJSON(), 'id'), _.pluck(data.fields, 'id'));
  220. equal(dataset.recordCount, 6);
  221. });
  222. });
  223. test('query', function () {
  224. var dataset = makeBackendDataset();
  225. // convenience for tests - get the data that should get changed
  226. var data = dataset._store.data;
  227. var dataset = makeBackendDataset();
  228. var queryObj = {
  229. size: 4
  230. , from: 2
  231. };
  232. dataset.query(queryObj).then(function(recordList) {
  233. deepEqual(recordList.models[0].toJSON(), data[2]);
  234. });
  235. });
  236. test('query sort', function () {
  237. var dataset = makeBackendDataset();
  238. // convenience for tests - get the data that should get changed
  239. var data = dataset._store.data;
  240. var queryObj = {
  241. sort: [
  242. {field: 'y', order: 'desc'}
  243. ]
  244. };
  245. dataset.query(queryObj).then(function() {
  246. var doc0 = dataset.records.models[0].toJSON();
  247. equal(doc0.x, 6);
  248. });
  249. });
  250. test('query string', function () {
  251. var dataset = makeBackendDataset();
  252. dataset.fetch();
  253. dataset.query({q: 'UK'}).then(function() {
  254. equal(dataset.records.length, 3);
  255. deepEqual(dataset.records.pluck('country'), ['UK', 'UK', 'UK']);
  256. });
  257. dataset.query({q: 'UK 6'}).then(function() {
  258. equal(dataset.records.length, 2);
  259. deepEqual(dataset.records.models[0].id, 1);
  260. });
  261. });
  262. test('filters', function () {
  263. var dataset = makeBackendDataset();
  264. dataset.queryState.addFilter({type: 'term', field: 'country', term: 'UK'});
  265. dataset.query().then(function() {
  266. equal(dataset.records.length, 3);
  267. deepEqual(dataset.records.pluck('country'), ['UK', 'UK', 'UK']);
  268. });
  269. dataset = makeBackendDataset();
  270. dataset.queryState.addFilter({type: 'range', field: 'date', start: '2011-01-01', stop: '2011-05-01'});
  271. dataset.query().then(function() {
  272. equal(dataset.records.length, 3);
  273. deepEqual(dataset.records.pluck('date'), ['2011-01-01','2011-02-03','2011-04-05']);
  274. });
  275. dataset = makeBackendDataset();
  276. dataset.queryState.addFilter({type: 'range', field: 'z', start: '0', stop: '10'});
  277. dataset.query().then(function() {
  278. equal(dataset.records.length, 3);
  279. deepEqual(dataset.records.pluck('z'), [3,6,9]);
  280. });
  281. });
  282. test('facet', function () {
  283. var dataset = makeBackendDataset();
  284. dataset.queryState.addFacet('country');
  285. dataset.query().then(function() {
  286. equal(dataset.facets.length, 1);
  287. var exp = [
  288. {
  289. term: 'UK',
  290. count: 3
  291. },
  292. {
  293. term: 'DE',
  294. count: 2
  295. },
  296. {
  297. term: 'US',
  298. count: 1
  299. }
  300. ];
  301. deepEqual(dataset.facets.get('country').toJSON().terms, exp);
  302. });
  303. });
  304. test('update and delete', function () {
  305. var dataset = makeBackendDataset();
  306. // convenience for tests - get the data that should get changed
  307. var data = dataset._store;
  308. dataset.query().then(function(docList) {
  309. equal(docList.length, Math.min(100, data.data.length));
  310. var doc1 = docList.models[0];
  311. deepEqual(doc1.toJSON(), data.data[0]);
  312. // Test UPDATE
  313. var newVal = 10;
  314. doc1.set({x: newVal});
  315. doc1.save();
  316. equal(dataset._changes.updates[0].x, newVal);
  317. doc1.destroy();
  318. deepEqual(dataset._changes.deletes[0], doc1.toJSON());
  319. dataset.save().then(function() {
  320. equal(data.data.length, 5);
  321. equal(data.data[0].x, memoryData.records[1].x);
  322. });
  323. });
  324. });
  325. })(this.jQuery);