-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathclickhouse-container.test.ts
More file actions
99 lines (91 loc) · 3.33 KB
/
clickhouse-container.test.ts
File metadata and controls
99 lines (91 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container";
import { ClickHouseClient, createClient } from "@clickhouse/client";
import path from "path";
import { RandomUuid } from "testcontainers";
const CONFIG_FILE_MODE = parseInt("0644", 8);
describe("ClickhouseContainer", () => {
jest.setTimeout(240_000);
it("should work with defaults", async () => {
const container = await new ClickhouseContainer().start();
const client = createClickhouseContainerHttpClient(container);
await testExample(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom credentials", async () => {
const uuid = new RandomUuid();
const container = await new ClickhouseContainer()
.withUsername(`un-${uuid.nextUuid()}`)
.withPassword(`pass-${uuid.nextUuid()}`)
.start();
const client = createClickhouseContainerHttpClient(container);
await testExample(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom database and custom yaml config", async () => {
const uuid = new RandomUuid();
const CONFIG_PATH_YAML = "/etc/clickhouse-server/config.d/config.yaml";
const container = await new ClickhouseContainer()
.withDatabase(`db-${uuid.nextUuid()}`)
.withPassword("")
.withCopyFilesToContainer([
{ source: path.join("testdata", "config.yaml"), target: CONFIG_PATH_YAML, mode: CONFIG_FILE_MODE },
])
.start();
const client = createClickhouseContainerHttpClient(container);
await testExample(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom image and custom xml config example", async () => {
const CONFIG_PATH_XML = "/etc/clickhouse-server/config.d/config.xml";
const container = await new ClickhouseContainer("23.8-alpine")
.withPassword("")
.withCopyFilesToContainer([
{ source: path.join("testdata", "config.xml"), target: CONFIG_PATH_XML, mode: CONFIG_FILE_MODE },
])
.start();
const client = createClickhouseContainerHttpClient(container);
await testExample(client, container.getDatabase());
await client.close();
await container.stop();
});
function createClickhouseContainerHttpClient(container: StartedClickhouseContainer) {
return createClient({
host: container.getHttpUrl(),
username: container.getUsername(),
password: container.getPassword(),
});
}
async function testExample(client: ClickHouseClient, db: string) {
const tableName = "array_json_each_row";
await client.command({
query: `DROP TABLE IF EXISTS ${db}.${tableName}`,
});
await client.command({
query: `
CREATE TABLE ${db}.${tableName}
(id UInt64, name String)
ENGINE MergeTree()
ORDER BY (id)
`,
});
await client.insert({
table: `${db}.${tableName}`,
values: [
{ id: 42, name: "foo" },
{ id: 42, name: "bar" },
],
format: "JSONEachRow",
});
const rows = await client.query({
query: `SELECT * FROM ${db}.${tableName}`,
format: "JSONEachRow",
});
expect(await rows.json()).toEqual([
{ id: "42", name: "foo" },
{ id: "42", name: "bar" },
]);
}
});