query_ctrl_specs.ts 6.8 KB

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