datasource_specs.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.utc().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. var requestOptions, parts, header;
  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. ctx.backendSrv.datasourceRequest = function(options) {
  44. requestOptions = options;
  45. return ctx.$q.when({data: {responses: []}});
  46. };
  47. ctx.ds.query({
  48. range: {
  49. from: moment([2015, 4, 30, 10]),
  50. to: moment([2015, 5, 1, 10])
  51. },
  52. targets: [{ bucketAggs: [], metrics: [], query: 'escape\\:test' }]
  53. });
  54. ctx.$rootScope.$apply();
  55. parts = requestOptions.data.split('\n');
  56. header = angular.fromJson(parts[0]);
  57. });
  58. it('should translate index pattern to current day', function() {
  59. expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
  60. });
  61. it('should json escape lucene query', function() {
  62. var body = angular.fromJson(parts[1]);
  63. expect(body.query.filtered.query.query_string.query).to.be('escape\\:test');
  64. });
  65. });
  66. });