Skip to content

Commit d604536

Browse files
committed
feat: use testcontainers-go for clickhouse tests
1 parent eba6a80 commit d604536

File tree

4 files changed

+212
-21
lines changed

4 files changed

+212
-21
lines changed

clickhouse/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ Install the clickhouse implementation:
3131
go get github.com/gofiber/storage/clickhouse
3232
```
3333

34-
Before running or testing this implementation, you must ensure a Clickhouse cluster is available.
34+
### Running the tests
35+
36+
This module uses Testcontainers for Go to run integration tests, which will start a local instance of Clickhouse as a Docker container under the hood. To run the tests, you must have Docker installed on your machine.
37+
38+
### Local development
39+
40+
Before running this implementation, you must ensure a Clickhouse cluster is available.
3541
For local development, we recommend using the Clickhouse Docker image; it contains everything
3642
necessary for the client to operate correctly.
3743

clickhouse/clickhouse_test.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package clickhouse
22

33
import (
4+
"context"
5+
"strconv"
6+
"strings"
47
"testing"
58
"time"
69

710
"github.com/stretchr/testify/assert"
811
"github.com/stretchr/testify/require"
12+
13+
"github.com/testcontainers/testcontainers-go/modules/clickhouse"
14+
)
15+
16+
const (
17+
clickhouseImage = "clickhouse/clickhouse-server:23.3.8.21-alpine"
18+
clickhouseUser string = "default"
19+
clickhousePass string = "password"
20+
clickhouseDB string = "fiber"
921
)
1022

1123
type TestOrBench interface {
@@ -15,15 +27,39 @@ type TestOrBench interface {
1527
func getTestConnection(t TestOrBench, cfg Config) (*Storage, error) {
1628
t.Helper()
1729

30+
tt := t.(*testing.T)
31+
32+
ctx := context.Background()
33+
34+
c, err := clickhouse.Run(ctx,
35+
clickhouseImage,
36+
clickhouse.WithUsername(clickhouseUser),
37+
clickhouse.WithPassword(clickhousePass),
38+
clickhouse.WithDatabase(clickhouseDB),
39+
)
40+
require.NoError(tt, err)
41+
42+
hostPort, err := c.ConnectionHost(ctx)
43+
require.NoError(tt, err)
44+
45+
pair := strings.Split(hostPort, ":")
46+
port, err := strconv.Atoi(pair[1])
47+
require.NoError(tt, err)
48+
49+
// configure the client for the testcontainers clickhouse instance
50+
cfg.Host = pair[0]
51+
cfg.Port = port
52+
cfg.Username = clickhouseUser
53+
cfg.Password = clickhousePass
54+
cfg.Database = clickhouseDB
55+
1856
client, err := New(cfg)
1957

2058
return client, err
2159
}
2260

2361
func Test_Connection(t *testing.T) {
2462
_, err := getTestConnection(t, Config{
25-
Host: "127.0.0.1",
26-
Port: 9001,
2763
Engine: Memory,
2864
Table: "test_table",
2965
Clean: true,
@@ -34,8 +70,6 @@ func Test_Connection(t *testing.T) {
3470

3571
func Test_Set(t *testing.T) {
3672
client, err := getTestConnection(t, Config{
37-
Host: "127.0.0.1",
38-
Port: 9001,
3973
Engine: Memory,
4074
Table: "test_table",
4175
Clean: true,
@@ -49,8 +83,6 @@ func Test_Set(t *testing.T) {
4983

5084
func Test_Set_With_Exp(t *testing.T) {
5185
client, err := getTestConnection(t, Config{
52-
Host: "127.0.0.1",
53-
Port: 9001,
5486
Engine: Memory,
5587
Table: "test_table",
5688
Clean: true,
@@ -64,8 +96,6 @@ func Test_Set_With_Exp(t *testing.T) {
6496

6597
func Test_Get(t *testing.T) {
6698
client, err := getTestConnection(t, Config{
67-
Host: "127.0.0.1",
68-
Port: 9001,
6999
Engine: Memory,
70100
Table: "test_table",
71101
Clean: true,
@@ -85,8 +115,6 @@ func Test_Get(t *testing.T) {
85115

86116
func Test_Get_With_Exp(t *testing.T) {
87117
client, err := getTestConnection(t, Config{
88-
Host: "127.0.0.1",
89-
Port: 9001,
90118
Engine: Memory,
91119
Table: "test_table",
92120
Clean: true,
@@ -113,8 +141,6 @@ func Test_Get_With_Exp(t *testing.T) {
113141

114142
func Test_Delete(t *testing.T) {
115143
client, err := getTestConnection(t, Config{
116-
Host: "127.0.0.1",
117-
Port: 9001,
118144
Engine: Memory,
119145
Table: "test_table",
120146
Clean: true,
@@ -137,8 +163,6 @@ func Test_Delete(t *testing.T) {
137163

138164
func Test_Reset(t *testing.T) {
139165
client, err := getTestConnection(t, Config{
140-
Host: "127.0.0.1",
141-
Port: 9001,
142166
Engine: Memory,
143167
Table: "test_table",
144168
Clean: true,
@@ -164,8 +188,6 @@ func Benchmark_Clickhouse_Set(b *testing.B) {
164188
b.ResetTimer()
165189

166190
client, err := getTestConnection(b, Config{
167-
Host: "127.0.0.1",
168-
Port: 9001,
169191
Engine: Memory,
170192
Table: "test_table",
171193
Clean: true,
@@ -186,8 +208,6 @@ func Benchmark_Clickhouse_Get(b *testing.B) {
186208
b.ResetTimer()
187209

188210
client, err := getTestConnection(b, Config{
189-
Host: "127.0.0.1",
190-
Port: 9001,
191211
Engine: Memory,
192212
Table: "test_table",
193213
Clean: true,
@@ -210,8 +230,6 @@ func Benchmark_Clickhouse_Set_And_Delete(b *testing.B) {
210230
b.ResetTimer()
211231

212232
client, err := getTestConnection(b, Config{
213-
Host: "127.0.0.1",
214-
Port: 9001,
215233
Engine: Memory,
216234
Table: "test_table",
217235
Clean: true,

clickhouse/go.mod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,63 @@ go 1.21
55
require (
66
github.com/ClickHouse/clickhouse-go/v2 v2.26.0
77
github.com/stretchr/testify v1.9.0
8+
github.com/testcontainers/testcontainers-go/modules/clickhouse v0.33.0
89
)
910

1011
require (
12+
dario.cat/mergo v1.0.0 // indirect
13+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
1114
github.com/ClickHouse/ch-go v0.61.5 // indirect
15+
github.com/Microsoft/go-winio v0.6.2 // indirect
1216
github.com/andybalholm/brotli v1.1.0 // indirect
17+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
18+
github.com/containerd/containerd v1.7.18 // indirect
19+
github.com/containerd/log v0.1.0 // indirect
20+
github.com/containerd/platforms v0.2.1 // indirect
21+
github.com/cpuguy83/dockercfg v0.3.1 // indirect
1322
github.com/davecgh/go-spew v1.1.1 // indirect
23+
github.com/distribution/reference v0.6.0 // indirect
24+
github.com/docker/docker v27.1.1+incompatible // indirect
25+
github.com/docker/go-connections v0.5.0 // indirect
26+
github.com/docker/go-units v0.5.0 // indirect
27+
github.com/felixge/httpsnoop v1.0.4 // indirect
1428
github.com/go-faster/city v1.0.1 // indirect
1529
github.com/go-faster/errors v0.7.1 // indirect
30+
github.com/go-logr/logr v1.4.2 // indirect
31+
github.com/go-logr/stdr v1.2.2 // indirect
32+
github.com/go-ole/go-ole v1.2.6 // indirect
33+
github.com/gogo/protobuf v1.3.2 // indirect
1634
github.com/google/uuid v1.6.0 // indirect
1735
github.com/klauspost/compress v1.17.9 // indirect
36+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
37+
github.com/magiconair/properties v1.8.7 // indirect
38+
github.com/moby/docker-image-spec v1.3.1 // indirect
39+
github.com/moby/patternmatcher v0.6.0 // indirect
40+
github.com/moby/sys/sequential v0.5.0 // indirect
41+
github.com/moby/sys/user v0.1.0 // indirect
42+
github.com/moby/term v0.5.0 // indirect
43+
github.com/morikuni/aec v1.0.0 // indirect
44+
github.com/opencontainers/go-digest v1.0.0 // indirect
45+
github.com/opencontainers/image-spec v1.1.0 // indirect
1846
github.com/paulmach/orb v0.11.1 // indirect
1947
github.com/pierrec/lz4/v4 v4.1.21 // indirect
2048
github.com/pkg/errors v0.9.1 // indirect
2149
github.com/pmezard/go-difflib v1.0.0 // indirect
50+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
2251
github.com/segmentio/asm v1.2.0 // indirect
52+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
53+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
2354
github.com/shopspring/decimal v1.4.0 // indirect
55+
github.com/sirupsen/logrus v1.9.3 // indirect
56+
github.com/testcontainers/testcontainers-go v0.33.0 // indirect
57+
github.com/tklauser/go-sysconf v0.3.12 // indirect
58+
github.com/tklauser/numcpus v0.6.1 // indirect
59+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
60+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
2461
go.opentelemetry.io/otel v1.28.0 // indirect
62+
go.opentelemetry.io/otel/metric v1.28.0 // indirect
2563
go.opentelemetry.io/otel/trace v1.28.0 // indirect
64+
golang.org/x/crypto v0.24.0 // indirect
2665
golang.org/x/sys v0.21.0 // indirect
2766
gopkg.in/yaml.v3 v3.0.1 // indirect
2867
)

0 commit comments

Comments
 (0)