-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcommand.test.ts
122 lines (109 loc) · 3.5 KB
/
command.test.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { expect } from 'chai';
import type { ResponseJSON } from '../../src';
import { type ClickHouseClient } from '../../src';
import {
createTestClient,
getClickHouseTestEnvironment,
TestEnv,
} from '../utils/client';
import { guid } from '../utils';
describe('command', () => {
let client: ClickHouseClient;
beforeEach(() => {
client = createTestClient();
});
afterEach(async () => {
await client.close();
});
it('sends a command to execute', async () => {
const { ddl, tableName, engine } = getDDL();
const commandResult = await client.command({
query: ddl,
format: 'TabSeparated',
});
await commandResult.text();
const selectResult = await client.select({
query: `SELECT * from system.tables where name = '${tableName}'`,
format: 'JSON',
});
const { data, rows } = await selectResult.json<
ResponseJSON<{ name: string; engine: string; create_table_query: string }>
>();
expect(rows).to.equal(1);
const table = data[0];
expect(table.name).equal(tableName);
expect(table.engine).equal(engine);
expect(table.create_table_query).to.be.a('string');
});
it('does not swallow ClickHouse error', (done) => {
const { ddl, tableName } = getDDL();
Promise.resolve()
.then(() => client.command({ query: ddl }))
.then(() => client.command({ query: ddl }))
.catch((e: any) => {
expect(e.code).to.equal('57');
expect(e.type).to.equal('TABLE_ALREADY_EXISTS');
// TODO remove whitespace from end
expect(e.message).equal(`Table default.${tableName} already exists. `);
done();
});
});
it.skip('can specify a parameterized query', async () => {
const commandResult = await client.command({
query: '',
query_params: {
table_name: 'example',
},
});
await commandResult.text();
// FIXME: use different DDL based on the TestEnv
const result = await client.select({
query: `SELECT * from system.tables where name = 'example'`,
format: 'JSON',
});
const { data, rows } = await result.json<
ResponseJSON<{ name: string; engine: string; create_table_query: string }>
>();
expect(rows).to.equal(1);
const table = data[0];
expect(table.name).to.equal('example');
});
});
function getDDL(): {
ddl: string;
tableName: string;
engine: string;
} {
const env = getClickHouseTestEnvironment();
const tableName = `command_test_${guid()}`;
switch (env) {
// ENGINE can be omitted in the cloud statements:
// it will use ReplicatedMergeTree and will add ON CLUSTER as well
case TestEnv.Cloud: {
const ddl = `
CREATE TABLE ${tableName}
(id UInt64, name String, sku Array(UInt8), timestamp DateTime)
ORDER BY (id)
`;
return { ddl, tableName, engine: 'ReplicatedMergeTree' };
}
case TestEnv.LocalSingleNode: {
const ddl = `
CREATE TABLE ${tableName}
(id UInt64, name String, sku Array(UInt8), timestamp DateTime)
ENGINE = MergeTree()
ORDER BY (id)
`;
return { ddl, tableName, engine: 'MergeTree' };
}
case TestEnv.LocalCluster: {
const ddl = `
CREATE TABLE ${tableName} ON CLUSTER '{cluster}'
(id UInt64, name String, sku Array(UInt8), timestamp DateTime)
ENGINE ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/{table}/{shard}', '{replica}')
ORDER BY (id)
`;
return { ddl, tableName, engine: 'ReplicatedMergeTree' };
}
}
}