query_ctrl_specs.ts 6.7 KB

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