mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: add tests for addAdHocFilters (#38594)
* Loki: add tests for addAdHocFilters * Change description of test Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * Replace ADD_FILTER with "=" operator in test description Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> * Loki: refafctor test for addAdHocFilter * Loki: refactor tests for better readability * Remove comments Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com> Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>
This commit is contained in:
@@ -84,6 +84,13 @@ const testMetricsResponse: FetchResponse<LokiResponse> = {
|
|||||||
config: ({} as unknown) as BackendSrvRequest,
|
config: ({} as unknown) as BackendSrvRequest,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
interface AdHocFilter {
|
||||||
|
condition: string;
|
||||||
|
key: string;
|
||||||
|
operator: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
describe('LokiDatasource', () => {
|
describe('LokiDatasource', () => {
|
||||||
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
const fetchMock = jest.spyOn(backendSrv, 'fetch');
|
||||||
|
|
||||||
@@ -782,6 +789,80 @@ describe('LokiDatasource', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('addAdHocFilters', () => {
|
||||||
|
let ds: LokiDatasource;
|
||||||
|
let adHocFilters: AdHocFilter[];
|
||||||
|
describe('when called with "=" operator', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
adHocFilters = [
|
||||||
|
{
|
||||||
|
condition: '',
|
||||||
|
key: 'job',
|
||||||
|
operator: '=',
|
||||||
|
value: 'grafana',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const templateSrvMock = ({
|
||||||
|
getAdhocFilters: (): AdHocFilter[] => adHocFilters,
|
||||||
|
replace: (a: string) => a,
|
||||||
|
} as unknown) as TemplateSrv;
|
||||||
|
ds = createLokiDSForTests(templateSrvMock);
|
||||||
|
});
|
||||||
|
describe('and query has no parser', () => {
|
||||||
|
it('then the correct label should be added for logs query', () => {
|
||||||
|
assertAdHocFilters('{bar="baz"}', '{bar="baz",job="grafana"}', ds);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('then the correct label should be added for metrics query', () => {
|
||||||
|
assertAdHocFilters('rate({bar="baz"}[5m])', 'rate({bar="baz",job="grafana"}[5m])', ds);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('and query has parser', () => {
|
||||||
|
it('then the correct label should be added for logs query', () => {
|
||||||
|
assertAdHocFilters('{bar="baz"} | logfmt', '{bar="baz"} | logfmt | job="grafana"', ds);
|
||||||
|
});
|
||||||
|
it('then the correct label should be added for metrics query', () => {
|
||||||
|
assertAdHocFilters('rate({bar="baz"} | logfmt [5m])', 'rate({bar="baz",job="grafana"} | logfmt [5m])', ds);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when called with "!=" operator', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
adHocFilters = [
|
||||||
|
{
|
||||||
|
condition: '',
|
||||||
|
key: 'job',
|
||||||
|
operator: '!=',
|
||||||
|
value: 'grafana',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const templateSrvMock = ({
|
||||||
|
getAdhocFilters: (): AdHocFilter[] => adHocFilters,
|
||||||
|
replace: (a: string) => a,
|
||||||
|
} as unknown) as TemplateSrv;
|
||||||
|
ds = createLokiDSForTests(templateSrvMock);
|
||||||
|
});
|
||||||
|
describe('and query has no parser', () => {
|
||||||
|
it('then the correct label should be added for logs query', () => {
|
||||||
|
assertAdHocFilters('{bar="baz"}', '{bar="baz",job!="grafana"}', ds);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('then the correct label should be added for metrics query', () => {
|
||||||
|
assertAdHocFilters('rate({bar="baz"}[5m])', 'rate({bar="baz",job!="grafana"}[5m])', ds);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('and query has parser', () => {
|
||||||
|
it('then the correct label should be added for logs query', () => {
|
||||||
|
assertAdHocFilters('{bar="baz"} | logfmt', '{bar="baz"} | logfmt | job!="grafana"', ds);
|
||||||
|
});
|
||||||
|
it('then the correct label should be added for metrics query', () => {
|
||||||
|
assertAdHocFilters('rate({bar="baz"} | logfmt [5m])', 'rate({bar="baz",job!="grafana"} | logfmt [5m])', ds);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('adjustInterval', () => {
|
describe('adjustInterval', () => {
|
||||||
const dynamicInterval = 15;
|
const dynamicInterval = 15;
|
||||||
const range = 1642;
|
const range = 1642;
|
||||||
@@ -803,6 +884,13 @@ describe('LokiDatasource', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) {
|
||||||
|
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
|
||||||
|
const result = ds.addAdHocFilters(lokiQuery.expr);
|
||||||
|
|
||||||
|
expect(result).toEqual(expectedResults);
|
||||||
|
}
|
||||||
|
|
||||||
function createLokiDSForTests(
|
function createLokiDSForTests(
|
||||||
templateSrvMock = ({
|
templateSrvMock = ({
|
||||||
getAdhocFilters: (): any[] => [],
|
getAdhocFilters: (): any[] => [],
|
||||||
|
|||||||
Reference in New Issue
Block a user