Skip to content

Commit fb528ce

Browse files
committed
feat: share db for eventrepo test to make things faster
1 parent d240c14 commit fb528ce

5 files changed

Lines changed: 141 additions & 75 deletions

File tree

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
path: bin/
7474
- name: Set bin as executable
7575
run: chmod +x bin/*
76-
- run: go mod download
76+
- run: go mod tidy
7777
- name: Run generate
7878
run: make generate
7979
- name: Porcelain

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25
55
require (
66
github.com/ClickHouse/clickhouse-go/v2 v2.40.1
77
github.com/DIMO-Network/clickhouse-infra v0.0.5
8-
github.com/DIMO-Network/cloudevent v0.1.4-0.20250829175417-ea1e179182a9
8+
github.com/DIMO-Network/cloudevent v0.1.4
99
github.com/DIMO-Network/server-garage v0.0.4
1010
github.com/DIMO-Network/shared v1.0.7
1111
github.com/aws/aws-sdk-go-v2 v1.38.0
@@ -20,7 +20,7 @@ require (
2020
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2
2121
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
2222
github.com/rs/zerolog v1.34.0
23-
github.com/stretchr/testify v1.10.0
23+
github.com/stretchr/testify v1.11.1
2424
github.com/swaggo/swag v1.16.6
2525
github.com/volatiletech/sqlboiler/v4 v4.19.1
2626
go.uber.org/mock v0.6.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ github.com/DATA-DOG/go-sqlmock v1.4.1 h1:ThlnYciV1iM/V0OSF/dtkqWb6xo5qITT1TJBG1M
1414
github.com/DATA-DOG/go-sqlmock v1.4.1/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
1515
github.com/DIMO-Network/clickhouse-infra v0.0.5 h1:zY1STwb9+vovpYXdjaeakE29pgZPIJLogJA+h3+IrbM=
1616
github.com/DIMO-Network/clickhouse-infra v0.0.5/go.mod h1:XS80lhSJNWBWGgZ+m4j7++zFj1wAXfmtV2gJfhGlabQ=
17-
github.com/DIMO-Network/cloudevent v0.1.4-0.20250829175417-ea1e179182a9 h1:YMYwpWsWU6zJDqdbW9oc1zX10y2WECPYDj1WsuhNkqc=
18-
github.com/DIMO-Network/cloudevent v0.1.4-0.20250829175417-ea1e179182a9/go.mod h1:Aaqfcg9FQod0CbsbPIz+8Ykr0udL0jNpO8AABPqEMaQ=
17+
github.com/DIMO-Network/cloudevent v0.1.4 h1:c6Sq4CyHt05V8OtnEXekUCRGfVuR1pFkJevfiKt1sYM=
18+
github.com/DIMO-Network/cloudevent v0.1.4/go.mod h1:Q2QpMEDYJ+VX0lz9SK2EUFxkuddV1XeF4aQ8LfegB68=
1919
github.com/DIMO-Network/server-garage v0.0.4 h1:K0wnQ2vJAtyK8lbINxKneVyqELORAu4Ut1ePZ3yb8cI=
2020
github.com/DIMO-Network/server-garage v0.0.4/go.mod h1:bdj0wKTjozjjD2w7Htk/rnzOv+jH+iBrhCuXOP/H00s=
2121
github.com/DIMO-Network/shared v1.0.7 h1:LfSgsqJ6R7EUyfo2GTfuhrCpoDcweJqe7eVOa4j7Xbo=
@@ -306,8 +306,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
306306
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
307307
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
308308
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
309-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
310-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
309+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
310+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
311311
github.com/swaggo/files/v2 v2.0.2 h1:Bq4tgS/yxLB/3nwOMcul5oLEUKa877Ykgz3CJMVbQKU=
312312
github.com/swaggo/files/v2 v2.0.2/go.mod h1:TVqetIzZsO9OhHX1Am9sRf9LdrFZqoK49N37KON/jr0=
313313
github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI=

pkg/eventrepo/db_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package eventrepo_test
2+
3+
import (
4+
"context"
5+
"sync"
6+
"sync/atomic"
7+
"testing"
8+
9+
"github.com/ClickHouse/clickhouse-go/v2"
10+
chconfig "github.com/DIMO-Network/clickhouse-infra/pkg/connect/config"
11+
"github.com/DIMO-Network/clickhouse-infra/pkg/container"
12+
"github.com/DIMO-Network/cloudevent"
13+
chindexer "github.com/DIMO-Network/cloudevent/pkg/clickhouse"
14+
"github.com/DIMO-Network/cloudevent/pkg/clickhouse/migrations"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
type TestContainer struct {
19+
container *container.Container
20+
onceSetup sync.Once
21+
refs atomic.Int64
22+
}
23+
24+
var globalTestContainer TestContainer
25+
26+
func (tc *TestContainer) TeardownIfLastTest(t *testing.T) {
27+
tc.refs.Add(1)
28+
t.Cleanup(func() {
29+
refs := tc.refs.Add(-1)
30+
if refs != 0 {
31+
return
32+
}
33+
tc.container.Terminate(context.Background())
34+
globalTestContainer.onceSetup = sync.Once{}
35+
globalTestContainer.refs = atomic.Int64{}
36+
globalTestContainer.container = nil
37+
})
38+
}
39+
40+
// setupClickHouseContainer starts a ClickHouse container for testing and returns the connection.
41+
func setupClickHouseContainer(t *testing.T) *container.Container {
42+
globalTestContainer.onceSetup.Do(func() {
43+
ctx := context.Background()
44+
settings := chconfig.Settings{
45+
User: "default",
46+
Database: "dimo",
47+
}
48+
49+
chContainer, err := container.CreateClickHouseContainer(ctx, settings)
50+
require.NoError(t, err)
51+
52+
chDB, err := chContainer.GetClickhouseAsDB()
53+
require.NoError(t, err)
54+
55+
err = migrations.RunGoose(ctx, []string{"up"}, chDB)
56+
require.NoError(t, err)
57+
globalTestContainer.container = chContainer
58+
})
59+
globalTestContainer.TeardownIfLastTest(t)
60+
return globalTestContainer.container
61+
}
62+
63+
// insertTestData inserts test data into ClickHouse.
64+
func insertTestData(t *testing.T, ctx context.Context, conn clickhouse.Conn, index *cloudevent.CloudEventHeader) string {
65+
values := chindexer.CloudEventToSlice(index)
66+
67+
err := conn.Exec(ctx, chindexer.InsertStmt, values...)
68+
require.NoError(t, err)
69+
return values[len(values)-1].(string)
70+
}

0 commit comments

Comments
 (0)