query_ctrl_specs.ts 6.7 KB

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