tokenizer.operators.test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var Tokenizer = require('../../../index').query.tokenizer;
  2. var assert = require('assert');
  3. describe('Tokenizer ::', function() {
  4. describe('Various Operators', function() {
  5. it('should generate a valid token array when LIKE is used', function() {
  6. var result = Tokenizer({
  7. select: ['*'],
  8. from: 'users',
  9. where: {
  10. or: [
  11. {
  12. name: {
  13. like: '%Test%'
  14. }
  15. },
  16. {
  17. id: {
  18. nin: [1, 2, 3]
  19. }
  20. }
  21. ]
  22. }
  23. });
  24. assert.deepEqual(result, [
  25. { type: 'IDENTIFIER', value: 'SELECT' },
  26. { type: 'VALUE', value: '*' },
  27. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  28. { type: 'IDENTIFIER', value: 'FROM' },
  29. { type: 'VALUE', value: 'users' },
  30. { type: 'ENDIDENTIFIER', value: 'FROM' },
  31. { type: 'IDENTIFIER', value: 'WHERE' },
  32. { type: 'CONDITION', value: 'OR' },
  33. { type: 'GROUP', value: 0 },
  34. { type: 'KEY', value: 'name' },
  35. { type: 'OPERATOR', value: 'like' },
  36. { type: 'VALUE', value: '%Test%' },
  37. { type: 'ENDOPERATOR', value: 'like' },
  38. { type: 'ENDGROUP', value: 0 },
  39. { type: 'GROUP', value: 1 },
  40. { type: 'KEY', value: 'id' },
  41. { type: 'CONDITION', value: 'NOTIN' },
  42. { type: 'VALUE', value: [1, 2, 3] },
  43. { type: 'ENDCONDITION', value: 'NOTIN' },
  44. { type: 'ENDGROUP', value: 1 },
  45. { type: 'ENDCONDITION', value: 'OR' },
  46. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  47. ]);
  48. });
  49. it('should generate a valid token array when != is used', function() {
  50. var result = Tokenizer({
  51. select: ['id'],
  52. from: 'users',
  53. where: {
  54. and: [
  55. {
  56. firstName: {
  57. '!=': 'Test'
  58. }
  59. },
  60. {
  61. lastName: {
  62. '!=': 'User'
  63. }
  64. }
  65. ]
  66. }
  67. });
  68. assert.deepEqual(result, [
  69. { type: 'IDENTIFIER', value: 'SELECT' },
  70. { type: 'VALUE', value: 'id' },
  71. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  72. { type: 'IDENTIFIER', value: 'FROM' },
  73. { type: 'VALUE', value: 'users' },
  74. { type: 'ENDIDENTIFIER', value: 'FROM' },
  75. { type: 'IDENTIFIER', value: 'WHERE' },
  76. { type: 'CONDITION', value: 'AND' },
  77. { type: 'GROUP', value: 0 },
  78. { type: 'KEY', value: 'firstName' },
  79. { type: 'OPERATOR', value: '!=' },
  80. { type: 'VALUE', value: 'Test' },
  81. { type: 'ENDOPERATOR', value: '!=' },
  82. { type: 'ENDGROUP', value: 0 },
  83. { type: 'GROUP', value: 1 },
  84. { type: 'KEY', value: 'lastName' },
  85. { type: 'OPERATOR', value: '!=' },
  86. { type: 'VALUE', value: 'User' },
  87. { type: 'ENDOPERATOR', value: '!=' },
  88. { type: 'ENDGROUP', value: 1 },
  89. { type: 'ENDCONDITION', value: 'AND' },
  90. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  91. ]);
  92. });
  93. it('should generate a valid token array when nested != statements are used', function() {
  94. var result = Tokenizer({
  95. select: ['*'],
  96. from: 'users',
  97. where: {
  98. or: [
  99. {
  100. or: [
  101. {
  102. id: {
  103. '!=': 1
  104. }
  105. },
  106. {
  107. id: {
  108. '>': 10
  109. }
  110. }
  111. ]
  112. },
  113. {
  114. name: {
  115. '!=': 'Tester'
  116. }
  117. }
  118. ]
  119. }
  120. });
  121. assert.deepEqual(result, [
  122. { type: 'IDENTIFIER', value: 'SELECT' },
  123. { type: 'VALUE', value: '*' },
  124. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  125. { type: 'IDENTIFIER', value: 'FROM' },
  126. { type: 'VALUE', value: 'users' },
  127. { type: 'ENDIDENTIFIER', value: 'FROM' },
  128. { type: 'IDENTIFIER', value: 'WHERE' },
  129. { type: 'CONDITION', value: 'OR' },
  130. { type: 'GROUP', value: 0 },
  131. { type: 'CONDITION', value: 'OR' },
  132. { type: 'GROUP', value: 0 },
  133. { type: 'KEY', value: 'id' },
  134. { type: 'OPERATOR', value: '!=' },
  135. { type: 'VALUE', value: 1 },
  136. { type: 'ENDOPERATOR', value: '!=' },
  137. { type: 'ENDGROUP', value: 0 },
  138. { type: 'GROUP', value: 1 },
  139. { type: 'KEY', value: 'id' },
  140. { type: 'OPERATOR', value: '>' },
  141. { type: 'VALUE', value: 10 },
  142. { type: 'ENDOPERATOR', value: '>' },
  143. { type: 'ENDGROUP', value: 1 },
  144. { type: 'ENDCONDITION', value: 'OR' },
  145. { type: 'ENDGROUP', value: 0 },
  146. { type: 'GROUP', value: 1 },
  147. { type: 'KEY', value: 'name' },
  148. { type: 'OPERATOR', value: '!=' },
  149. { type: 'VALUE', value: 'Tester' },
  150. { type: 'ENDOPERATOR', value: '!=' },
  151. { type: 'ENDGROUP', value: 1 },
  152. { type: 'ENDCONDITION', value: 'OR' },
  153. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  154. ]);
  155. });
  156. it('should generate a valid token array when multiple operators are used', function() {
  157. var result = Tokenizer({
  158. select: ['*'],
  159. from: 'users',
  160. where: {
  161. or: [
  162. {
  163. name: 'John'
  164. },
  165. {
  166. votes: {
  167. '>': 100
  168. },
  169. title: {
  170. '!=': 'Admin'
  171. }
  172. }
  173. ]
  174. }
  175. });
  176. assert.deepEqual(result, [
  177. { type: 'IDENTIFIER', value: 'SELECT' },
  178. { type: 'VALUE', value: '*' },
  179. { type: 'ENDIDENTIFIER', value: 'SELECT' },
  180. { type: 'IDENTIFIER', value: 'FROM' },
  181. { type: 'VALUE', value: 'users' },
  182. { type: 'ENDIDENTIFIER', value: 'FROM' },
  183. { type: 'IDENTIFIER', value: 'WHERE' },
  184. { type: 'CONDITION', value: 'OR' },
  185. { type: 'GROUP', value: 0 },
  186. { type: 'KEY', value: 'name' },
  187. { type: 'VALUE', value: 'John' },
  188. { type: 'ENDGROUP', value: 0 },
  189. { type: 'GROUP', value: 1 },
  190. { type: 'KEY', value: 'votes' },
  191. { type: 'OPERATOR', value: '>' },
  192. { type: 'VALUE', value: 100 },
  193. { type: 'ENDOPERATOR', value: '>' },
  194. { type: 'KEY', value: 'title' },
  195. { type: 'OPERATOR', value: '!=' },
  196. { type: 'VALUE', value: 'Admin' },
  197. { type: 'ENDOPERATOR', value: '!=' },
  198. { type: 'ENDGROUP', value: 1 },
  199. { type: 'ENDCONDITION', value: 'OR' },
  200. { type: 'ENDIDENTIFIER', value: 'WHERE' }
  201. ]);
  202. });
  203. });
  204. });