query_ctrl_specs.ts 6.2 KB

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