analyzer.union.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. var Analyzer = require('../../../index').query.analyzer;
  2. var tokenize = require('../../support/tokenize');
  3. var assert = require('assert');
  4. describe('Analyzer ::', function() {
  5. describe('UNION statements', function() {
  6. it('should generate a valid group for UNION statements', function() {
  7. var tokens = tokenize({
  8. select: ['*'],
  9. from: 'users',
  10. where: {
  11. and: [
  12. {
  13. firstName: 'Bob'
  14. }
  15. ]
  16. },
  17. union: [
  18. {
  19. select: ['*'],
  20. from: 'users',
  21. where: {
  22. and: [
  23. {
  24. lastName: 'Smith'
  25. }
  26. ]
  27. }
  28. },
  29. {
  30. select: ['*'],
  31. from: 'users',
  32. where: {
  33. and: [
  34. {
  35. middleName: 'Allen'
  36. }
  37. ]
  38. }
  39. }
  40. ]
  41. });
  42. var result = Analyzer(tokens);
  43. assert.deepEqual(result, [
  44. [
  45. { type: 'IDENTIFIER', value: 'SELECT' },
  46. { type: 'VALUE', value: '*' }
  47. ],
  48. [
  49. { type: 'IDENTIFIER', value: 'FROM' },
  50. { type: 'VALUE', value: 'users' }
  51. ],
  52. [
  53. { type: 'IDENTIFIER', value: 'WHERE' },
  54. { type: 'CONDITION', value: 'AND' },
  55. [
  56. { type: 'KEY', value: 'firstName' },
  57. { type: 'VALUE', value: 'Bob' }
  58. ]
  59. ],
  60. [
  61. { type: 'UNION', value: 'UNION' },
  62. [
  63. [
  64. [
  65. { type: 'IDENTIFIER', value: 'SELECT' },
  66. { type: 'VALUE', value: '*' }
  67. ],
  68. [
  69. { type: 'IDENTIFIER', value: 'FROM' },
  70. { type: 'VALUE', value: 'users' }
  71. ],
  72. [
  73. { type: 'IDENTIFIER', value: 'WHERE' },
  74. { type: 'CONDITION', value: 'AND' },
  75. [
  76. { type: 'KEY', value: 'lastName' },
  77. { type: 'VALUE', value: 'Smith' }
  78. ]
  79. ]
  80. ],
  81. [
  82. [
  83. { type: 'IDENTIFIER', value: 'SELECT' },
  84. { type: 'VALUE', value: '*' }
  85. ],
  86. [
  87. { type: 'IDENTIFIER', value: 'FROM' },
  88. { type: 'VALUE', value: 'users' }
  89. ],
  90. [
  91. { type: 'IDENTIFIER', value: 'WHERE' },
  92. { type: 'CONDITION', value: 'AND' },
  93. [
  94. { type: 'KEY', value: 'middleName' },
  95. { type: 'VALUE', value: 'Allen' }
  96. ]
  97. ]
  98. ]
  99. ]
  100. ]
  101. ]);
  102. });
  103. });
  104. });