query_ctrl_specs.ts 5.6 KB

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