influx_query_specs.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  2. import InfluxQuery = require('../influx_query');
  3. describe('InfluxQuery', function() {
  4. describe('render series with mesurement only', function() {
  5. it('should generate correct query', function() {
  6. var query = new InfluxQuery({
  7. measurement: 'cpu',
  8. });
  9. var queryText = query.render();
  10. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
  11. });
  12. });
  13. describe('render series with math and alias', function() {
  14. it('should generate correct query', function() {
  15. var query = new InfluxQuery({
  16. measurement: 'cpu',
  17. select: [
  18. [
  19. {type: 'field', params: ['value']},
  20. {type: 'mean', params: []},
  21. {type: 'math', params: ['/100']},
  22. {type: 'alias', params: ['text']},
  23. ]
  24. ]
  25. });
  26. var queryText = query.render();
  27. expect(queryText).to.be('SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
  28. });
  29. });
  30. describe('render series without group by', function() {
  31. it('should generate correct query', function() {
  32. var query = new InfluxQuery({
  33. measurement: 'cpu',
  34. select: [[{type: 'field', params: ['value']}]],
  35. groupBy: [],
  36. });
  37. var queryText = query.render();
  38. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
  39. });
  40. });
  41. describe('render series without group by and fill', function() {
  42. it('should generate correct query', function() {
  43. var query = new InfluxQuery({
  44. measurement: 'cpu',
  45. select: [[{type: 'field', params: ['value']}]],
  46. groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
  47. });
  48. var queryText = query.render();
  49. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(0)');
  50. });
  51. });
  52. describe('when adding group by part', function() {
  53. it('should add tag before fill', function() {
  54. var query = new InfluxQuery({
  55. measurement: 'cpu',
  56. groupBy: [{type: 'time'}, {type: 'fill'}]
  57. });
  58. query.addGroupBy('tag(host)');
  59. expect(query.target.groupBy.length).to.be(3);
  60. expect(query.target.groupBy[1].type).to.be('tag');
  61. expect(query.target.groupBy[1].params[0]).to.be('host');
  62. expect(query.target.groupBy[2].type).to.be('fill');
  63. });
  64. it('should add tag last if no fill', function() {
  65. var query = new InfluxQuery({
  66. measurement: 'cpu',
  67. groupBy: []
  68. });
  69. query.addGroupBy('tag(host)');
  70. expect(query.target.groupBy.length).to.be(1);
  71. expect(query.target.groupBy[0].type).to.be('tag');
  72. });
  73. });
  74. describe('when adding select part', function() {
  75. it('should add mean after after field', function() {
  76. var query = new InfluxQuery({
  77. measurement: 'cpu',
  78. select: [[{type: 'field', params: ['value']}]]
  79. });
  80. query.addSelectPart(query.selectModels[0], 'mean');
  81. expect(query.target.select[0].length).to.be(2);
  82. expect(query.target.select[0][1].type).to.be('mean');
  83. });
  84. it('should replace sum by mean', function() {
  85. var query = new InfluxQuery({
  86. measurement: 'cpu',
  87. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  88. });
  89. query.addSelectPart(query.selectModels[0], 'sum');
  90. expect(query.target.select[0].length).to.be(2);
  91. expect(query.target.select[0][1].type).to.be('sum');
  92. });
  93. it('should add math before alias', function() {
  94. var query = new InfluxQuery({
  95. measurement: 'cpu',
  96. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
  97. });
  98. query.addSelectPart(query.selectModels[0], 'math');
  99. expect(query.target.select[0].length).to.be(4);
  100. expect(query.target.select[0][2].type).to.be('math');
  101. });
  102. it('should add math last', function() {
  103. var query = new InfluxQuery({
  104. measurement: 'cpu',
  105. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  106. });
  107. query.addSelectPart(query.selectModels[0], 'math');
  108. expect(query.target.select[0].length).to.be(3);
  109. expect(query.target.select[0][2].type).to.be('math');
  110. });
  111. it('should replace math', function() {
  112. var query = new InfluxQuery({
  113. measurement: 'cpu',
  114. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
  115. });
  116. query.addSelectPart(query.selectModels[0], 'math');
  117. expect(query.target.select[0].length).to.be(3);
  118. expect(query.target.select[0][2].type).to.be('math');
  119. });
  120. });
  121. });