query_ctrl_specs.ts 6.6 KB

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