query_ctrl_specs.ts 6.9 KB

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