query_ctrl_specs.ts 6.8 KB

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