Skip to content

Commit 897362a

Browse files
dorothyyzhclaude
andcommitted
refactor(tests): use redisx.MustStartTestSuite instead of raw testcontainers redis
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 20fd997 commit 897362a

4 files changed

Lines changed: 15 additions & 39 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ require (
66
github.com/go-sql-driver/mysql v1.8.1
77
github.com/jackc/pgx/v5 v5.9.2
88
github.com/pkg/errors v0.9.1
9-
github.com/qor5/x/v3 v3.2.1-0.20260512062513-2ee630cec6e0
9+
github.com/qor5/x/v3 v3.2.1-0.20260513132712-130e4f847f24
1010
github.com/redis/go-redis/v9 v9.16.0
1111
github.com/stretchr/testify v1.11.1
12-
github.com/testcontainers/testcontainers-go/modules/redis v0.42.0
1312
golang.org/x/sync v0.20.0
1413
gorm.io/gorm v1.31.1
1514
)
@@ -107,6 +106,7 @@ require (
107106
github.com/spf13/viper v1.19.0 // indirect
108107
github.com/subosito/gotenv v1.6.0 // indirect
109108
github.com/testcontainers/testcontainers-go v0.42.0 // indirect
109+
github.com/testcontainers/testcontainers-go/modules/redis v0.42.0 // indirect
110110
github.com/theplant/appkit v0.0.0-20250528023215-3d0d299dc4c6 // indirect
111111
github.com/theplant/cachex v0.0.0-20251210183652-8e675368cbc5 // indirect
112112
github.com/theplant/inject v1.2.2 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:Om
234234
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
235235
github.com/qor5/confx v0.0.0-20250426065316-0d28db5b4d54 h1:sO/saPkFgwfLaiCVTg+e9x6lAYBrqY91h1REu4NLMm0=
236236
github.com/qor5/confx v0.0.0-20250426065316-0d28db5b4d54/go.mod h1:03dPo1SHYn9sU57mH67Y1p9FIcglWaHr4i/xkeYmX4o=
237-
github.com/qor5/x/v3 v3.2.1-0.20260512062513-2ee630cec6e0 h1:IbgsrYwAlFduDI83u/wYgnDt+MxMfns2pyG1F41274E=
238-
github.com/qor5/x/v3 v3.2.1-0.20260512062513-2ee630cec6e0/go.mod h1:y/9Or1Q4jOHezDVdYu/m29ZbRcmSKqwebrdTiOh8ZZ8=
237+
github.com/qor5/x/v3 v3.2.1-0.20260513132712-130e4f847f24 h1:TBE21zvsfGFTgA/A2X19uSpmwRoJ1+6nO/RvUzZu1wM=
238+
github.com/qor5/x/v3 v3.2.1-0.20260513132712-130e4f847f24/go.mod h1:HElNR0JmewJro09DBZgOh+ZrzpEtYd69bfHvAAa1aOo=
239239
github.com/redis/go-redis/v9 v9.16.0 h1:OotgqgLSRCmzfqChbQyG1PHC3tLNR89DG4jdOERSEP4=
240240
github.com/redis/go-redis/v9 v9.16.0/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
241241
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=

ratelimiter_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@ package ratelimiter_test
22

33
import (
44
"context"
5-
"fmt"
65
"log"
7-
"strings"
86
"testing"
97
"time"
108

119
redis "github.com/redis/go-redis/v9"
10+
"github.com/qor5/x/v3/gormx"
11+
"github.com/qor5/x/v3/redisx"
1212
"github.com/stretchr/testify/require"
13-
testredis "github.com/testcontainers/testcontainers-go/modules/redis"
1413
"github.com/theplant/ratelimiter"
1514
"github.com/theplant/ratelimiter/redisrl"
1615
"github.com/theplant/ratelimiter/sqlrl"
17-
"github.com/qor5/x/v3/gormx"
1816
"gorm.io/gorm"
1917
)
2018

@@ -33,25 +31,15 @@ func TestMain(m *testing.M) {
3331
}
3432
}()
3533

36-
redisContainer, err := testredis.Run(ctx, "redis:7-alpine")
37-
if err != nil {
38-
panic(fmt.Errorf("failed to start redis container: %w", err))
39-
}
34+
redisTestSuite := redisx.MustStartTestSuite(ctx)
4035
defer func() {
41-
if err := redisContainer.Terminate(context.Background()); err != nil {
42-
log.Fatalf("Failed to terminate redis container: %v", err)
36+
if err := redisTestSuite.Stop(context.Background()); err != nil {
37+
log.Fatalf("Failed to stop redis test suite: %v", err)
4338
}
4439
}()
4540

46-
endpoint, err := redisContainer.ConnectionString(ctx)
47-
if err != nil {
48-
panic(fmt.Errorf("failed to get redis connection string: %w", err))
49-
}
50-
5141
db = testSuite.DB()
52-
redisCli = redis.NewClient(&redis.Options{
53-
Addr: strings.TrimPrefix(endpoint, "redis://"),
54-
})
42+
redisCli = redisTestSuite.Client()
5543
defer redisCli.Close()
5644

5745
// Create SQL rate limiter and migrate table

redisrl/redis_test.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,26 @@ package redisrl_test
22

33
import (
44
"context"
5-
"fmt"
6-
"strings"
75
"testing"
86

7+
"github.com/qor5/x/v3/redisx"
98
redis "github.com/redis/go-redis/v9"
109
"github.com/stretchr/testify/require"
11-
testredis "github.com/testcontainers/testcontainers-go/modules/redis"
1210
"github.com/theplant/ratelimiter/redisrl"
1311
)
1412

1513
var redisClient *redis.Client
1614

1715
func TestMain(m *testing.M) {
1816
ctx := context.Background()
19-
container, err := testredis.Run(ctx, "redis:7-alpine")
20-
if err != nil {
21-
panic(fmt.Errorf("failed to start redis container: %w", err))
22-
}
17+
suite := redisx.MustStartTestSuite(ctx)
2318
defer func() {
24-
if err := container.Terminate(context.Background()); err != nil {
25-
panic(fmt.Errorf("failed to terminate redis container: %w", err))
19+
if err := suite.Stop(context.Background()); err != nil {
20+
panic(err)
2621
}
2722
}()
2823

29-
endpoint, err := container.ConnectionString(ctx)
30-
if err != nil {
31-
panic(fmt.Errorf("failed to get redis connection string: %w", err))
32-
}
33-
34-
redisClient = redis.NewClient(&redis.Options{
35-
Addr: strings.TrimPrefix(endpoint, "redis://"),
36-
})
24+
redisClient = suite.Client()
3725
defer redisClient.Close()
3826

3927
m.Run()

0 commit comments

Comments
 (0)