graphiteDatasource-specs.js 4.2 KB

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