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