analyzer.and.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var Analyzer = require('../../../index').query.analyzer;
  2. var tokenize = require('../../support/tokenize');
  3. var assert = require('assert');
  4. describe('Analyzer ::', function() {
  5. describe('Grouping statements with AND', function() {
  6. it('should generate a valid group when AND is used as an array', function() {
  7. var tokens = tokenize({
  8. select: ['*'],
  9. from: 'users',
  10. where: {
  11. and: [
  12. {
  13. firstName: 'foo'
  14. },
  15. {
  16. lastName: 'bar'
  17. }
  18. ]
  19. }
  20. });
  21. var result = Analyzer(tokens);
  22. assert.deepEqual(result, [
  23. [
  24. { type: 'IDENTIFIER', value: 'SELECT' },
  25. { type: 'VALUE', value: '*' }
  26. ],
  27. [
  28. { type: 'IDENTIFIER', value: 'FROM' },
  29. { type: 'VALUE', value: 'users' }
  30. ],
  31. [
  32. { type: 'IDENTIFIER', value: 'WHERE' },
  33. { type: 'CONDITION', value: 'AND' },
  34. [
  35. { type: 'KEY', value: 'firstName' },
  36. { type: 'VALUE', value: 'foo' }
  37. ],
  38. [
  39. { type: 'KEY', value: 'lastName' },
  40. { type: 'VALUE', value: 'bar' }
  41. ]
  42. ]
  43. ]);
  44. });
  45. it('should generate a valid group when using nested OR conditions', function() {
  46. var tokens = tokenize({
  47. select: ['*'],
  48. from: 'users',
  49. where: {
  50. and: [
  51. {
  52. or: [
  53. {
  54. firstName: 'John'
  55. },
  56. {
  57. lastName: 'Smith'
  58. }
  59. ]
  60. },
  61. {
  62. or: [
  63. {
  64. qty: {
  65. '>': 100
  66. }
  67. },
  68. {
  69. price: {
  70. '<': 10.00
  71. }
  72. }
  73. ]
  74. }
  75. ]
  76. }
  77. });
  78. var result = Analyzer(tokens);
  79. assert.deepEqual(result, [
  80. [
  81. { type: 'IDENTIFIER', value: 'SELECT' },
  82. { type: 'VALUE', value: '*' }
  83. ],
  84. [
  85. { type: 'IDENTIFIER', value: 'FROM' },
  86. { type: 'VALUE', value: 'users' }
  87. ],
  88. [
  89. { type: 'IDENTIFIER', value: 'WHERE' },
  90. { type: 'CONDITION', value: 'AND' },
  91. [
  92. [
  93. { type: 'KEY', value: 'firstName' },
  94. { type: 'VALUE', value: 'John' }
  95. ],
  96. [
  97. { type: 'KEY', value: 'lastName' },
  98. { type: 'VALUE', value: 'Smith' }
  99. ]
  100. ],
  101. [
  102. [
  103. { type: 'KEY', value: 'qty' },
  104. { type: 'OPERATOR', value: '>' },
  105. { type: 'VALUE', value: 100 }
  106. ],
  107. [
  108. { type: 'KEY', value: 'price' },
  109. { type: 'OPERATOR', value: '<' },
  110. { type: 'VALUE', value: 10.00 }
  111. ]
  112. ]
  113. ]
  114. ]);
  115. });
  116. });
  117. });