@@ -137,70 +137,75 @@ func TestLogsClusterConfigOff(t *testing.T) {
137
137
}
138
138
139
139
func TestLogsTableEngineConfig (t * testing.T ) {
140
- teName := "CustomEngine"
141
- te := TableEngine {Name : teName }
142
- expectedTEValue := fmt .Sprintf ("%s()" , teName )
143
- testTableEngineConfig (t , te , expectedTEValue , true , func (t * testing.T , dsn string , fns ... func (* Config )) {
140
+ testTableEngineConfig (t , func (t * testing.T , dsn string , test tableEngineTestConfig , fns ... func (* Config )) {
144
141
exporter := newTestLogsExporter (t , dsn , fns ... )
145
- require . NotEmpty (t , exporter .cfg .TableEngine . Name )
142
+ test . sanityCheck (t , exporter .cfg .TableEngine )
146
143
})
147
144
}
148
145
149
- func TestLogsTableEngineConfigWithParams (t * testing.T ) {
150
- teName := "CustomEngine"
151
- teParams := "'/x/y/z', 'some_param', another_param, last_param"
152
- te := TableEngine {Name : teName , Params : teParams }
153
- expectedTEValue := fmt .Sprintf ("%s(%s)" , teName , teParams )
154
- testTableEngineConfig (t , te , expectedTEValue , true , func (t * testing.T , dsn string , fns ... func (* Config )) {
155
- exporter := newTestLogsExporter (t , dsn , fns ... )
156
- require .NotEmpty (t , exporter .cfg .TableEngine .Name )
157
- })
158
- }
146
+ func testTableEngineConfig (t * testing.T , completion tableEngineTestCompletion ) {
147
+ tests := []tableEngineTestConfig {
148
+ {
149
+ name : "no params" ,
150
+ teName : "CustomEngine" ,
151
+ teParams : "" ,
152
+ expectedTableName : "CustomEngine" ,
153
+ shouldSucceed : true ,
154
+ },
155
+ {
156
+ name : "with params" ,
157
+ teName : "CustomEngine" ,
158
+ teParams : "'/x/y/z', 'some_param', another_param, last_param" ,
159
+ expectedTableName : "CustomEngine" ,
160
+ shouldSucceed : true ,
161
+ },
162
+ {
163
+ name : "with empty name" ,
164
+ teName : "" ,
165
+ teParams : "" ,
166
+ expectedTableName : defaultTableEngineName ,
167
+ shouldSucceed : true ,
168
+ },
169
+ {
170
+ name : "fail" ,
171
+ teName : "CustomEngine" ,
172
+ teParams : "" ,
173
+ expectedTableName : defaultTableEngineName ,
174
+ shouldSucceed : false ,
175
+ },
176
+ }
159
177
160
- func TestLogsEmptyTableEngineConfig (t * testing.T ) {
161
- expectedTEValue := fmt .Sprintf ("%s()" , defaultTableEngineName )
162
- te := TableEngine {Name : "" }
163
- testTableEngineConfig (t , te , expectedTEValue , true , func (t * testing.T , dsn string , fns ... func (* Config )) {
164
- exporter := newTestLogsExporter (t , dsn , fns ... )
165
- require .Empty (t , exporter .cfg .TableEngine .Name )
166
- })
167
- }
178
+ for _ , tt := range tests {
179
+ te := TableEngine {Name : tt .teName , Params : tt .teParams }
180
+ expectedTEValue := fmt .Sprintf ("%s(%s)" , tt .expectedTableName , tt .teParams )
168
181
169
- func TestLogsTableEngineConfigFail (t * testing.T ) {
170
- teName := "CustomEngine"
171
- te := TableEngine {Name : teName }
172
- expectedTEValue := fmt .Sprintf ("%s()" , defaultTableEngineName )
173
- testTableEngineConfig (t , te , expectedTEValue , false , func (t * testing.T , dsn string , fns ... func (* Config )) {
174
- exporter := newTestLogsExporter (t , dsn , fns ... )
175
- require .NotEmpty (t , exporter .cfg .TableEngine .Name )
176
- })
177
- }
182
+ t .Run ("test table engine config " + tt .name , func (t * testing.T ) {
183
+ initClickhouseTestServer (t , func (query string , values []driver.Value ) error {
184
+ firstLine := getQueryFirstLine (query )
185
+ if ! strings .HasPrefix (strings .ToLower (firstLine ), "create table" ) {
186
+ return nil
187
+ }
178
188
179
- func testTableEngineConfig ( t * testing. T , tableEngine TableEngine , expectedTableEngineValue string , shouldSucceed bool , completion exporterValuesProvider ) {
180
- initClickhouseTestServer ( t , func ( query string , values []driver. Value ) error {
181
- firstLine := getQueryFirstLine ( query )
182
- if ! strings . HasPrefix ( strings . ToLower ( firstLine ), "create table" ) {
183
- return nil
184
- }
189
+ check := checkTableEngineQueryDefinition ( query , expectedTEValue )
190
+ if tt . shouldSucceed {
191
+ require . NoError ( t , check )
192
+ } else {
193
+ require . Error ( t , check )
194
+ }
185
195
186
- check := checkTableEngineQueryDefinition (query , expectedTableEngineValue )
187
- if shouldSucceed {
188
- require .NoError (t , check )
189
- } else {
190
- require .Error (t , check )
191
- }
196
+ return nil
197
+ })
192
198
193
- return nil
194
- })
199
+ var configMods []func (* Config )
200
+ if te .Name != "" {
201
+ configMods = append (configMods , func (cfg * Config ) {
202
+ cfg .TableEngine = te
203
+ })
204
+ }
195
205
196
- var configMods []func (* Config )
197
- if tableEngine .Name != "" {
198
- configMods = append (configMods , func (cfg * Config ) {
199
- cfg .TableEngine = tableEngine
206
+ completion (t , defaultEndpoint , tt , configMods ... )
200
207
})
201
208
}
202
-
203
- completion (t , defaultEndpoint , configMods ... )
204
209
}
205
210
206
211
func testClusterConfigOn (t * testing.T , completion exporterValuesProvider ) {
@@ -323,11 +328,28 @@ func initClickhouseTestServer(t *testing.T, recorder recorder) {
323
328
324
329
type recorder func (query string , values []driver.Value ) error
325
330
type exporterValuesProvider func (t * testing.T , dsn string , fns ... func (* Config ))
331
+ type tableEngineTestCompletion func (t * testing.T , dsn string , test tableEngineTestConfig , fns ... func (* Config ))
326
332
327
333
type testClickhouseDriver struct {
328
334
recorder recorder
329
335
}
330
336
337
+ type tableEngineTestConfig struct {
338
+ name string
339
+ teName string
340
+ teParams string
341
+ expectedTableName string
342
+ shouldSucceed bool
343
+ }
344
+
345
+ func (teTest tableEngineTestConfig ) sanityCheck (t * testing.T , te TableEngine ) {
346
+ if teTest .teName == "" {
347
+ require .Empty (t , te .Name )
348
+ } else {
349
+ require .NotEmpty (t , te .Name )
350
+ }
351
+ }
352
+
331
353
func (t * testClickhouseDriver ) Open (_ string ) (driver.Conn , error ) {
332
354
return & testClickhouseDriverConn {
333
355
recorder : t .recorder ,
0 commit comments