datasource_specs.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {describe, beforeEach, it, 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: [''], name: 'graphiteProd', jsonData: {}};
  7. beforeEach(angularMocks.module('grafana.core'));
  8. beforeEach(angularMocks.module('grafana.services'));
  9. beforeEach(ctx.providePhase(['backendSrv', 'templateSrv']));
  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. panelId: 3,
  23. rangeRaw: { from: 'now-1h', to: 'now' },
  24. targets: [{ target: 'prod1.count' }, {target: 'prod2.count'}],
  25. maxDataPoints: 500,
  26. };
  27. var results;
  28. var requestOptions;
  29. beforeEach(function() {
  30. ctx.backendSrv.datasourceRequest = function(options) {
  31. requestOptions = options;
  32. return ctx.$q.when({data: [{ target: 'prod1.count', datapoints: [[10, 1], [12,1]] }]});
  33. };
  34. ctx.ds.query(query).then(function(data) { results = data; });
  35. ctx.$rootScope.$apply();
  36. });
  37. it('should generate the correct query', function() {
  38. expect(requestOptions.url).to.be('/render');
  39. });
  40. it('should set unique requestId', function() {
  41. expect(requestOptions.requestId).to.be('graphiteProd.panelId.3');
  42. });
  43. it('should query correctly', function() {
  44. var params = requestOptions.data.split('&');
  45. expect(params).to.contain('target=prod1.count');
  46. expect(params).to.contain('target=prod2.count');
  47. expect(params).to.contain('from=-1h');
  48. expect(params).to.contain('until=now');
  49. });
  50. it('should exclude undefined params', function() {
  51. var params = requestOptions.data.split('&');
  52. expect(params).to.not.contain('cacheTimeout=undefined');
  53. });
  54. it('should return series list', function() {
  55. expect(results.data.length).to.be(1);
  56. expect(results.data[0].target).to.be('prod1.count');
  57. });
  58. it('should convert to millisecond resolution', function() {
  59. expect(results.data[0].datapoints[0][0]).to.be(10);
  60. });
  61. });
  62. describe('building graphite params', function() {
  63. it('should return empty array if no targets', function() {
  64. var results = ctx.ds.buildGraphiteParams({
  65. targets: [{}]
  66. });
  67. expect(results.length).to.be(0);
  68. });
  69. it('should uri escape targets', function() {
  70. var results = ctx.ds.buildGraphiteParams({
  71. targets: [{target: 'prod1.{test,test2}'}, {target: 'prod2.count'}]
  72. });
  73. expect(results).to.contain('target=prod1.%7Btest%2Ctest2%7D');
  74. });
  75. it('should replace target placeholder', function() {
  76. var results = ctx.ds.buildGraphiteParams({
  77. targets: [{target: 'series1'}, {target: 'series2'}, {target: 'asPercent(#A,#B)'}]
  78. });
  79. expect(results[2]).to.be('target=asPercent(series1%2Cseries2)');
  80. });
  81. it('should replace target placeholder for hidden series', function() {
  82. var results = ctx.ds.buildGraphiteParams({
  83. targets: [{target: 'series1', hide: true}, {target: 'sumSeries(#A)', hide: true}, {target: 'asPercent(#A,#B)'}]
  84. });
  85. expect(results[0]).to.be('target=' + encodeURIComponent('asPercent(series1,sumSeries(series1))'));
  86. });
  87. it('should replace target placeholder when nesting query references', function() {
  88. var results = ctx.ds.buildGraphiteParams({
  89. targets: [{target: 'series1'}, {target: 'sumSeries(#A)'}, {target: 'asPercent(#A,#B)'}]
  90. });
  91. expect(results[2]).to.be('target=' + encodeURIComponent("asPercent(series1,sumSeries(series1))"));
  92. });
  93. it('should fix wrong minute interval parameters', function() {
  94. var results = ctx.ds.buildGraphiteParams({
  95. targets: [{target: "summarize(prod.25m.count, '25m', 'sum')" }]
  96. });
  97. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.25m.count, '25min', 'sum')"));
  98. });
  99. it('should fix wrong month interval parameters', function() {
  100. var results = ctx.ds.buildGraphiteParams({
  101. targets: [{target: "summarize(prod.5M.count, '5M', 'sum')" }]
  102. });
  103. expect(results[0]).to.be('target=' + encodeURIComponent("summarize(prod.5M.count, '5mon', 'sum')"));
  104. });
  105. it('should ignore empty targets', function() {
  106. var results = ctx.ds.buildGraphiteParams({
  107. targets: [{target: 'series1'}, {target: ''}]
  108. });
  109. expect(results.length).to.be(2);
  110. });
  111. });
  112. });