elasticsearch-specs.js 2.2 KB

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