datasource_specs.ts 4.2 KB

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