Skip to content

Commit 530edf6

Browse files
author
Oleksandr Kostenko
committed
Refactor: unify table engine tests.
1 parent 7d2ff65 commit 530edf6

File tree

3 files changed

+78
-123
lines changed

3 files changed

+78
-123
lines changed

exporter/clickhouseexporter/exporter_logs_test.go

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -137,70 +137,75 @@ func TestLogsClusterConfigOff(t *testing.T) {
137137
}
138138

139139
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)) {
144141
exporter := newTestLogsExporter(t, dsn, fns...)
145-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
142+
test.sanityCheck(t, exporter.cfg.TableEngine)
146143
})
147144
}
148145

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+
}
159177

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)
168181

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+
}
178188

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+
}
185195

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+
})
192198

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+
}
195205

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...)
200207
})
201208
}
202-
203-
completion(t, defaultEndpoint, configMods...)
204209
}
205210

206211
func testClusterConfigOn(t *testing.T, completion exporterValuesProvider) {
@@ -323,11 +328,28 @@ func initClickhouseTestServer(t *testing.T, recorder recorder) {
323328

324329
type recorder func(query string, values []driver.Value) error
325330
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))
326332

327333
type testClickhouseDriver struct {
328334
recorder recorder
329335
}
330336

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+
331353
func (t *testClickhouseDriver) Open(_ string) (driver.Conn, error) {
332354
return &testClickhouseDriverConn{
333355
recorder: t.recorder,

exporter/clickhouseexporter/exporter_metrics_test.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -125,42 +125,9 @@ func TestMetricsClusterConfigOff(t *testing.T) {
125125
}
126126

127127
func TestMetricsTableEngineConfig(t *testing.T) {
128-
teName := "CustomEngine"
129-
te := TableEngine{Name: teName}
130-
expectedTEValue := fmt.Sprintf("%s()", teName)
131-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
128+
testTableEngineConfig(t, func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config)) {
132129
exporter := newTestMetricsExporter(t, dsn, fns...)
133-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
134-
})
135-
}
136-
137-
func TestMetricsTableEngineConfigWithParams(t *testing.T) {
138-
teName := "CustomEngine"
139-
teParams := "'/x/y/z', 'some_param', another_param, last_param"
140-
te := TableEngine{Name: teName, Params: teParams}
141-
expectedTEValue := fmt.Sprintf("%s(%s)", teName, teParams)
142-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
143-
exporter := newTestMetricsExporter(t, dsn, fns...)
144-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
145-
})
146-
}
147-
148-
func TestMetricsEmptyTableEngineConfig(t *testing.T) {
149-
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
150-
te := TableEngine{Name: ""}
151-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
152-
exporter := newTestMetricsExporter(t, dsn, fns...)
153-
require.Empty(t, exporter.cfg.TableEngine.Name)
154-
})
155-
}
156-
157-
func TestMetricsTableEngineConfigFail(t *testing.T) {
158-
teName := "CustomEngine"
159-
te := TableEngine{Name: teName}
160-
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
161-
testTableEngineConfig(t, te, expectedTEValue, false, func(t *testing.T, dsn string, fns ...func(*Config)) {
162-
exporter := newTestMetricsExporter(t, dsn, fns...)
163-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
130+
test.sanityCheck(t, exporter.cfg.TableEngine)
164131
})
165132
}
166133

exporter/clickhouseexporter/exporter_traces_test.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"testing"
1111
"time"
12-
"fmt"
1312

1413
"github.com/stretchr/testify/require"
1514
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -64,42 +63,9 @@ func TestTracesClusterConfigOff(t *testing.T) {
6463
}
6564

6665
func TestTracesTableEngineConfig(t *testing.T) {
67-
teName := "CustomEngine"
68-
te := TableEngine{Name: teName}
69-
expectedTEValue := fmt.Sprintf("%s()", teName)
70-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
66+
testTableEngineConfig(t, func(t *testing.T, dsn string, test tableEngineTestConfig, fns ...func(*Config)) {
7167
exporter := newTestTracesExporter(t, dsn, fns...)
72-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
73-
})
74-
}
75-
76-
func TestTracesTableEngineConfigWithParams(t *testing.T) {
77-
teName := "CustomEngine"
78-
teParams := "'/x/y/z', 'some_param', another_param, last_param"
79-
te := TableEngine{Name: teName, Params: teParams}
80-
expectedTEValue := fmt.Sprintf("%s(%s)", teName, teParams)
81-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
82-
exporter := newTestTracesExporter(t, dsn, fns...)
83-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
84-
})
85-
}
86-
87-
func TestTracesEmptyTableEngineConfig(t *testing.T) {
88-
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
89-
te := TableEngine{Name: ""}
90-
testTableEngineConfig(t, te, expectedTEValue, true, func(t *testing.T, dsn string, fns ...func(*Config)) {
91-
exporter := newTestTracesExporter(t, dsn, fns...)
92-
require.Empty(t, exporter.cfg.TableEngine.Name)
93-
})
94-
}
95-
96-
func TestTracesTableEngineConfigFail(t *testing.T) {
97-
teName := "CustomEngine"
98-
te := TableEngine{Name: teName}
99-
expectedTEValue := fmt.Sprintf("%s()", defaultTableEngineName)
100-
testTableEngineConfig(t, te, expectedTEValue, false, func(t *testing.T, dsn string, fns ...func(*Config)) {
101-
exporter := newTestTracesExporter(t, dsn, fns...)
102-
require.NotEmpty(t, exporter.cfg.TableEngine.Name)
68+
test.sanityCheck(t, exporter.cfg.TableEngine)
10369
})
10470
}
10571

0 commit comments

Comments
 (0)