11import { expect } from 'chai' ;
2- import { createClient , type ClickHouseClient } from '../../src' ;
3- import type { ResponseJSON } from '../../src/clickhouse_types' ;
2+ import type { ResponseJSON } from '../../src' ;
3+ import { type ClickHouseClient } from '../../src' ;
4+ import {
5+ createTestClient ,
6+ getClickHouseTestEnvironment ,
7+ TestEnv ,
8+ } from '../utils/client' ;
9+ import { guid } from '../utils' ;
410
511describe ( 'command' , ( ) => {
612 let client : ClickHouseClient ;
13+ beforeEach ( ( ) => {
14+ client = createTestClient ( ) ;
15+ } ) ;
716 afterEach ( async ( ) => {
8- await client . command ( { query : 'DROP TABLE example' } ) ;
917 await client . close ( ) ;
1018 } ) ;
1119
1220 it ( 'sends a command to execute' , async ( ) => {
13- client = createClient ( ) ;
14- const ddl =
15- 'CREATE TABLE example (id UInt64, name String, sku Array(UInt8), timestamp DateTime) Engine = Memory' ;
21+ const { ddl, tableName, engine } = getDDL ( ) ;
1622
17- await client . command ( { query : ddl } ) ;
23+ const commandResult = await client . command ( {
24+ query : ddl ,
25+ format : 'TabSeparated' ,
26+ } ) ;
27+ await commandResult . text ( ) ;
1828
19- const result = await client . select ( {
20- query : `SELECT * from system.tables where name = 'example '` ,
29+ const selectResult = await client . select ( {
30+ query : `SELECT * from system.tables where name = '${ tableName } '` ,
2131 format : 'JSON' ,
2232 } ) ;
2333
24- const { data, rows } = await result . json <
34+ const { data, rows } = await selectResult . json <
2535 ResponseJSON < { name : string ; engine : string ; create_table_query : string } >
2636 > ( ) ;
2737
2838 expect ( rows ) . to . equal ( 1 ) ;
2939 const table = data [ 0 ] ;
30- expect ( table . name ) . equal ( 'example' ) ;
31- expect ( table . engine ) . equal ( 'Memory' ) ;
40+ expect ( table . name ) . equal ( tableName ) ;
41+ expect ( table . engine ) . equal ( engine ) ;
3242 expect ( table . create_table_query ) . to . be . a ( 'string' ) ;
3343 } ) ;
3444
3545 it ( 'does not swallow ClickHouse error' , ( done ) => {
36- client = createClient ( ) ;
37-
38- const ddl =
39- 'CREATE TABLE example (id UInt64, name String, sku Array(UInt8), timestamp DateTime) Engine = Memory' ;
40-
46+ const { ddl, tableName } = getDDL ( ) ;
4147 Promise . resolve ( )
4248 . then ( ( ) => client . command ( { query : ddl } ) )
4349 . then ( ( ) => client . command ( { query : ddl } ) )
4450 . catch ( ( e : any ) => {
4551 expect ( e . code ) . to . equal ( '57' ) ;
4652 expect ( e . type ) . to . equal ( 'TABLE_ALREADY_EXISTS' ) ;
4753 // TODO remove whitespace from end
48- expect ( e . message ) . equal ( ' Table default.example already exists. ' ) ;
54+ expect ( e . message ) . equal ( ` Table default.${ tableName } already exists. ` ) ;
4955 done ( ) ;
5056 } ) ;
5157 } ) ;
5258
5359 it . skip ( 'can specify a parameterized query' , async ( ) => {
54- client = createClient ( ) ;
55- await client . command ( {
56- query :
57- 'CREATE TABLE {table_name: String} (id UInt64, name String, sku Array(UInt8), timestamp DateTime) Engine = Memory' ,
60+ const commandResult = await client . command ( {
61+ query : '' ,
5862 query_params : {
5963 table_name : 'example' ,
6064 } ,
6165 } ) ;
66+ await commandResult . text ( ) ;
6267
68+ // FIXME: use different DDL based on the TestEnv
6369 const result = await client . select ( {
6470 query : `SELECT * from system.tables where name = 'example'` ,
6571 format : 'JSON' ,
@@ -74,3 +80,43 @@ describe('command', () => {
7480 expect ( table . name ) . to . equal ( 'example' ) ;
7581 } ) ;
7682} ) ;
83+
84+ function getDDL ( ) : {
85+ ddl : string ;
86+ tableName : string ;
87+ engine : string ;
88+ } {
89+ const env = getClickHouseTestEnvironment ( ) ;
90+ const tableName = `command_test_${ guid ( ) } ` ;
91+ switch ( env ) {
92+ // ENGINE can be omitted in the cloud statements:
93+ // it will use ReplicatedMergeTree and will add ON CLUSTER as well
94+ case TestEnv . Cloud : {
95+ const ddl = `
96+ CREATE TABLE ${ tableName }
97+ (id UInt64, name String, sku Array(UInt8), timestamp DateTime)
98+ ORDER BY (id)
99+ ` ;
100+ return { ddl, tableName, engine : 'ReplicatedMergeTree' } ;
101+ }
102+ case TestEnv . LocalSingleNode : {
103+ const ddl = `
104+ CREATE TABLE ${ tableName }
105+ (id UInt64, name String, sku Array(UInt8), timestamp DateTime)
106+ ENGINE = MergeTree()
107+ ORDER BY (id)
108+ ` ;
109+ return { ddl, tableName, engine : 'MergeTree' } ;
110+ }
111+
112+ case TestEnv . LocalCluster : {
113+ const ddl = `
114+ CREATE TABLE ${ tableName } ON CLUSTER '{cluster}'
115+ (id UInt64, name String, sku Array(UInt8), timestamp DateTime)
116+ ENGINE ReplicatedMergeTree('/clickhouse/{cluster}/tables/{database}/{table}/{shard}', '{replica}')
117+ ORDER BY (id)
118+ ` ;
119+ return { ddl, tableName, engine : 'ReplicatedMergeTree' } ;
120+ }
121+ }
122+ }
0 commit comments