analyzer.operators.test.js 6.2 KB

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