t.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*jslint node : true, continue : true,
  2. devel : true, indent : 2, maxerr : 50,
  3. newcap : true, nomen : true, plusplus : true,
  4. regexp : true, vars : false, white : true
  5. unparam : true, sloppy : true, todo : true
  6. */
  7. /*global global, module, require, $, KI, document, window */
  8. // global global, module, require, $, KI, document, window
  9. 'use strict';
  10. // BEGIN module-scope vars
  11. var
  12. TAFFY = require( '../taffy.js' ).taffy,
  13. // Load libraries
  14. // Declare data sources
  15. cfgMap = {
  16. friend_list : [
  17. {"id":1,"gender":"M","first":"John","last":"Smith",
  18. "city":"Seattle, WA","status":"Active"},
  19. {"id":2,"gender":"F","first":"Kelly","last":"Ruth",
  20. "city":"Dallas, TX","status":"Active"},
  21. {"id":3,"gender":"M","first":"Jeff","last":"Stevenson",
  22. "city":"Washington, D.C.","status":"Active"},
  23. {"id":4,"gender":"F","first":"Jennifer","last":"Gill",
  24. "city":"Seattle, WA","status":"Active"}
  25. ]
  26. },
  27. // Declare utlities
  28. getVarType, cloneFriendList,
  29. // Declare init used to reset state for tests
  30. // Declare tests
  31. testSmoke, testShowBug, testDummy
  32. ;
  33. // END module-scope vars
  34. // BEGIN public utility /getVarType/
  35. // Returns 'Function', 'Object', 'Array',
  36. // 'String', 'Number', 'Boolean', or 'Undefined',
  37. getVarType = function ( data ) {
  38. if (undefined === data ){ return 'Undefined'; }
  39. if (data === null ){ return 'Null'; }
  40. return {}.toString.call(data).slice(8, -1);
  41. };
  42. // END public utility /getVarType/
  43. // BEGIN cloneFriendList
  44. cloneFriendList = function () { return cfgMap.friend_list.slice(); };
  45. // END cloneFriendList
  46. // BEGIN testSmoke
  47. testSmoke = function ( test_obj ) {
  48. var
  49. friend_db, query_list, query_count, initial_list, expect_map,
  50. idx, bit_list, key_name, val_data, expect_data, actual_str, msg_str,
  51. partial, chain_list, chain_count, i, chain_map
  52. ;
  53. friend_db = TAFFY( cloneFriendList() );
  54. // Fast clone; see stackoverflow.com/questions/122102/5344074
  55. initial_list = JSON.parse(JSON.stringify(friend_db().get()));
  56. // query data
  57. //
  58. query_list = [
  59. [ 'filter_city', [ { city : "Seattle, WA"}, { key: 'get' } ] ],
  60. [ 'filter_id_num', [ { id : 1 }, { key : 'get' } ] ],
  61. [ 'filter_id_str', [ { id : '1'}, { key : 'get' } ] ],
  62. [ 'filter_name', [ { first : 'John', last : 'Smith' }, { key : 'get' } ] ],
  63. [ 'kelly_by_id', [ { id : 2}, { key : 'first' } ] ],
  64. [ 'kelly_last_name', [ { id : 2}, { key : 'first' }, { _xprop : 'last' } ] ],
  65. [ 'id_list', [ null, { key : 'select', val : 'id' } ] ],
  66. [ 'city_list', [ null, { key : 'distinct', val : 'city' } ] ],
  67. [ 'filter_001', [ null, { key : 'filter', val : { city : "Seattle, WA"} }, { key : 'get' } ] ],
  68. // should match initial order
  69. [ 'order_001', [ null, { key : 'get' } ] ],
  70. [ 'order_002', [ null, { key : 'order', val : 'gender' }, { key : 'get' } ] ],
  71. [ 'order_003', [ null, { key : 'order', val : 'gender desc' }, { key : 'get' } ] ],
  72. [ 'order_004', [ null, { key : 'order', val : 'gender, last' }, { key : 'get' } ] ],
  73. [ 'order_005', [ null, { key : 'order', val : 'gender desc, last desc' }, { key : 'get' } ] ],
  74. // TODO: asec is not really supported, but is default sort.
  75. // Also should the abbreviation be 'asc' or similar?
  76. [ 'order_006', [ null, { key : 'order', val : 'first' }, { key : 'get' } ] ],
  77. [ 'order_007', [ null, { key : 'order', val : 'last' }, { key : 'get' } ] ],
  78. [ 'order_008', [ null, { key : 'order', val : 'first desc' }, { key : 'get' } ] ],
  79. [ 'order_009', [ null, { key : 'order', val : 'last desc' }, { key : 'get' } ] ],
  80. // should match initial order
  81. [ 'order_001', [ null, { key : 'get' } ] ],
  82. [ 'sort_001_002', [ { _xsort : 'last' }, { key : 'get' } ] ],
  83. // should match changed order
  84. [ 'sort_001_002', [ null, { key : 'get'} ] ],
  85. [ 'get_ruth', [ { "last" : "Ruth" }, { key : 'update', val : { first : 'KelBel' } }, { key : 'first' } ] ]
  86. ];
  87. query_count = query_list.length;
  88. test_obj.expect( query_count );
  89. expect_map = {
  90. filter_city : [{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}],
  91. filter_id_num : [{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true}],
  92. filter_id_str : [],
  93. filter_name : [{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true}],
  94. kelly_by_id : {"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},
  95. kelly_last_name : 'Ruth',
  96. id_list : [1,2,3,4],
  97. city_list : ["Seattle, WA","Dallas, TX","Washington, D.C."],
  98. filter_001 : [{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}],
  99. order_001 : initial_list,
  100. order_002 : [{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true}],
  101. order_003 : [{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}],
  102. order_004 : [{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true}],
  103. order_005 : [{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}],
  104. order_006 : [{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true}],
  105. order_007 : [{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true}],
  106. order_008 : [{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true}],
  107. order_009 : [{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true}],
  108. order_010 : initial_list,
  109. sort_001_002 : [{"id":4,"gender":"F","first":"Jennifer","last":"Gill","city":"Seattle, WA","status":"Active","___id":"T000002R000005","___s":true},{"id":2,"gender":"F","first":"Kelly","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true},{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","status":"Active","___id":"T000002R000002","___s":true},{"id":3,"gender":"M","first":"Jeff","last":"Stevenson","city":"Washington, D.C.","status":"Active","___id":"T000002R000004","___s":true}],
  110. get_ruth : {"id":2,"gender":"F","first":"KelBel","last":"Ruth","city":"Dallas, TX","status":"Active","___id":"T000002R000003","___s":true}
  111. };
  112. for ( idx = 0; idx < query_count; idx++ ) {
  113. bit_list = query_list[ idx ];
  114. key_name = bit_list[ 0 ];
  115. val_data = bit_list[ 1 ];
  116. expect_data = expect_map[ key_name ] || '';
  117. chain_list = val_data;
  118. chain_count = chain_list.length;
  119. _PARTIAL_:
  120. for ( i = 0; i < chain_count; i++ ) {
  121. chain_map = chain_list[ i ];
  122. if ( i === 0 ) {
  123. if ( ! chain_map ) {
  124. partial = friend_db();
  125. }
  126. else if ( chain_map._xsort ) {
  127. friend_db.sort( chain_map._xsort );
  128. partial = friend_db();
  129. }
  130. else {
  131. partial = friend_db( chain_map );
  132. }
  133. }
  134. else {
  135. if ( chain_map._xprop ) {
  136. partial = partial[ chain_map._xprop ];
  137. }
  138. else if ( chain_map.val ) {
  139. partial = partial[ chain_map.key ]( chain_map.val );
  140. }
  141. else {
  142. partial = partial[ chain_map.key ]();
  143. }
  144. }
  145. }
  146. actual_str = JSON.stringify( partial );
  147. msg_str = actual_str + ' === ' + JSON.stringify( expect_data );
  148. test_obj.deepEqual( partial, expect_data, msg_str );
  149. }
  150. test_obj.done();
  151. };
  152. // END testSmoke
  153. // BEGIN testShowBug
  154. testShowBug = function ( test_obj ) {
  155. var friend_db = TAFFY( cloneFriendList() );
  156. test_obj.expect( 0 );
  157. friend_db().each(function ( row_map, idx ) {
  158. if ( row_map.city === 'Seattle, WA' ){
  159. row_map.city = 'WallaWalla, WA';
  160. }
  161. });
  162. // console.log( 'Example filter bug - rows changed without using '
  163. // + '.update() will filter by their original values. '
  164. // );
  165. // console.log( 'We expect 0 rows, but get 2... ' );
  166. // console.log(
  167. // friend_db().filter({ city : 'Seattle, WA'}).get()
  168. // );
  169. // console.log( '...even though the city has changed in the collection.' );
  170. // console.log( friend_db().get() );
  171. // console.log( '' );
  172. //
  173. // console.log( 'Example filter when .update() is used.');
  174. // friend_db = TAFFY( cloneFriendList() );
  175. //
  176. // friend_db({ city : 'Seattle, WA' })
  177. // .update({ city : 'WallaWalla, WA' });
  178. //
  179. // console.log( 'now we get the correct response (0 rows) ...' );
  180. // console.log(
  181. // friend_db().filter({ city : 'Seattle, WA'}).get()
  182. // );
  183. // console.log( friend_db().get() );
  184. // console.log( '... that reflects the taffy collection.' );
  185. test_obj.done();
  186. };
  187. // END testShowBug
  188. testDummy = function ( test_obj ) {
  189. test_obj.expect( 0 );
  190. test_obj.done();
  191. // See http://stackoverflow.com/questions/10952806
  192. // Suprisingly, a non-zero exit value (echo $?) is provided
  193. // when the tests do not pass, which is awesome!
  194. //
  195. setTimeout( function (){ process.exit(0); }, 100 );
  196. };
  197. module.exports = {
  198. testSmoke : testSmoke,
  199. testShowBug : testShowBug,
  200. testDummy : testDummy
  201. };