language_provider.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { AbstractLabelOperator, AbstractQuery, DataSourceInstanceSettings } from '@grafana/data';
  2. import { TemplateSrv } from '../../../features/templating/template_srv';
  3. import { ElasticDatasource } from './datasource';
  4. import LanguageProvider from './language_provider';
  5. import { ElasticsearchOptions, ElasticsearchQuery } from './types';
  6. const templateSrvStub = {
  7. getAdhocFilters: jest.fn(() => [] as any[]),
  8. replace: jest.fn((a: string) => a),
  9. } as any;
  10. const dataSource = new ElasticDatasource(
  11. {
  12. url: 'http://es.com',
  13. database: '[asd-]YYYY.MM.DD',
  14. jsonData: {
  15. interval: 'Daily',
  16. esVersion: '2.0.0',
  17. timeField: '@time',
  18. },
  19. } as DataSourceInstanceSettings<ElasticsearchOptions>,
  20. templateSrvStub as TemplateSrv
  21. );
  22. const baseLogsQuery: Partial<ElasticsearchQuery> = {
  23. metrics: [{ type: 'logs', id: '1' }],
  24. };
  25. describe('transform abstract query to elasticsearch query', () => {
  26. it('With some labels', () => {
  27. const instance = new LanguageProvider(dataSource);
  28. const abstractQuery: AbstractQuery = {
  29. refId: 'bar',
  30. labelMatchers: [
  31. { name: 'label1', operator: AbstractLabelOperator.Equal, value: 'value1' },
  32. { name: 'label2', operator: AbstractLabelOperator.NotEqual, value: 'value2' },
  33. { name: 'label3', operator: AbstractLabelOperator.EqualRegEx, value: 'value3' },
  34. { name: 'label4', operator: AbstractLabelOperator.NotEqualRegEx, value: 'value4' },
  35. ],
  36. };
  37. const result = instance.importFromAbstractQuery(abstractQuery);
  38. expect(result).toEqual({
  39. ...baseLogsQuery,
  40. query: 'label1:"value1" AND NOT label2:"value2" AND label3:/value3/ AND NOT label4:/value4/',
  41. refId: abstractQuery.refId,
  42. });
  43. });
  44. it('Empty query', () => {
  45. const instance = new LanguageProvider(dataSource);
  46. const abstractQuery = { labelMatchers: [], refId: 'foo' };
  47. const result = instance.importFromAbstractQuery(abstractQuery);
  48. expect(result).toEqual({
  49. ...baseLogsQuery,
  50. query: '',
  51. refId: abstractQuery.refId,
  52. });
  53. });
  54. });