graphiteTargetCtrl-specs.js 5.7 KB

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