analyzer.where.simple.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. var Analyzer = require('../../../index').query.analyzer;
  2. var tokenize = require('../../support/tokenize');
  3. var assert = require('assert');
  4. describe('Analyzer ::', function() {
  5. describe('Simple WHERE statements', function() {
  6. it('should generate a valid group', function() {
  7. var tokens = tokenize({
  8. select: ['id'],
  9. where: {
  10. and: [
  11. {
  12. firstName: 'Test'
  13. },
  14. {
  15. lastName: 'User'
  16. }
  17. ]
  18. },
  19. from: 'users'
  20. });
  21. var result = Analyzer(tokens);
  22. assert.deepEqual(result, [
  23. [
  24. { type: 'IDENTIFIER', value: 'SELECT' },
  25. { type: 'VALUE', value: 'id' }
  26. ],
  27. [
  28. { type: 'IDENTIFIER', value: 'WHERE' },
  29. { type: 'CONDITION', value: 'AND' },
  30. [
  31. { type: 'KEY', value: 'firstName' },
  32. { type: 'VALUE', value: 'Test' },
  33. ],
  34. [
  35. { type: 'KEY', value: 'lastName' },
  36. { type: 'VALUE', value: 'User' }
  37. ]
  38. ],
  39. [
  40. { type: 'IDENTIFIER', value: 'FROM' },
  41. { type: 'VALUE', value: 'users' }
  42. ]
  43. ]);
  44. });
  45. it('should generate a valid group when used with operators', function() {
  46. var tokens = tokenize({
  47. select: ['*'],
  48. where: {
  49. and: [
  50. {
  51. votes: {
  52. '>': 100
  53. }
  54. }
  55. ]
  56. },
  57. from: 'users'
  58. });
  59. var result = Analyzer(tokens);
  60. assert.deepEqual(result, [
  61. [
  62. { type: 'IDENTIFIER', value: 'SELECT' },
  63. { type: 'VALUE', value: '*' }
  64. ],
  65. [
  66. { type: 'IDENTIFIER', value: 'WHERE' },
  67. { type: 'CONDITION', value: 'AND' },
  68. [
  69. { type: 'KEY', value: 'votes' },
  70. { type: 'OPERATOR', value: '>' },
  71. { type: 'VALUE', value: 100 }
  72. ]
  73. ],
  74. [
  75. { type: 'IDENTIFIER', value: 'FROM' },
  76. { type: 'VALUE', value: 'users' }
  77. ]
  78. ]);
  79. });
  80. it('should generate a valid group when used with multiple operators', function() {
  81. var tokens = tokenize({
  82. select: ['*'],
  83. where: {
  84. and: [
  85. {
  86. votes: {
  87. '>': 100
  88. }
  89. },
  90. {
  91. votes: {
  92. '<': 200
  93. }
  94. }
  95. ]
  96. },
  97. from: 'users'
  98. });
  99. var result = Analyzer(tokens);
  100. assert.deepEqual(result, [
  101. [
  102. { type: 'IDENTIFIER', value: 'SELECT' },
  103. { type: 'VALUE', value: '*' }
  104. ],
  105. [
  106. { type: 'IDENTIFIER', value: 'WHERE' },
  107. { type: 'CONDITION', value: 'AND' },
  108. [
  109. { type: 'KEY', value: 'votes' },
  110. { type: 'OPERATOR', value: '>' },
  111. { type: 'VALUE', value: 100 }
  112. ],
  113. [
  114. { type: 'KEY', value: 'votes' },
  115. { type: 'OPERATOR', value: '<' },
  116. { type: 'VALUE', value: 200 }
  117. ]
  118. ],
  119. [
  120. { type: 'IDENTIFIER', value: 'FROM' },
  121. { type: 'VALUE', value: 'users' }
  122. ]
  123. ]);
  124. });
  125. it('should generate a valid group when used with multiple columns and operators', function() {
  126. var tokens = tokenize({
  127. select: ['*'],
  128. where: {
  129. and: [
  130. {
  131. votes: {
  132. '>': 100
  133. }
  134. },
  135. {
  136. age: {
  137. '<': 50
  138. }
  139. }
  140. ]
  141. },
  142. from: 'users'
  143. });
  144. var result = Analyzer(tokens);
  145. assert.deepEqual(result, [
  146. [
  147. { type: 'IDENTIFIER', value: 'SELECT' },
  148. { type: 'VALUE', value: '*' }
  149. ],
  150. [
  151. { type: 'IDENTIFIER', value: 'WHERE' },
  152. { type: 'CONDITION', value: 'AND' },
  153. [
  154. { type: 'KEY', value: 'votes' },
  155. { type: 'OPERATOR', value: '>' },
  156. { type: 'VALUE', value: 100 }
  157. ],
  158. [
  159. { type: 'KEY', value: 'age' },
  160. { type: 'OPERATOR', value: '<' },
  161. { type: 'VALUE', value: 50 }
  162. ]
  163. ],
  164. [
  165. { type: 'IDENTIFIER', value: 'FROM' },
  166. { type: 'VALUE', value: 'users' }
  167. ]
  168. ]);
  169. });
  170. });
  171. });