query_ctrl_specs.ts 6.5 KB

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