From 22e547f625ecd5638f3d05ca16cbd0bcc30a95bb Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Thu, 29 Sep 2022 15:19:59 +0200 Subject: [PATCH] Chore: update language provider test --- .../datasource/loki/LanguageProvider.test.ts | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/loki/LanguageProvider.test.ts b/public/app/plugins/datasource/loki/LanguageProvider.test.ts index 95e4c26d6e4..2b9e16985e1 100644 --- a/public/app/plugins/datasource/loki/LanguageProvider.test.ts +++ b/public/app/plugins/datasource/loki/LanguageProvider.test.ts @@ -1,14 +1,17 @@ import Plain from 'slate-plain-serializer'; -import { AbstractLabelOperator } from '@grafana/data'; +import { AbstractLabelOperator, DataFrame } from '@grafana/data'; import { TypeaheadInput } from '@grafana/ui'; -import { TemplateSrv } from 'app/features/templating/template_srv'; +import { executeAnnotationQuery } from 'app/features/annotations/executeAnnotationQuery'; import LanguageProvider, { LokiHistoryItem } from './LanguageProvider'; import { LokiDatasource } from './datasource'; import { createLokiDatasource, createMetadataRequest } from './mocks'; +import { extractLogParserFromDataFrame } from './responseUtils'; import { LokiQueryType } from './types'; +jest.mock('./responseUtils'); + jest.mock('app/store/store', () => ({ store: { getState: jest.fn().mockReturnValue({ @@ -297,6 +300,49 @@ describe('Query imports', () => { }); }); }); + + describe('getParserAndLabelKeys()', () => { + let datasource: LokiDatasource, languageProvider: LanguageProvider; + const extractLogParserFromDataFrameMock = extractLogParserFromDataFrame as jest.Mock; + beforeEach(() => { + datasource = createLokiDatasource(); + languageProvider = new LanguageProvider(datasource); + }); + + it('identifies selectors with JSON parser data', async () => { + jest.spyOn(datasource, 'getDataSamples').mockResolvedValue([{}] as DataFrame[]); + extractLogParserFromDataFrameMock.mockReturnValueOnce({ hasLogfmt: false, hasJSON: true }); + + expect(await languageProvider.getParserAndLabelKeys('{place="luna"}')).toEqual({ + extractedLabelKeys: [], + hasJSON: true, + hasLogfmt: false, + }); + }); + + it('identifies selectors with Logfmt parser data', async () => { + jest.spyOn(datasource, 'getDataSamples').mockResolvedValue([{}] as DataFrame[]); + extractLogParserFromDataFrameMock.mockReturnValueOnce({ hasLogfmt: true, hasJSON: false }); + + expect(await languageProvider.getParserAndLabelKeys('{place="luna"}')).toEqual({ + extractedLabelKeys: [], + hasJSON: false, + hasLogfmt: true, + }); + }); + + it('correctly processes empty data', async () => { + jest.spyOn(datasource, 'getDataSamples').mockResolvedValue([]); + extractLogParserFromDataFrameMock.mockClear(); + + expect(await languageProvider.getParserAndLabelKeys('{place="luna"}')).toEqual({ + extractedLabelKeys: [], + hasJSON: false, + hasLogfmt: false, + }); + expect(extractLogParserFromDataFrameMock).not.toHaveBeenCalled(); + }); + }); }); async function getLanguageProvider(datasource: LokiDatasource) { @@ -334,7 +380,7 @@ function setup( labelsAndValues: Record, series?: Record>> ): LokiDatasource { - const datasource = createLokiDatasource({} as unknown as TemplateSrv); + const datasource = createLokiDatasource(); const rangeMock = { start: 1560153109000,