query_ctrl_specs.ts 6.8 KB

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