datasource_specs.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  2. import helpers from 'test/specs/helpers';
  3. import {GraphiteDatasource} from "../datasource";
  4. describe('graphiteDatasource', function() {
  5. var ctx = new helpers.ServiceTestContext();
  6. var instanceSettings: any = {url: ['']};
  7. beforeEach(angularMocks.module('grafana.core'));
  8. beforeEach(angularMocks.module('grafana.services'));
  9. beforeEach(ctx.providePhase(['backendSrv']));
  10. beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) {
  11. ctx.$q = $q;
  12. ctx.$httpBackend = $httpBackend;
  13. ctx.$rootScope = $rootScope;
  14. ctx.$injector = $injector;
  15. $httpBackend.when('GET', /\.html$/).respond('');
  16. }));
  17. beforeEach(function() {
  18. ctx.ds = ctx.$injector.instantiate(GraphiteDatasource, {instanceSettings: instanceSettings});
  19. });
  20. describe('When querying influxdb with one target using query editor target spec', function() {
  21. var query = {
  22. rangeRaw: { from: 'now-1h', to: 'now' },
  23. targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
  24. maxDataPoints: 500,
  25. };
  26. var results;
  27. var requestOptions;
  28. beforeEach(function() {
  29. ctx.backendSrv.datasourceRequest = function(options) {
  30. requestOptions = options;
  31. return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
  32. };
  33. ctx.ds.query(query).then(function(data) { results = data; });
  34. ctx.$rootScope.$apply();
  35. });
  36. it('should generate the correct query', function() {
  37. expect(requestOptions.url).to.be('/render');
  38. });
  39. it('should query correctly', function() {
  40. var params = requestOptions.data.split('&');
  41. expect(params).to.contain('target=prod1.count');
  42. expect(params).to.contain('target=prod2.count');
  43. expect(params).to.contain('from=-1h');
  44. expect(params).to.contain('until=now');
  45. });
  46. it('should exclude undefined params', function() {
  47. var params = requestOptions.data.split('&');
  48. expect(params).to.not.contain('cacheTimeout=undefined');
  49. });
  50. it('should return series list', function() {
  51. expect(results.data.length).to.be(1);
  52. expect(results.data[0].target).to.be('prod1.count');
  53. });
  54. it('should convert to millisecond resolution', function() {
  55. expect(results.data[0].datapoints[0][0]).to.be(10);
  56. });
  57. });
  58. describe('building graphite params', function() {
  59. it('should return empty array if no targets', function() {
  60. var results = ctx.ds.buildGraphiteParams({
  61. targets: [{}]
  62. });
  63. expect(results.length).to.be(0);
  64. });
  65. it('should uri escape targets', function() {
  66. var results = ctx.ds.buildGraphiteParams({
  67. targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
  68. });
  69. expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
  70. });
  71. it('should replace target placeholder', function() {
  72. var results = ctx.ds.buildGraphiteParams({
  73. targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
  74. });
  75. expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
  76. });
  77. it('should replace target placeholder for hidden series', function() {
  78. var results = ctx.ds.buildGraphiteParams({
  79. targets: [{target: 'series1', hide: true}, {target: 'sumSeries(#A)', hide: true}, {target: 'asPercent(#A,#B)'}]
  80. });
  81. expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  82. });
  83. it('should replace target placeholder when nesting query references', function() {
  84. var results = ctx.ds.buildGraphiteParams({
  85. targets: [{target: 'series1'}, {target: 'sumSeries(#A)'}, {target: 'asPercent(#A,#B)'}]
  86. });
  87. expect(results[2]).to.be('target=' + encodeURIComponent("asPercent(series1,sumSeries(series1))"));
  88. });
  89. it('should fix wrong minute interval parameters', function() {
  90. var results = ctx.ds.buildGraphiteParams({
  91. targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }]
  92. });
  93. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  94. });
  95. it('should fix wrong month interval parameters', function() {
  96. var results = ctx.ds.buildGraphiteParams({
  97. targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }]
  98. });
  99. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  100. });
  101. it('should ignore empty targets', function() {
  102. var results = ctx.ds.buildGraphiteParams({
  103. targets: [{target: 'series1'}, {target: ''}]
  104. });
  105. expect(results.length).to.be(2);
  106. });
  107. });
  108. });