datasource_specs.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ///<amd-dependency path="../datasource" />
  2. ///<amd-dependency path="test/specs/helpers" name="helpers" />
  3. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  4. import moment = require('moment');
  5. import angular = require('angular');
  6. declare var helpers: any;
  7. describe('ElasticDatasource', function() {
  8. var ctx = new helpers.ServiceTestContext();
  9. beforeEach(angularMocks.module('grafana.services'));
  10. beforeEach(ctx.providePhase(['templateSrv', 'backendSrv']));
  11. beforeEach(ctx.createService('ElasticDatasource'));
  12. beforeEach(function() {
  13. ctx.ds = new ctx.service({jsonData: {}});
  14. });
  15. describe('When testing datasource with index pattern', function() {
  16. beforeEach(function() {
  17. ctx.ds = new ctx.service({
  18. url: 'http://es.com',
  19. index: '[asd-]YYYY.MM.DD',
  20. jsonData: { interval: 'Daily' }
  21. });
  22. });
  23. it('should translate index pattern to current day', function() {
  24. var requestOptions;
  25. ctx.backendSrv.datasourceRequest = function(options) {
  26. requestOptions = options;
  27. return ctx.$q.when({});
  28. };
  29. ctx.ds.testDatasource();
  30. ctx.$rootScope.$apply();
  31. var today = moment().format("YYYY.MM.DD");
  32. expect(requestOptions.url).to.be("http://es.com/asd-" + today + '/_stats');
  33. });
  34. });
  35. describe('When issueing metric query with interval pattern', function() {
  36. beforeEach(function() {
  37. ctx.ds = new ctx.service({
  38. url: 'http://es.com',
  39. index: '[asd-]YYYY.MM.DD',
  40. jsonData: { interval: 'Daily' }
  41. });
  42. });
  43. it('should translate index pattern to current day', function() {
  44. var requestOptions;
  45. ctx.backendSrv.datasourceRequest = function(options) {
  46. requestOptions = options;
  47. return ctx.$q.when({data: {responses: []}});
  48. };
  49. ctx.ds.query({
  50. range: {
  51. from: moment([2015, 4, 30, 10]),
  52. to: moment([2015, 5, 1, 10])
  53. },
  54. targets: [{ bucketAggs: [], metrics: [] }]
  55. });
  56. ctx.$rootScope.$apply();
  57. var parts = requestOptions.data.split('\n');
  58. var header = angular.fromJson(parts[0]);
  59. expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
  60. });
  61. });
  62. });