analyzer.aggregations.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var Analyzer = require('../../../index').query.analyzer;
  2. var tokenize = require('../../support/tokenize');
  3. var assert = require('assert');
  4. describe('Analyzer ::', function() {
  5. describe('Aggregations', function() {
  6. it('should generate a valid group when when GROUP BY is used', function() {
  7. var tokens = tokenize({
  8. select: ['*'],
  9. from: 'users',
  10. groupBy: 'count'
  11. });
  12. var result = Analyzer(tokens);
  13. assert.deepEqual(result, [
  14. [
  15. { type: 'IDENTIFIER', value: 'SELECT' },
  16. { type: 'VALUE', value: '*' }
  17. ],
  18. [
  19. { type: 'IDENTIFIER', value: 'FROM' },
  20. { type: 'VALUE', value: 'users' }
  21. ],
  22. [
  23. { type: 'IDENTIFIER', value: 'GROUPBY' },
  24. { type: 'VALUE', value: 'count' }
  25. ]
  26. ]);
  27. });
  28. it('should generate a valid group when when MIN is used', function() {
  29. var tokens = tokenize({
  30. min: 'active',
  31. from: 'users'
  32. });
  33. var result = Analyzer(tokens);
  34. assert.deepEqual(result, [
  35. [
  36. { type: 'IDENTIFIER', value: 'MIN' },
  37. { type: 'VALUE', value: 'active' }
  38. ],
  39. [
  40. { type: 'IDENTIFIER', value: 'FROM' },
  41. { type: 'VALUE', value: 'users' }
  42. ]
  43. ]);
  44. });
  45. it('should generate a valid group when when MAX is used', function() {
  46. var tokens = tokenize({
  47. max: 'active',
  48. from: 'users'
  49. });
  50. var result = Analyzer(tokens);
  51. assert.deepEqual(result, [
  52. [
  53. { type: 'IDENTIFIER', value: 'MAX' },
  54. { type: 'VALUE', value: 'active' }
  55. ],
  56. [
  57. { type: 'IDENTIFIER', value: 'FROM' },
  58. { type: 'VALUE', value: 'users' }
  59. ]
  60. ]);
  61. });
  62. it('should generate a valid group when when SUM is used', function() {
  63. var tokens = tokenize({
  64. sum: 'active',
  65. from: 'users'
  66. });
  67. var result = Analyzer(tokens);
  68. assert.deepEqual(result, [
  69. [
  70. { type: 'IDENTIFIER', value: 'SUM' },
  71. { type: 'VALUE', value: 'active' }
  72. ],
  73. [
  74. { type: 'IDENTIFIER', value: 'FROM' },
  75. { type: 'VALUE', value: 'users' }
  76. ]
  77. ]);
  78. });
  79. it('should generate a valid group when when AVG is used', function() {
  80. var tokens = tokenize({
  81. avg: 'active',
  82. from: 'users'
  83. });
  84. var result = Analyzer(tokens);
  85. assert.deepEqual(result, [
  86. [
  87. { type: 'IDENTIFIER', value: 'AVG' },
  88. { type: 'VALUE', value: 'active' }
  89. ],
  90. [
  91. { type: 'IDENTIFIER', value: 'FROM' },
  92. { type: 'VALUE', value: 'users' }
  93. ]
  94. ]);
  95. });
  96. });
  97. });