tokenizer.and.test.js 3.7 KB

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