console/integration-tests/testkit/clickhouse.ts
jdolle eb6984efa0
Speed up app deployment 1000+ test (#7691)
Co-authored-by: Adam Benhassen <adambenhassen@hotmail.com>
2026-02-17 12:18:49 +01:00

52 lines
1.5 KiB
TypeScript

/* eslint-disable no-process-env */
import { ensureEnv } from './env';
import { getServiceHost } from './utils';
const user = ensureEnv('CLICKHOUSE_USER');
const password = ensureEnv('CLICKHOUSE_PASSWORD');
const credentials = Buffer.from(`${user}:${password}`).toString('base64');
export async function clickHouseQuery<T>(query: string): Promise<{
data: T[];
rows: number;
}> {
const clickhouseAddress = await getServiceHost('clickhouse', 8123);
const endpoint = `http://${clickhouseAddress}/?default_format=JSON`;
const response = await fetch(endpoint, {
method: 'POST',
body: query,
headers: {
Accept: 'application/json',
'Accept-Encoding': 'gzip',
Authorization: `Basic ${credentials}`,
},
});
if (response.status !== 200) {
const body = await response.text();
throw new Error(`Failed CH query ${query} with status ${response.status} and body:\n${body}`);
}
return response.json();
}
export async function clickHouseInsert<T>(query: string): Promise<void> {
const clickhouseAddress = await getServiceHost('clickhouse', 8123);
const endpoint = `http://${clickhouseAddress}/?default_format=JSON`;
const response = await fetch(endpoint, {
method: 'POST',
body: query,
headers: {
Accept: 'application/json',
'Accept-Encoding': 'gzip',
Authorization: `Basic ${credentials}`,
},
});
if (response.status !== 200) {
const body = await response.text();
throw new Error(`Failed CH query ${query} with status ${response.status} and body:\n${body}`);
}
}