query_ctrl_specs.ts 6.6 KB

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