datasource_specs.ts 4.6 KB

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