query_ctrl_specs.ts 5.7 KB

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