tokenizer.union.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var Tokenizer = require('../../../index').query.tokenizer;
  2. var assert = require('assert');
  3. describe('Tokenizer ::', function() {
  4. describe('Union ::', function() {
  5. it('should generate a valid token array for a UNION array', function() {
  6. var result = Tokenizer({
  7. select: ['*'],
  8. from: 'users',
  9. where: {
  10. and: [
  11. {
  12. firstName: 'Bob'
  13. }
  14. ]
  15. },
  16. union: [
  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: 'UNION' },
  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: 'UNION' }
  94. ]);
  95. });
  96. });
  97. });