datasource_specs.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import "../datasource";
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
  3. import moment from 'moment';
  4. import angular from 'angular';
  5. import helpers from 'test/specs/helpers';
  6. describe('ElasticDatasource', function() {
  7. var ctx = new helpers.ServiceTestContext();
  8. beforeEach(angularMocks.module('grafana.core'));
  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. describe('When issueing document query', function() {
  67. var requestOptions, parts, header;
  68. beforeEach(function() {
  69. ctx.ds = new ctx.service({url: 'http://es.com', index: 'test', jsonData: {}});
  70. ctx.backendSrv.datasourceRequest = function(options) {
  71. requestOptions = options;
  72. return ctx.$q.when({data: {responses: []}});
  73. };
  74. ctx.ds.query({
  75. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  76. targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'test' }]
  77. });
  78. ctx.$rootScope.$apply();
  79. parts = requestOptions.data.split('\n');
  80. header = angular.fromJson(parts[0]);
  81. });
  82. it('should set search type to query_then_fetch', function() {
  83. expect(header.search_type).to.eql('query_then_fetch');
  84. });
  85. it('should set size', function() {
  86. var body = angular.fromJson(parts[1]);
  87. expect(body.size).to.be(500);
  88. });
  89. });
  90. });