query_ctrl_specs.ts 5.8 KB

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