Explore: Use fixed dates to prevent DST issues in tests (#103497)

* Explore: Use fixed dates to prevent DST issues in tests

* PanelQueryRunner: Use fixed dates to prevent DST issues in tests
This commit is contained in:
Sven Grossmann
2025-04-06 17:04:28 +02:00
committed by GitHub
parent bdbe94abc8
commit 8eea0c9a3d
2 changed files with 15 additions and 7 deletions

View File

@ -250,27 +250,35 @@ describe('when buildQueryTransaction', () => {
it('it should calculate interval based on time range', () => {
const queries = [{ refId: 'A' }];
const queryOptions = { maxDataPoints: 1000, minInterval: '15s' };
const range = { from: dateTime().subtract(1, 'd'), to: dateTime(), raw: { from: '1h', to: '1h' } };
const from = dateTime('2023-01-01T12:00:00Z');
const to = dateTime('2023-01-02T12:00:00Z');
const range = { from, to, raw: { from: '1h', to: '1h' } };
const transaction = buildQueryTransaction('left', queries, queryOptions, range, false);
expect(transaction.request.intervalMs).toEqual(60000);
});
it('it should calculate interval taking minInterval into account', () => {
const queries = [{ refId: 'A' }];
const queryOptions = { maxDataPoints: 1000, minInterval: '15s' };
const range = { from: dateTime().subtract(1, 'm'), to: dateTime(), raw: { from: '1h', to: '1h' } };
const from = dateTime('2023-01-01T12:00:00Z');
const to = dateTime('2023-01-01T12:01:00Z');
const range = { from, to, raw: { from: '1h', to: '1h' } };
const transaction = buildQueryTransaction('left', queries, queryOptions, range, false);
expect(transaction.request.intervalMs).toEqual(15000);
});
it('it should calculate interval taking maxDataPoints into account', () => {
const queries = [{ refId: 'A' }];
const queryOptions = { maxDataPoints: 10, minInterval: '15s' };
const range = { from: dateTime().subtract(1, 'd'), to: dateTime(), raw: { from: '1h', to: '1h' } };
const from = dateTime('2023-01-01T12:00:00Z');
const to = dateTime('2023-01-02T12:00:00Z');
const range = { from, to, raw: { from: '1h', to: '1h' } };
const transaction = buildQueryTransaction('left', queries, queryOptions, range, false);
expect(transaction.request.interval).toEqual('2h');
});
it('it should create a request with X-Cache-Skip set to true', () => {
const queries = [{ refId: 'A' }];
const range = { from: dateTime().subtract(1, 'd'), to: dateTime(), raw: { from: '1h', to: '1h' } };
const from = dateTime('2023-01-01T12:00:00Z');
const to = dateTime('2023-01-02T12:00:00Z');
const range = { from, to, raw: { from: '1h', to: '1h' } };
const transaction = buildQueryTransaction('left', queries, {}, range, false);
expect(transaction.request.skipQueryCache).toBe(true);
});

View File

@ -5,7 +5,7 @@ import { Subject } from 'rxjs';
// Importing this way to be able to spy on grafana/data
import * as grafanaData from '@grafana/data';
import { DataSourceApi, TypedVariableModel } from '@grafana/data';
import { DataSourceApi, dateTime, TypedVariableModel } from '@grafana/data';
import { DataSourceSrv, setDataSourceSrv, setEchoSrv } from '@grafana/runtime';
import { TemplateSrvMock } from 'app/features/templating/template_srv.mock';
@ -155,8 +155,8 @@ function describeQueryRunnerScenario(
minInterval: ctx.minInterval,
maxDataPoints: ctx.maxDataPoints ?? Infinity,
timeRange: {
from: grafanaData.dateTime().subtract(1, 'days'),
to: grafanaData.dateTime(),
from: dateTime('2023-01-01T12:00:00Z'),
to: dateTime('2023-01-02T12:00:00Z'),
raw: { from: '1d', to: 'now' },
},
panelId: 1,