query_ctrl_specs.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 gfunc from '../gfunc';
  5. import helpers from 'test/specs/helpers';
  6. import {GraphiteQueryCtrl} from '../query_ctrl';
  7. describe('GraphiteQueryCtrl', function() {
  8. var ctx = new helpers.ControllerTestContext();
  9. beforeEach(angularMocks.module('grafana.core'));
  10. beforeEach(angularMocks.module('grafana.controllers'));
  11. beforeEach(angularMocks.module('grafana.services'));
  12. beforeEach(angularMocks.module(function($compileProvider) {
  13. $compileProvider.preAssignBindingsEnabled(true);
  14. }));
  15. beforeEach(ctx.providePhase());
  16. beforeEach(angularMocks.inject(($rootScope, $controller, $q) => {
  17. ctx.$q = $q;
  18. ctx.scope = $rootScope.$new();
  19. ctx.target = {target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'};
  20. ctx.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
  21. ctx.panelCtrl = {panel: {}};
  22. ctx.panelCtrl = {
  23. panel: {
  24. targets: [ctx.target]
  25. }
  26. };
  27. ctx.panelCtrl.refresh = sinon.spy();
  28. ctx.ctrl = $controller(GraphiteQueryCtrl, {$scope: ctx.scope}, {
  29. panelCtrl: ctx.panelCtrl,
  30. datasource: ctx.datasource,
  31. target: ctx.target
  32. });
  33. ctx.scope.$digest();
  34. }));
  35. describe('init', function() {
  36. it('should validate metric key exists', function() {
  37. expect(ctx.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
  38. });
  39. it('should delete last segment if no metrics are found', function() {
  40. expect(ctx.ctrl.segments[2].value).to.be('select metric');
  41. });
  42. it('should parse expression and build function model', function() {
  43. expect(ctx.ctrl.functions.length).to.be(2);
  44. });
  45. });
  46. describe('when adding function', function() {
  47. beforeEach(function() {
  48. ctx.ctrl.target.target = 'test.prod.*.count';
  49. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  50. ctx.ctrl.parseTarget();
  51. ctx.ctrl.addFunction(gfunc.getFuncDef('aliasByNode'));
  52. });
  53. it('should add function with correct node number', function() {
  54. expect(ctx.ctrl.functions[0].params[0]).to.be(2);
  55. });
  56. it('should update target', function() {
  57. expect(ctx.ctrl.target.target).to.be('aliasByNode(test.prod.*.count, 2)');
  58. });
  59. it('should call refresh', function() {
  60. expect(ctx.panelCtrl.refresh.called).to.be(true);
  61. });
  62. });
  63. describe('when adding function before any metric segment', function() {
  64. beforeEach(function() {
  65. ctx.ctrl.target.target = '';
  66. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}]));
  67. ctx.ctrl.parseTarget();
  68. ctx.ctrl.addFunction(gfunc.getFuncDef('asPercent'));
  69. });
  70. it('should add function and remove select metric link', function() {
  71. expect(ctx.ctrl.segments.length).to.be(0);
  72. });
  73. });
  74. describe('when initalizing target without metric expression and only function', function() {
  75. beforeEach(function() {
  76. ctx.ctrl.target.target = 'asPercent(#A, #B)';
  77. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  78. ctx.ctrl.parseTarget();
  79. ctx.scope.$digest();
  80. });
  81. it('should not add select metric segment', function() {
  82. expect(ctx.ctrl.segments.length).to.be(0);
  83. });
  84. it('should add both series refs as params', function() {
  85. expect(ctx.ctrl.functions[0].params.length).to.be(2);
  86. });
  87. });
  88. describe('when initializing a target with single param func using variable', function() {
  89. beforeEach(function() {
  90. ctx.ctrl.target.target = 'movingAverage(prod.count, $var)';
  91. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  92. ctx.ctrl.parseTarget();
  93. });
  94. it('should add 2 segments', function() {
  95. expect(ctx.ctrl.segments.length).to.be(2);
  96. });
  97. it('should add function param', function() {
  98. expect(ctx.ctrl.functions[0].params.length).to.be(1);
  99. });
  100. });
  101. describe('when initalizing target without metric expression and function with series-ref', function() {
  102. beforeEach(function() {
  103. ctx.ctrl.target.target = 'asPercent(metric.node.count, #A)';
  104. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  105. ctx.ctrl.parseTarget();
  106. });
  107. it('should add segments', function() {
  108. expect(ctx.ctrl.segments.length).to.be(3);
  109. });
  110. it('should have correct func params', function() {
  111. expect(ctx.ctrl.functions[0].params.length).to.be(1);
  112. });
  113. });
  114. describe('when getting altSegments and metricFindQuery retuns empty array', function() {
  115. beforeEach(function() {
  116. ctx.ctrl.target.target = 'test.count';
  117. ctx.ctrl.datasource.metricFindQuery.returns(ctx.$q.when([]));
  118. ctx.ctrl.parseTarget();
  119. ctx.ctrl.getAltSegments(1).then(function(results) {
  120. ctx.altSegments = results;
  121. });
  122. ctx.scope.$digest();
  123. });
  124. it('should have no segments', function() {
  125. expect(ctx.altSegments.length).to.be(0);
  126. });
  127. });
  128. describe('targetChanged', function() {
  129. beforeEach(function() {
  130. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  131. ctx.ctrl.parseTarget();
  132. ctx.ctrl.target.target = '';
  133. ctx.ctrl.targetChanged();
  134. });
  135. it('should rebuld target after expression model', function() {
  136. expect(ctx.ctrl.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*, 1), 2)');
  137. });
  138. it('should call panelCtrl.refresh', function() {
  139. expect(ctx.panelCtrl.refresh.called).to.be(true);
  140. });
  141. });
  142. describe('when updating targets with nested query', function() {
  143. beforeEach(function() {
  144. ctx.ctrl.target.target = 'scaleToSeconds(#A)';
  145. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  146. ctx.ctrl.parseTarget();
  147. ctx.ctrl.panelCtrl.panel.targets = [ {
  148. target: 'nested.query.count',
  149. refId: 'A'
  150. }];
  151. ctx.ctrl.updateModelTarget();
  152. });
  153. it('target should remain the same', function() {
  154. expect(ctx.ctrl.target.target).to.be('scaleToSeconds(#A)');
  155. });
  156. it('targetFull should include nexted queries', function() {
  157. expect(ctx.ctrl.target.targetFull).to.be('scaleToSeconds(nested.query.count)');
  158. });
  159. });
  160. describe('when updating target used in other query', function() {
  161. beforeEach(function() {
  162. ctx.ctrl.target.target = 'metrics.a.count';
  163. ctx.ctrl.target.refId = 'A';
  164. ctx.ctrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([{expandable: false}]));
  165. ctx.ctrl.parseTarget();
  166. ctx.ctrl.panelCtrl.panel.targets = [
  167. ctx.ctrl.target, {target: 'sumSeries(#A)', refId: 'B'}
  168. ];
  169. ctx.ctrl.updateModelTarget();
  170. });
  171. it('targetFull of other query should update', function() {
  172. expect(ctx.ctrl.panel.targets[1].targetFull).to.be('sumSeries(metrics.a.count)');
  173. });
  174. });
  175. });