61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
|
|
//go:build integration
|
||
|
|
|
||
|
|
package repository
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
"github.com/stretchr/testify/suite"
|
||
|
|
)
|
||
|
|
|
||
|
|
// IntegrationRedisSuite Redis 集成测试基础套件
|
||
|
|
// 所有 Redis 集成测试应嵌入此套件
|
||
|
|
type IntegrationRedisSuite struct {
|
||
|
|
suite.Suite
|
||
|
|
rdb *redis.Client
|
||
|
|
ctx context.Context
|
||
|
|
host string
|
||
|
|
port string
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetupSuite 连接 Redis
|
||
|
|
func (s *IntegrationRedisSuite) SetupSuite() {
|
||
|
|
s.ctx = context.Background()
|
||
|
|
s.host = "localhost"
|
||
|
|
s.port = "6379"
|
||
|
|
|
||
|
|
s.rdb = redis.NewClient(&redis.Options{
|
||
|
|
Addr: s.host + ":" + s.port,
|
||
|
|
DialTimeout: 5 * time.Second,
|
||
|
|
ReadTimeout: 3 * time.Second,
|
||
|
|
WriteTimeout: 3 * time.Second,
|
||
|
|
PoolSize: 10,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// SetupTest 每个测试前清空数据库
|
||
|
|
func (s *IntegrationRedisSuite) SetupTest() {
|
||
|
|
if s.rdb == nil {
|
||
|
|
s.T().Skip("Redis not available, skipping integration test")
|
||
|
|
}
|
||
|
|
s.rdb.FlushDB(s.ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
// TearDownSuite 关闭连接
|
||
|
|
func (s *IntegrationRedisSuite) TearDownSuite() {
|
||
|
|
if s.rdb != nil {
|
||
|
|
s.rdb.Close()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Redis 返回的辅助方法
|
||
|
|
func (s *IntegrationRedisSuite) Redis() *redis.Client {
|
||
|
|
return s.rdb
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *IntegrationRedisSuite) Context() context.Context {
|
||
|
|
return s.ctx
|
||
|
|
}
|