Files
grafana/public/app/plugins/datasource/elasticsearch/utils.test.ts
Gareth Dawson d6e4f2a504 Elasticsearch: Enable logs samples for metric queries (#70258)
* enable logs samples on elastic ds

* add tests for getSupplementaryQuery

* only display log samples for date_hostogram queries

* changes

* test

* Update public/app/plugins/datasource/elasticsearch/datasource.test.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* Update public/app/plugins/datasource/elasticsearch/datasource.test.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* Update public/app/plugins/datasource/elasticsearch/datasource.test.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* Update public/app/plugins/datasource/elasticsearch/datasource.test.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* Update public/app/plugins/datasource/elasticsearch/datasource.test.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

* address feedback / tests

---------

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
2023-06-22 12:53:05 +01:00

82 lines
2.1 KiB
TypeScript

import { ElasticsearchQuery } from './types';
import { isTimeSeriesQuery, removeEmpty } from './utils';
describe('removeEmpty', () => {
it('Should remove all empty', () => {
const original = {
stringsShouldBeKept: 'Something',
unlessTheyAreEmpty: '',
nullToBeRemoved: null,
undefinedToBeRemoved: null,
zeroShouldBeKept: 0,
booleansShouldBeKept: false,
emptyObjectsShouldBeRemoved: {},
emptyArrayShouldBeRemoved: [],
nonEmptyArraysShouldBeKept: [1, 2, 3],
nestedObjToBeRemoved: {
toBeRemoved: undefined,
},
nestedObjectToKeep: {
thisShouldBeRemoved: null,
thisShouldBeKept: 'Hello, Grafana',
},
};
const expectedResult = {
stringsShouldBeKept: 'Something',
zeroShouldBeKept: 0,
booleansShouldBeKept: false,
nonEmptyArraysShouldBeKept: [1, 2, 3],
nestedObjectToKeep: {
thisShouldBeKept: 'Hello, Grafana',
},
};
expect(removeEmpty(original)).toStrictEqual(expectedResult);
});
});
describe('isTimeSeriesQuery', () => {
it('should return false when given a log query', () => {
const logsQuery: ElasticsearchQuery = {
refId: `A`,
metrics: [{ type: 'logs', id: '1' }],
};
expect(isTimeSeriesQuery(logsQuery)).toBe(false);
});
it('should return false when bucket aggs are empty', () => {
const query: ElasticsearchQuery = {
refId: `A`,
bucketAggs: [],
};
expect(isTimeSeriesQuery(query)).toBe(false);
});
it('returns false when empty date_histogram is not last', () => {
const query: ElasticsearchQuery = {
refId: `A`,
bucketAggs: [
{ id: '1', type: 'date_histogram' },
{ id: '2', type: 'terms' },
],
};
expect(isTimeSeriesQuery(query)).toBe(false);
});
it('returns true when empty date_histogram is last', () => {
const query: ElasticsearchQuery = {
refId: `A`,
bucketAggs: [
{ id: '1', type: 'terms' },
{ id: '2', type: 'date_histogram' },
],
};
expect(isTimeSeriesQuery(query)).toBe(true);
});
});