datasource_specs.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.core'));
  10. beforeEach(angularMocks.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.utc().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. var requestOptions, parts, header;
  38. beforeEach(function() {
  39. ctx.ds = new ctx.service({
  40. url: 'http://es.com',
  41. index: '[asd-]YYYY.MM.DD',
  42. jsonData: { interval: 'Daily' }
  43. });
  44. ctx.backendSrv.datasourceRequest = function(options) {
  45. requestOptions = options;
  46. return ctx.$q.when({data: {responses: []}});
  47. };
  48. ctx.ds.query({
  49. range: {
  50. from: moment([2015, 4, 30, 10]),
  51. to: moment([2015, 5, 1, 10])
  52. },
  53. targets: [{ bucketAggs: [], metrics: [], query: 'escape\\:test' }]
  54. });
  55. ctx.$rootScope.$apply();
  56. parts = requestOptions.data.split('\n');
  57. header = angular.fromJson(parts[0]);
  58. });
  59. it('should translate index pattern to current day', function() {
  60. expect(header.index).to.eql(['asd-2015.05.30', 'asd-2015.05.31', 'asd-2015.06.01']);
  61. });
  62. it('should json escape lucene query', function() {
  63. var body = angular.fromJson(parts[1]);
  64. expect(body.query.filtered.query.query_string.query).to.be('escape\\:test');
  65. });
  66. });
  67. describe('When issueing document query', function() {
  68. var requestOptions, parts, header;
  69. beforeEach(function() {
  70. ctx.ds = new ctx.service({url: 'http://es.com', index: 'test', jsonData: {}});
  71. ctx.backendSrv.datasourceRequest = function(options) {
  72. requestOptions = options;
  73. return ctx.$q.when({data: {responses: []}});
  74. };
  75. ctx.ds.query({
  76. range: { from: moment([2015, 4, 30, 10]), to: moment([2015, 5, 1, 10]) },
  77. targets: [{ bucketAggs: [], metrics: [{type: 'raw_document'}], query: 'test' }]
  78. });
  79. ctx.$rootScope.$apply();
  80. parts = requestOptions.data.split('\n');
  81. header = angular.fromJson(parts[0]);
  82. });
  83. it('should set search type to query_then_fetch', function() {
  84. expect(header.search_type).to.eql('query_then_fetch');
  85. });
  86. it('should set size', function() {
  87. var body = angular.fromJson(parts[1]);
  88. expect(body.size).to.be(500);
  89. });
  90. });
  91. });