influxdbQueryCtrl-specs.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 add plus button for another filter', function() {
  51. expect(ctx.scope.tagSegments[3].fake).to.be(true);
  52. });
  53. });
  54. describe('when last tag value segment is updated to regex', function() {
  55. beforeEach(function() {
  56. ctx.scope.init();
  57. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button'}, 0);
  58. ctx.scope.tagSegmentUpdated({value: '/server.*/', type: 'value'}, 2);
  59. });
  60. it('should update operator', function() {
  61. expect(ctx.scope.tagSegments[1].value).to.be('=~');
  62. });
  63. });
  64. describe('when second tag key is added', function() {
  65. beforeEach(function() {
  66. ctx.scope.init();
  67. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  68. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  69. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  70. });
  71. it('should update tag key', function() {
  72. expect(ctx.scope.target.tags[1].key).to.be('key2');
  73. });
  74. it('should add AND segment', function() {
  75. expect(ctx.scope.tagSegments[3].value).to.be('AND');
  76. });
  77. });
  78. describe('when condition is changed', function() {
  79. beforeEach(function() {
  80. ctx.scope.init();
  81. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  82. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  83. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  84. ctx.scope.tagSegmentUpdated({value: 'OR', type: 'condition'}, 3);
  85. });
  86. it('should update tag condition', function() {
  87. expect(ctx.scope.target.tags[1].condition).to.be('OR');
  88. });
  89. it('should update AND segment', function() {
  90. expect(ctx.scope.tagSegments[3].value).to.be('OR');
  91. expect(ctx.scope.tagSegments.length).to.be(7);
  92. });
  93. });
  94. describe('when deleting first tag filter after value is selected', function() {
  95. beforeEach(function() {
  96. ctx.scope.init();
  97. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  98. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  99. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 0);
  100. });
  101. it('should remove tags', function() {
  102. expect(ctx.scope.target.tags.length).to.be(0);
  103. });
  104. it('should remove all segment after 2 and replace with plus button', function() {
  105. expect(ctx.scope.tagSegments.length).to.be(1);
  106. expect(ctx.scope.tagSegments[0].type).to.be('plus-button');
  107. });
  108. });
  109. describe('when deleting second tag value before second tag value is complete', function() {
  110. beforeEach(function() {
  111. ctx.scope.init();
  112. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  113. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  114. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  115. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  116. });
  117. it('should remove all segment after 2 and replace with plus button', function() {
  118. expect(ctx.scope.tagSegments.length).to.be(4);
  119. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  120. });
  121. });
  122. describe('when deleting second tag value before second tag value is complete', function() {
  123. beforeEach(function() {
  124. ctx.scope.init();
  125. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  126. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  127. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  128. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  129. });
  130. it('should remove all segment after 2 and replace with plus button', function() {
  131. expect(ctx.scope.tagSegments.length).to.be(4);
  132. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  133. });
  134. });
  135. describe('when deleting second tag value after second tag filter is complete', function() {
  136. beforeEach(function() {
  137. ctx.scope.init();
  138. ctx.scope.tagSegmentUpdated({value: 'asd', type: 'plus-button' }, 0);
  139. ctx.scope.tagSegmentUpdated({value: 'server1', type: 'value'}, 2);
  140. ctx.scope.tagSegmentUpdated({value: 'key2', type: 'plus-button'}, 3);
  141. ctx.scope.tagSegmentUpdated({value: 'value', type: 'value'}, 6);
  142. ctx.scope.tagSegmentUpdated(ctx.scope.removeTagFilterSegment, 4);
  143. });
  144. it('should remove all segment after 2 and replace with plus button', function() {
  145. expect(ctx.scope.tagSegments.length).to.be(4);
  146. expect(ctx.scope.tagSegments[3].type).to.be('plus-button');
  147. });
  148. });
  149. describe('when adding group by', function() {
  150. beforeEach(function() {
  151. ctx.scope.init();
  152. ctx.scope.groupByTagUpdated({value: 'host', type: 'plus-button' }, 0);
  153. });
  154. it('should add group by', function() {
  155. expect(ctx.scope.target.groupByTags.length).to.be(1);
  156. expect(ctx.scope.target.groupByTags[0]).to.be('host');
  157. });
  158. it('should add another plus button segment', function() {
  159. expect(ctx.scope.groupBySegments[1].type).to.be('plus-button');
  160. });
  161. });
  162. describe('when removing group by', function() {
  163. beforeEach(function() {
  164. ctx.scope.init();
  165. ctx.scope.groupByTagUpdated({value: 'host', type: 'plus-button' }, 0);
  166. ctx.scope.groupByTagUpdated(ctx.scope.removeGroupBySegment, 0);
  167. });
  168. it('should add group by', function() {
  169. expect(ctx.scope.target.groupByTags.length).to.be(0);
  170. });
  171. it('should remove segment', function() {
  172. expect(ctx.scope.groupBySegments.length).to.be(1);
  173. expect(ctx.scope.groupBySegments[0].type).to.be('plus-button');
  174. });
  175. });
  176. });
  177. });