123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- import { of } from 'rxjs';
- import { getDefaultTimeRange, VariableSupportType } from '@grafana/data';
- import { VariableRefresh } from '../types';
- import { QueryRunners, variableDummyRefId } from './queryRunners';
- describe('QueryRunners', () => {
- describe('when using a legacy data source', () => {
- const getLegacyTestContext = (variable?: any) => {
- const defaultTimeRange = getDefaultTimeRange();
- variable = variable ?? { query: 'A query' };
- const timeSrv = {
- timeRange: jest.fn().mockReturnValue(defaultTimeRange),
- };
- const datasource: any = { metricFindQuery: jest.fn().mockResolvedValue([{ text: 'A', value: 'A' }]) };
- const runner = new QueryRunners().getRunnerForDatasource(datasource);
- const runRequest = jest.fn().mockReturnValue(of({}));
- const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest };
- const request: any = {};
- return { timeSrv, datasource, runner, variable, runnerArgs, request, defaultTimeRange };
- };
- describe('and calling getRunnerForDatasource', () => {
- it('then it should return LegacyQueryRunner', () => {
- const { runner } = getLegacyTestContext();
- expect(runner!.type).toEqual(VariableSupportType.Legacy);
- });
- });
- describe('and calling getTarget', () => {
- it('then it should return correct target', () => {
- const { runner, datasource, variable } = getLegacyTestContext();
- const target = runner.getTarget({ datasource, variable });
- expect(target).toEqual('A query');
- });
- });
- describe('and calling runRequest with a variable that refreshes when time range changes', () => {
- const { datasource, runner, runnerArgs, request, timeSrv, defaultTimeRange } = getLegacyTestContext({
- query: 'A query',
- refresh: VariableRefresh.onTimeRangeChanged,
- });
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({
- series: [{ text: 'A', value: 'A' }],
- state: 'Done',
- timeRange: defaultTimeRange,
- });
- });
- });
- it('and it should call timeSrv.timeRange()', () => {
- expect(timeSrv.timeRange).toHaveBeenCalledTimes(1);
- });
- it('and it should call metricFindQuery with correct options', () => {
- expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1);
- expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', {
- range: defaultTimeRange,
- searchFilter: 'A searchFilter',
- variable: {
- query: 'A query',
- refresh: VariableRefresh.onTimeRangeChanged,
- },
- });
- });
- });
- describe('and calling runRequest with a variable that refreshes on dashboard load', () => {
- const { datasource, runner, runnerArgs, request, timeSrv, defaultTimeRange } = getLegacyTestContext({
- query: 'A query',
- refresh: VariableRefresh.onDashboardLoad,
- });
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({
- series: [{ text: 'A', value: 'A' }],
- state: 'Done',
- timeRange: defaultTimeRange,
- });
- });
- });
- it('and it should call timeSrv.timeRange()', () => {
- expect(timeSrv.timeRange).toHaveBeenCalledTimes(1);
- });
- it('and it should call metricFindQuery with correct options', () => {
- expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1);
- expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', {
- range: defaultTimeRange,
- searchFilter: 'A searchFilter',
- variable: {
- query: 'A query',
- refresh: VariableRefresh.onDashboardLoad,
- },
- });
- });
- });
- describe('and calling runRequest with a variable that does not refresh when time range changes', () => {
- const { datasource, runner, runnerArgs, request, timeSrv } = getLegacyTestContext({
- query: 'A query',
- refresh: VariableRefresh.never,
- });
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const values = received[0];
- expect(values).toEqual({
- series: [{ text: 'A', value: 'A' }],
- state: 'Done',
- timeRange: undefined,
- });
- });
- });
- it('and it should not call timeSrv.timeRange()', () => {
- expect(timeSrv.timeRange).not.toHaveBeenCalled();
- });
- it('and it should call metricFindQuery with correct options', () => {
- expect(datasource.metricFindQuery).toHaveBeenCalledTimes(1);
- expect(datasource.metricFindQuery).toHaveBeenCalledWith('A query', {
- range: undefined,
- searchFilter: 'A searchFilter',
- variable: {
- query: 'A query',
- refresh: VariableRefresh.never,
- },
- });
- });
- });
- });
- describe('when using a data source with standard variable support', () => {
- const getStandardTestContext = (datasource?: any) => {
- const variable: any = { query: { refId: 'A', query: 'A query' } };
- const timeSrv = {};
- datasource = datasource ?? {
- variables: {
- getType: () => VariableSupportType.Standard,
- toDataQuery: (query: any) => ({ ...query, extra: 'extra' }),
- },
- };
- const runner = new QueryRunners().getRunnerForDatasource(datasource);
- const runRequest = jest.fn().mockReturnValue(of({}));
- const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest };
- const request: any = {};
- return { timeSrv, datasource, runner, variable, runnerArgs, request, runRequest };
- };
- describe('and calling getRunnerForDatasource', () => {
- it('then it should return StandardQueryRunner', () => {
- const { runner } = getStandardTestContext();
- expect(runner!.type).toEqual(VariableSupportType.Standard);
- });
- });
- describe('and calling getTarget', () => {
- it('then it should return correct target', () => {
- const { runner, variable, datasource } = getStandardTestContext();
- const target = runner.getTarget({ datasource, variable });
- expect(target).toEqual({ refId: 'A', query: 'A query', extra: 'extra' });
- });
- });
- describe('and calling runRequest with a datasource that uses a custom query', () => {
- const { runner, request, runnerArgs, runRequest, datasource } = getStandardTestContext({
- variables: {
- getType: () => VariableSupportType.Standard,
- toDataQuery: () => undefined,
- query: () => undefined,
- },
- });
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({});
- });
- });
- it('then it should call runRequest with correct args', () => {
- expect(runRequest).toHaveBeenCalledTimes(1);
- expect(runRequest).toHaveBeenCalledWith(datasource, {}, datasource.variables.query);
- });
- });
- describe('and calling runRequest with a datasource that has no custom query', () => {
- const { runner, request, runnerArgs, runRequest, datasource } = getStandardTestContext({
- variables: { getType: () => VariableSupportType.Standard, toDataQuery: () => undefined },
- });
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({});
- });
- });
- it('then it should call runRequest with correct args', () => {
- expect(runRequest).toHaveBeenCalledTimes(1);
- expect(runRequest).toHaveBeenCalledWith(datasource, {});
- });
- });
- });
- describe('when using a data source with custom variable support', () => {
- const getCustomTestContext = () => {
- const variable: any = { query: { refId: 'A', query: 'A query' } };
- const timeSrv = {};
- const datasource: any = {
- variables: { getType: () => VariableSupportType.Custom, query: () => undefined, editor: {} },
- };
- const runner = new QueryRunners().getRunnerForDatasource(datasource);
- const runRequest = jest.fn().mockReturnValue(of({}));
- const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest };
- const request: any = {};
- return { timeSrv, datasource, runner, variable, runnerArgs, request, runRequest };
- };
- describe('and calling getRunnerForDatasource', () => {
- it('then it should return CustomQueryRunner', () => {
- const { runner } = getCustomTestContext();
- expect(runner!.type).toEqual(VariableSupportType.Custom);
- });
- });
- describe('and calling getTarget', () => {
- it('then it should return correct target', () => {
- const { runner, variable, datasource } = getCustomTestContext();
- const target = runner.getTarget({ datasource, variable });
- expect(target).toEqual({ refId: 'A', query: 'A query' });
- });
- });
- describe('and calling runRequest', () => {
- const { runner, request, runnerArgs, runRequest, datasource } = getCustomTestContext();
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({});
- });
- });
- it('then it should call runRequest with correct args', () => {
- expect(runRequest).toHaveBeenCalledTimes(1);
- expect(runRequest).toHaveBeenCalledWith(datasource, {}, datasource.variables.query);
- });
- });
- });
- describe('when using a data source with datasource variable support', () => {
- const getDatasourceTestContext = () => {
- const variable: any = { query: { refId: 'A', query: 'A query' } };
- const timeSrv = {};
- const datasource: any = {
- variables: { getType: () => VariableSupportType.Datasource },
- };
- const runner = new QueryRunners().getRunnerForDatasource(datasource);
- const runRequest = jest.fn().mockReturnValue(of({}));
- const runnerArgs: any = { datasource, variable, searchFilter: 'A searchFilter', timeSrv, runRequest };
- const request: any = {};
- return { timeSrv, datasource, runner, variable, runnerArgs, request, runRequest };
- };
- describe('and calling getRunnerForDatasource', () => {
- it('then it should return DatasourceQueryRunner', () => {
- const { runner } = getDatasourceTestContext();
- expect(runner!.type).toEqual(VariableSupportType.Datasource);
- });
- });
- describe('and calling getTarget', () => {
- it('then it should return correct target', () => {
- const { runner, datasource, variable } = getDatasourceTestContext();
- const target = runner.getTarget({ datasource, variable });
- expect(target).toEqual({ refId: 'A', query: 'A query' });
- });
- describe('and ref id is missing', () => {
- it('then it should return correct target with dummy ref id', () => {
- const { runner, datasource, variable } = getDatasourceTestContext();
- delete variable.query.refId;
- const target = runner.getTarget({ datasource, variable });
- expect(target).toEqual({ refId: variableDummyRefId, query: 'A query' });
- });
- });
- });
- describe('and calling runRequest', () => {
- const { runner, request, runnerArgs, runRequest, datasource } = getDatasourceTestContext();
- const observable = runner.runRequest(runnerArgs, request);
- it('then it should return correct observable', async () => {
- await expect(observable).toEmitValuesWith((received) => {
- const value = received[0];
- expect(value).toEqual({});
- });
- });
- it('then it should call runRequest with correct args', () => {
- expect(runRequest).toHaveBeenCalledTimes(1);
- expect(runRequest).toHaveBeenCalledWith(datasource, {});
- });
- });
- });
- describe('when using a data source with unknown variable support', () => {
- describe('and calling getRunnerForDatasource', () => {
- it('then it should throw', () => {
- const datasource: any = {
- variables: {},
- };
- expect(() => new QueryRunners().getRunnerForDatasource(datasource)).toThrow();
- });
- });
- });
- });
|