analyzer.or.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 OR', function() {
  6. it('should generate a valid group', function() {
  7. var tokens = tokenize({
  8. select: ['*'],
  9. where: {
  10. or: [
  11. {
  12. id: { '>': 10 }
  13. },
  14. {
  15. name: 'Tester'
  16. }
  17. ]
  18. },
  19. from: 'users'
  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: 'WHERE' },
  29. [
  30. { type: 'KEY', value: 'id' },
  31. { type: 'OPERATOR', value: '>' },
  32. { type: 'VALUE', value: 10 }
  33. ],
  34. [
  35. { type: 'KEY', value: 'name' },
  36. { type: 'VALUE', value: 'Tester' }
  37. ]
  38. ],
  39. [
  40. { type: 'IDENTIFIER', value: 'FROM' },
  41. { type: 'VALUE', value: 'users' }
  42. ]
  43. ]);
  44. });
  45. it('should generate a valid group when using nested OR conditions', function() {
  46. var tokens = tokenize({
  47. select: ['*'],
  48. where: {
  49. or: [
  50. {
  51. or: [
  52. { id: 1 },
  53. { id: { '>': 10 } }
  54. ]
  55. },
  56. {
  57. name: 'Tester'
  58. }
  59. ]
  60. },
  61. from: 'users'
  62. });
  63. var result = Analyzer(tokens);
  64. assert.deepEqual(result, [
  65. [
  66. { type: 'IDENTIFIER', value: 'SELECT' },
  67. { type: 'VALUE', value: '*' }
  68. ],
  69. [
  70. { type: 'IDENTIFIER', value: 'WHERE' },
  71. [
  72. [
  73. { type: 'KEY', value: 'id' },
  74. { type: 'VALUE', value: 1 }
  75. ],
  76. [
  77. { type: 'KEY', value: 'id' },
  78. { type: 'OPERATOR', value: '>' },
  79. { type: 'VALUE', value: 10 }
  80. ]
  81. ],
  82. [
  83. { type: 'KEY', value: 'name' },
  84. { type: 'VALUE', value: 'Tester' }
  85. ]
  86. ],
  87. [
  88. { type: 'IDENTIFIER', value: 'FROM' },
  89. { type: 'VALUE', value: 'users' }
  90. ]
  91. ]);
  92. });
  93. });
  94. });