influxdbQueryCtrl-specs.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. define([
  2. 'helpers',
  3. 'plugins/datasource/influxdb/queryCtrl'
  4. ], function(helpers) {
  5. 'use strict';
  6. describe('InfluxDBQueryCtrl', function() {
  7. var ctx = new helpers.ControllerTestContext();
  8. beforeEach(module('grafana.controllers'));
  9. beforeEach(ctx.providePhase());
  10. beforeEach(ctx.createControllerPhase('InfluxQueryCtrl'));
  11. beforeEach(function() {
  12. ctx.scope.target = {};
  13. ctx.scope.$parent = { get_data: sinon.spy() };
  14. ctx.scope.datasource = ctx.datasource;
  15. ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  16. });
  17. describe('init', function() {
  18. beforeEach(function() {
  19. ctx.scope.init();
  20. });
  21. it('should init tagSegments', function() {
  22. expect(ctx.scope.tagSegments.length).to.be(1);
  23. });
  24. it('should init measurementSegment', function() {
  25. expect(ctx.scope.measurementSegment.value).to.be('select measurement');
  26. });
  27. });
  28. describe('when first tag segment is updated', function() {
  29. beforeEach(function() {
  30. ctx.scope.init();
  31. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button'}, 0);
  32. });
  33. it('should update tag key', function() {
  34. expect(ctx.scope.target.tags[0].key).to.be('asd');
  35. expect(ctx.scope.tagSegments[0].type).to.be('key');
  36. });
  37. it('should add tagSegments', function() {
  38. expect(ctx.scope.tagSegments.length).to.be(3);
  39. });
  40. });
  41. describe('when last tag value segment is updated', function() {
  42. beforeEach(function() {
  43. ctx.scope.init();
  44. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button'}, 0);
  45. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  46. });
  47. it('should update tag value', function() {
  48. expect(ctx.scope.target.tags[0].value).to.be('server1');
  49. });
  50. it('should set tag operator', function() {
  51. expect(ctx.scope.target.tags[0].operator).to.be('=');
  52. });
  53. it('should add plus button for another filter', function() {
  54. expect(ctx.scope.tagSegments[3].fake).to.be(true);
  55. });
  56. });
  57. describe('when last tag value segment is updated to regex', function() {
  58. beforeEach(function() {
  59. ctx.scope.init();
  60. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button'}, 0);
  61. ctx.scope.tagSegmentUpdated({value: '/server.*/', type: 'value'}, 2);
  62. });
  63. it('should update operator', function() {
  64. expect(ctx.scope.tagSegments[1].value).to.be('=~');
  65. expect(ctx.scope.target.tags[0].operator).to.be('=~');
  66. });
  67. });
  68. describe('when second tag key is added', function() {
  69. beforeEach(function() {
  70. ctx.scope.init();
  71. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  72. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  73. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  74. });
  75. it('should update tag key', function() {
  76. expect(ctx.scope.target.tags[1].key).to.be('key2');
  77. });
  78. it('should add AND segment', function() {
  79. expect(ctx.scope.tagSegments[3].value).to.be('AND');
  80. });
  81. });
  82. describe('when condition is changed', function() {
  83. beforeEach(function() {
  84. ctx.scope.init();
  85. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  86. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  87. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  88. ctx.scope.tagSegmentUpdated({value: 'OR', type: 'condition'}, 3);
  89. });
  90. it('should update tag condition', function() {
  91. expect(ctx.scope.target.tags[1].condition).to.be('OR');
  92. });
  93. it('should update AND segment', function() {
  94. expect(ctx.scope.tagSegments[3].value).to.be('OR');
  95. expect(ctx.scope.tagSegments.length).to.be(7);
  96. });
  97. });
  98. describe('when deleting first tag filter after value is selected', function() {
  99. beforeEach(function() {
  100. ctx.scope.init();
  101. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  102. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  103. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 0);
  104. });
  105. it('should remove tags', function() {
  106. expect(ctx.scope.target.tags.length).to.be(0);
  107. });
  108. it('should remove all segment after 2 and replace with plus button', function() {
  109. expect(ctx.scope.tagSegments.length).to.be(1);
  110. expect(ctx.scope.tagSegments[0].type).to.be('plus-button');
  111. });
  112. });
  113. describe('when deleting second tag value before second tag value is complete', function() {
  114. beforeEach(function() {
  115. ctx.scope.init();
  116. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  117. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  118. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  119. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  120. });
  121. it('should remove all segment after 2 and replace with plus button', function() {
  122. expect(ctx.scope.tagSegments.length).to.be(4);
  123. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  124. });
  125. });
  126. describe('when deleting second tag value before second tag value is complete', function() {
  127. beforeEach(function() {
  128. ctx.scope.init();
  129. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  130. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  131. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  132. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  133. });
  134. it('should remove all segment after 2 and replace with plus button', function() {
  135. expect(ctx.scope.tagSegments.length).to.be(4);
  136. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  137. });
  138. });
  139. describe('when deleting second tag value after second tag filter is complete', function() {
  140. beforeEach(function() {
  141. ctx.scope.init();
  142. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  143. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  144. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  145. ctx.scope.tagSegmentUpdated({value: 'value', type: 'value'}, 6);
  146. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  147. });
  148. it('should remove all segment after 2 and replace with plus button', function() {
  149. expect(ctx.scope.tagSegments.length).to.be(4);
  150. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  151. });
  152. });
  153. describe('when adding group by', function() {
  154. beforeEach(function() {
  155. ctx.scope.init();
  156. ctx.scope.groupByTagUpdated({value: 'host', type: 'plus-button' }, 0);
  157. });
  158. it('should add group by', function() {
  159. expect(ctx.scope.target.groupByTags.length).to.be(1);
  160. expect(ctx.scope.target.groupByTags[0]).to.be('host');
  161. });
  162. it('should add another plus button segment', function() {
  163. expect(ctx.scope.groupBySegments[1].type).to.be('plus-button');
  164. });
  165. });
  166. describe('when removing group by', function() {
  167. beforeEach(function() {
  168. ctx.scope.init();
  169. ctx.scope.groupByTagUpdated({value: 'host', type: 'plus-button' }, 0);
  170. ctx.scope.groupByTagUpdated(ctx.scope.removeGroupBySegment, 0);
  171. });
  172. it('should add group by', function() {
  173. expect(ctx.scope.target.groupByTags.length).to.be(0);
  174. });
  175. it('should remove segment', function() {
  176. expect(ctx.scope.groupBySegments.length).to.be(1);
  177. expect(ctx.scope.groupBySegments[0].type).to.be('plus-button');
  178. });
  179. });
  180. });
  181. });