query_ctrl_specs.ts 5.3 KB

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