tokenizer.where.simple.test.js 5.2 KB

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