query_ctrl_specs.ts 6.8 KB

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