tokenizer.unionAll.test.js 5.2 KB

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