2026-05-10 23:12:26 +02:00
|
|
|
import { describe, expect, it, vi } from 'vitest';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { KtxPostgresHistoricSqlQueryClient } from './historic-sql-query-client.js';
|
|
|
|
|
import type { KtxPostgresPoolConfig, KtxPostgresPoolFactory } from './connector.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
describe('KtxPostgresHistoricSqlQueryClient', () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
it('executes parameterized read-only SQL through the native Postgres connector pool', async () => {
|
|
|
|
|
const queryCalls: Array<{ sql: string; params?: unknown[] }> = [];
|
|
|
|
|
const release = vi.fn();
|
|
|
|
|
const end = vi.fn(async () => {});
|
2026-05-10 23:51:24 +02:00
|
|
|
const poolFactory: KtxPostgresPoolFactory = {
|
|
|
|
|
createPool(_config: KtxPostgresPoolConfig) {
|
2026-05-10 23:12:26 +02:00
|
|
|
return {
|
|
|
|
|
async connect() {
|
|
|
|
|
return {
|
|
|
|
|
async query(sql: string, params?: unknown[]) {
|
|
|
|
|
queryCalls.push({ sql, params });
|
|
|
|
|
return {
|
|
|
|
|
fields: [{ name: 'answer', dataTypeID: 23 }],
|
|
|
|
|
rows: [{ answer: 42 }],
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
release,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
end,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-05-10 23:51:24 +02:00
|
|
|
const client = new KtxPostgresHistoricSqlQueryClient({
|
2026-05-10 23:12:26 +02:00
|
|
|
connectionId: 'warehouse',
|
|
|
|
|
connection: {
|
|
|
|
|
driver: 'postgres',
|
|
|
|
|
readonly: true,
|
|
|
|
|
url: 'postgresql://readonly:secret@pg.example.test/warehouse', // pragma: allowlist secret
|
|
|
|
|
},
|
|
|
|
|
poolFactory,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await expect(client.executeQuery('SELECT $1::int AS answer', [42])).resolves.toEqual({
|
|
|
|
|
headers: ['answer'],
|
|
|
|
|
rows: [[42]],
|
|
|
|
|
totalRows: 1,
|
|
|
|
|
});
|
|
|
|
|
expect(queryCalls).toEqual([{ sql: 'SELECT $1::int AS answer', params: [42] }]);
|
|
|
|
|
|
|
|
|
|
await client.cleanup();
|
|
|
|
|
expect(release).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(end).toHaveBeenCalledTimes(1);
|
|
|
|
|
});
|
|
|
|
|
});
|