The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/plugins/datasource/postgres/datasource.test.ts

66 lines
1.9 KiB

import { backendSrv } from 'app/core/services/backend_srv';
import { of, throwError } from 'rxjs';
import { createFetchResponse } from 'test/helpers/createFetchResponse';
import { PostgresDatasource } from './datasource';
jest.mock('@grafana/runtime', () => ({
...(jest.requireActual('@grafana/runtime') as any),
getBackendSrv: () => backendSrv,
getTemplateSrv: () => ({
replace: (val: string): string => {
return val;
},
}),
}));
describe('Postgres datasource', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('when performing testDatasource call', () => {
it('should return the error from the server', async () => {
setupFetchMock(
undefined,
throwError(() => ({
status: 400,
statusText: 'Bad Request',
data: {
results: {
meta: {
error: 'db query error: pq: password authentication failed for user "postgres"',
frames: [
{
schema: {
refId: 'meta',
meta: {
executedQueryString: 'SELECT 1',
},
fields: [],
},
data: {
values: [],
},
},
],
},
},
},
}))
);
const ds = new PostgresDatasource({ name: '', id: 0, jsonData: {} } as any);
const result = await ds.testDatasource();
expect(result.status).toEqual('error');
expect(result.message).toEqual('db query error: pq: password authentication failed for user "postgres"');
});
});
});
function setupFetchMock(response: any, mock?: any) {
const defaultMock = () => mock ?? of(createFetchResponse(response));
const fetchMock = jest.spyOn(backendSrv, 'fetch');
fetchMock.mockImplementation(defaultMock);
return fetchMock;
}