-
-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathclickhouse-container.test.ts
More file actions
102 lines (93 loc) · 3.41 KB
/
clickhouse-container.test.ts
File metadata and controls
102 lines (93 loc) · 3.41 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
100
101
102
import { ClickhouseContainer, StartedClickhouseContainer } from "./clickhouse-container";
import { ClickHouseClient, createClient } from "@clickhouse/client";
import path from "path";
describe("ClickhouseContainer", () => {
jest.setTimeout(240_000);
it("should work with defaults", async () => {
const container = await new ClickhouseContainer().start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom credentials", async () => {
const container = await new ClickhouseContainer()
.withUsername(`un${(Math.random()*1000000) | 0}`)
.withPassword(`pass${(Math.random()*1000000) | 0}`)
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom database", async () => {
const container = await new ClickhouseContainer()
.withDatabase(`db${(Math.random()*1000000) | 0}`)
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom image", async () => {
const container = await new ClickhouseContainer("23.8-alpine").start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom xml config", async () => {
const container = await new ClickhouseContainer()
.withPassword("")
.withXmlConfigFile(path.join("testdata", "config.xml"))
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
it("should work with custom yaml config", async () => {
const container = await new ClickhouseContainer()
.withPassword("")
.withXmlConfigFile(path.join("testdata", "config.yaml"))
.start();
const client = createClickhouseContainerHttpClient(container);
await _test(client, container.getDatabase());
await client.close();
await container.stop();
});
function createClickhouseContainerHttpClient(container: StartedClickhouseContainer) {
return createClient({
host: `http://${container.getHost()}:${container.getHostPorts().http}`,
username: container.getUsername(),
password: container.getPassword(),
});
}
async function _test(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"}]);
}
});