influx_query_specs.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
  2. import InfluxQuery from '../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('series with single tag only', function() {
  31. it('should generate correct query', function() {
  32. var query = new InfluxQuery({
  33. measurement: 'cpu',
  34. groupBy: [{type: 'time', params: ['auto']}],
  35. tags: [{key: 'hostname', value: 'server1'}]
  36. });
  37. var queryText = query.render();
  38. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' AND $timeFilter'
  39. + ' GROUP BY time($interval)');
  40. });
  41. it('should switch regex operator with tag value is regex', function() {
  42. var query = new InfluxQuery({
  43. measurement: 'cpu',
  44. groupBy: [{type: 'time', params: ['auto']}],
  45. tags: [{key: 'app', value: '/e.*/'}]
  46. });
  47. var queryText = query.render();
  48. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "app" =~ /e.*/ AND $timeFilter GROUP BY time($interval)');
  49. });
  50. });
  51. describe('series with multiple tags only', function() {
  52. it('should generate correct query', function() {
  53. var query = new InfluxQuery({
  54. measurement: 'cpu',
  55. groupBy: [{type: 'time', params: ['auto']}],
  56. tags: [{key: 'hostname', value: 'server1'}, {key: 'app', value: 'email', condition: "AND"}]
  57. });
  58. var queryText = query.render();
  59. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' AND "app" = \'email\' AND ' +
  60. '$timeFilter GROUP BY time($interval)');
  61. });
  62. });
  63. describe('series with tags OR condition', function() {
  64. it('should generate correct query', function() {
  65. var query = new InfluxQuery({
  66. measurement: 'cpu',
  67. groupBy: [{type: 'time', params: ['auto']}],
  68. tags: [{key: 'hostname', value: 'server1'}, {key: 'hostname', value: 'server2', condition: "OR"}]
  69. });
  70. var queryText = query.render();
  71. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' OR "hostname" = \'server2\' AND ' +
  72. '$timeFilter GROUP BY time($interval)');
  73. });
  74. });
  75. describe('series with groupByTag', function() {
  76. it('should generate correct query', function() {
  77. var query = new InfluxQuery({
  78. measurement: 'cpu',
  79. tags: [],
  80. groupBy: [{type: 'time', interval: 'auto'}, {type: 'tag', params: ['host']}],
  81. });
  82. var queryText = query.render();
  83. expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter ' +
  84. 'GROUP BY time($interval), "host"');
  85. });
  86. });
  87. describe('render series without group by', function() {
  88. it('should generate correct query', function() {
  89. var query = new InfluxQuery({
  90. measurement: 'cpu',
  91. select: [[{type: 'field', params: ['value']}]],
  92. groupBy: [],
  93. });
  94. var queryText = query.render();
  95. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
  96. });
  97. });
  98. describe('render series without group by and fill', function() {
  99. it('should generate correct query', function() {
  100. var query = new InfluxQuery({
  101. measurement: 'cpu',
  102. select: [[{type: 'field', params: ['value']}]],
  103. groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
  104. });
  105. var queryText = query.render();
  106. expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(0)');
  107. });
  108. });
  109. describe('when adding group by part', function() {
  110. it('should add tag before fill', function() {
  111. var query = new InfluxQuery({
  112. measurement: 'cpu',
  113. groupBy: [{type: 'time'}, {type: 'fill'}]
  114. });
  115. query.addGroupBy('tag(host)');
  116. expect(query.target.groupBy.length).to.be(3);
  117. expect(query.target.groupBy[1].type).to.be('tag');
  118. expect(query.target.groupBy[1].params[0]).to.be('host');
  119. expect(query.target.groupBy[2].type).to.be('fill');
  120. });
  121. it('should add tag last if no fill', function() {
  122. var query = new InfluxQuery({
  123. measurement: 'cpu',
  124. groupBy: []
  125. });
  126. query.addGroupBy('tag(host)');
  127. expect(query.target.groupBy.length).to.be(1);
  128. expect(query.target.groupBy[0].type).to.be('tag');
  129. });
  130. });
  131. describe('when adding select part', function() {
  132. it('should add mean after after field', function() {
  133. var query = new InfluxQuery({
  134. measurement: 'cpu',
  135. select: [[{type: 'field', params: ['value']}]]
  136. });
  137. query.addSelectPart(query.selectModels[0], 'mean');
  138. expect(query.target.select[0].length).to.be(2);
  139. expect(query.target.select[0][1].type).to.be('mean');
  140. });
  141. it('should replace sum by mean', function() {
  142. var query = new InfluxQuery({
  143. measurement: 'cpu',
  144. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  145. });
  146. query.addSelectPart(query.selectModels[0], 'sum');
  147. expect(query.target.select[0].length).to.be(2);
  148. expect(query.target.select[0][1].type).to.be('sum');
  149. });
  150. it('should add math before alias', function() {
  151. var query = new InfluxQuery({
  152. measurement: 'cpu',
  153. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
  154. });
  155. query.addSelectPart(query.selectModels[0], 'math');
  156. expect(query.target.select[0].length).to.be(4);
  157. expect(query.target.select[0][2].type).to.be('math');
  158. });
  159. it('should add math last', function() {
  160. var query = new InfluxQuery({
  161. measurement: 'cpu',
  162. select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
  163. });
  164. query.addSelectPart(query.selectModels[0], 'math');
  165. expect(query.target.select[0].length).to.be(3);
  166. expect(query.target.select[0][2].type).to.be('math');
  167. });
  168. it('should replace math', function() {
  169. var query = new InfluxQuery({
  170. measurement: 'cpu',
  171. select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
  172. });
  173. query.addSelectPart(query.selectModels[0], 'math');
  174. expect(query.target.select[0].length).to.be(3);
  175. expect(query.target.select[0][2].type).to.be('math');
  176. });
  177. });
  178. });