mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
Primarily- moving majority of the types and utils from @grafana/ui to @grafana/data * Move types from grafana-ui to grafana-data * Move valueFormats to grafana-data * Move utils from grafana-ui to grafana-data * Update imports in grafana-ui * revert data's tsconfig change * Update imports in grafana-runtime * Fix import paths in grafana-ui * Move rxjs to devDeps * Core import updates batch 1 * Import updates batch 2 * Imports fix batch 3 * Imports fixes batch i don't know * Fix imorts in grafana-toolkit * Fix imports after master merge
142 lines
3.5 KiB
TypeScript
142 lines
3.5 KiB
TypeScript
import { PrometheusDatasource } from './datasource';
|
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
|
import { PromContext, PromOptions } from './types';
|
|
import { dateTime, LoadingState } from '@grafana/data';
|
|
|
|
const defaultInstanceSettings: DataSourceInstanceSettings<PromOptions> = {
|
|
url: 'test_prom',
|
|
jsonData: {},
|
|
} as any;
|
|
|
|
const backendSrvMock: any = {
|
|
datasourceRequest: jest.fn(),
|
|
};
|
|
|
|
const templateSrvMock: any = {
|
|
replace(): null {
|
|
return null;
|
|
},
|
|
getAdhocFilters(): any[] {
|
|
return [];
|
|
},
|
|
};
|
|
|
|
const timeSrvMock: any = {
|
|
timeRange(): any {
|
|
return {
|
|
from: dateTime(),
|
|
to: dateTime(),
|
|
};
|
|
},
|
|
};
|
|
|
|
describe('datasource', () => {
|
|
describe('query', () => {
|
|
const ds = new PrometheusDatasource(
|
|
defaultInstanceSettings,
|
|
{} as any,
|
|
backendSrvMock,
|
|
templateSrvMock,
|
|
timeSrvMock
|
|
);
|
|
|
|
it('returns empty array when no queries', done => {
|
|
expect.assertions(2);
|
|
ds.query(makeQuery([])).subscribe({
|
|
next(next) {
|
|
expect(next.data).toEqual([]);
|
|
expect(next.state).toBe(LoadingState.Done);
|
|
},
|
|
complete() {
|
|
done();
|
|
},
|
|
});
|
|
});
|
|
|
|
it('performs time series queries', done => {
|
|
expect.assertions(2);
|
|
backendSrvMock.datasourceRequest.mockReturnValueOnce(Promise.resolve(makePromResponse()));
|
|
ds.query(makeQuery([{}])).subscribe({
|
|
next(next) {
|
|
expect(next.data.length).not.toBe(0);
|
|
expect(next.state).toBe(LoadingState.Done);
|
|
},
|
|
complete() {
|
|
done();
|
|
},
|
|
});
|
|
});
|
|
|
|
it('with 2 queries and used from Explore, sends results as they arrive', done => {
|
|
expect.assertions(4);
|
|
backendSrvMock.datasourceRequest.mockReturnValue(Promise.resolve(makePromResponse()));
|
|
const responseStatus = [LoadingState.Loading, LoadingState.Done];
|
|
ds.query(makeQuery([{ context: PromContext.Explore }, { context: PromContext.Explore }])).subscribe({
|
|
next(next) {
|
|
expect(next.data.length).not.toBe(0);
|
|
expect(next.state).toBe(responseStatus.shift());
|
|
},
|
|
complete() {
|
|
done();
|
|
},
|
|
});
|
|
});
|
|
|
|
it('with 2 queries and used from Panel, waits for all to finish until sending Done status', done => {
|
|
expect.assertions(2);
|
|
backendSrvMock.datasourceRequest.mockReturnValue(Promise.resolve(makePromResponse()));
|
|
ds.query(makeQuery([{ context: PromContext.Panel }, { context: PromContext.Panel }])).subscribe({
|
|
next(next) {
|
|
expect(next.data.length).not.toBe(0);
|
|
expect(next.state).toBe(LoadingState.Done);
|
|
},
|
|
complete() {
|
|
done();
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
function makeQuery(targets: any[]): any {
|
|
return {
|
|
targets: targets.map(t => {
|
|
return {
|
|
instant: false,
|
|
start: dateTime().subtract(5, 'minutes'),
|
|
end: dateTime(),
|
|
expr: 'test',
|
|
showingGraph: true,
|
|
...t,
|
|
};
|
|
}),
|
|
range: {
|
|
from: dateTime(),
|
|
to: dateTime(),
|
|
},
|
|
interval: '15s',
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Creates a pretty bogus prom response. Definitelly needs more work but right now we do not test the contents of the
|
|
* messages anyway.
|
|
*/
|
|
function makePromResponse() {
|
|
return {
|
|
data: {
|
|
data: {
|
|
result: [
|
|
{
|
|
metric: {
|
|
__name__: 'test_metric',
|
|
},
|
|
values: [[1568369640, 1]],
|
|
},
|
|
],
|
|
resultType: 'matrix',
|
|
},
|
|
},
|
|
};
|
|
}
|