新增通道服务相关测试用例
This commit is contained in:
40
pkg/testutil/db.go
Normal file
40
pkg/testutil/db.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/orm"
|
||||
q "platform/web/queries"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SetupDBTest 创建一个带有 sqlmock 的 GORM 数据库连接
|
||||
func SetupDBTest(t *testing.T) sqlmock.Sqlmock {
|
||||
|
||||
// 创建 sqlmock
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("创建sqlmock失败: %v", err)
|
||||
}
|
||||
|
||||
// 配置 gorm 连接
|
||||
gormDB, err := gorm.Open(postgres.New(postgres.Config{
|
||||
Conn: db,
|
||||
PreferSimpleProtocol: true, // 禁用 prepared statement 缓存
|
||||
}), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("gorm 打开数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
q.SetDefault(gormDB)
|
||||
orm.DB = gormDB
|
||||
|
||||
// 使用 t.Cleanup 确保测试结束后关闭数据库连接
|
||||
t.Cleanup(func() {
|
||||
db.Close()
|
||||
})
|
||||
|
||||
return mock
|
||||
}
|
||||
30
pkg/testutil/redis.go
Normal file
30
pkg/testutil/redis.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/rds"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// SetupRedisTest 创建一个测试用的Redis实例
|
||||
// 返回miniredis实例,使用t.Cleanup自动清理资源
|
||||
func SetupRedisTest(t *testing.T) *miniredis.Miniredis {
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
t.Fatalf("设置 miniredis 失败: %v", err)
|
||||
}
|
||||
|
||||
// 替换 Redis 客户端为测试客户端
|
||||
rds.Client = redis.NewClient(&redis.Options{
|
||||
Addr: mr.Addr(),
|
||||
})
|
||||
|
||||
// 使用t.Cleanup确保测试结束后恢复原始客户端并关闭miniredis
|
||||
t.Cleanup(func() {
|
||||
mr.Close()
|
||||
})
|
||||
|
||||
return mr
|
||||
}
|
||||
128
pkg/testutil/remote.go
Normal file
128
pkg/testutil/remote.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"platform/pkg/remote"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// MockCloudClient 是CloudClient接口的测试实现
|
||||
type MockCloudClient struct {
|
||||
// 存储预期结果的字段
|
||||
EdgesMock func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error)
|
||||
ConnectMock func(param remote.CloudConnectReq) error
|
||||
DisconnectMock func(param remote.CloudDisconnectReq) (int, error)
|
||||
AutoQueryMock func() (remote.CloudConnectResp, error)
|
||||
|
||||
// 记录调用历史
|
||||
EdgesCalls []remote.CloudEdgesReq
|
||||
ConnectCalls []remote.CloudConnectReq
|
||||
DisconnectCalls []remote.CloudDisconnectReq
|
||||
AutoQueryCalls int
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// 确保MockCloudClient实现了CloudClient接口
|
||||
var _ remote.CloudClient = (*MockCloudClient)(nil)
|
||||
|
||||
func (m *MockCloudClient) CloudEdges(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.EdgesCalls = append(m.EdgesCalls, param)
|
||||
if m.EdgesMock != nil {
|
||||
return m.EdgesMock(param)
|
||||
}
|
||||
return &remote.CloudEdgesResp{}, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ConnectCalls = append(m.ConnectCalls, param)
|
||||
if m.ConnectMock != nil {
|
||||
return m.ConnectMock(param)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.DisconnectCalls = append(m.DisconnectCalls, param)
|
||||
if m.DisconnectMock != nil {
|
||||
return m.DisconnectMock(param)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (m *MockCloudClient) CloudAutoQuery() (remote.CloudConnectResp, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.AutoQueryCalls++
|
||||
if m.AutoQueryMock != nil {
|
||||
return m.AutoQueryMock()
|
||||
}
|
||||
return remote.CloudConnectResp{}, nil
|
||||
}
|
||||
|
||||
// MockGatewayClient 是GatewayClient接口的测试实现
|
||||
type MockGatewayClient struct {
|
||||
// 存储预期结果的字段
|
||||
PortConfigsMock func(params []remote.PortConfigsReq) error
|
||||
PortActiveMock func(param ...remote.PortActiveReq) (map[string]remote.PortData, error)
|
||||
|
||||
// 记录调用历史
|
||||
PortConfigsCalls [][]remote.PortConfigsReq
|
||||
PortActiveCalls [][]remote.PortActiveReq
|
||||
|
||||
// 用于并发安全
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// 确保MockGatewayClient实现了GatewayClient接口
|
||||
var _ remote.GatewayClient = (*MockGatewayClient)(nil)
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.PortConfigsCalls = append(m.PortConfigsCalls, params)
|
||||
if m.PortConfigsMock != nil {
|
||||
return m.PortConfigsMock(params)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockGatewayClient) GatewayPortActive(param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.PortActiveCalls = append(m.PortActiveCalls, param)
|
||||
if m.PortActiveMock != nil {
|
||||
return m.PortActiveMock(param...)
|
||||
}
|
||||
return map[string]remote.PortData{}, nil
|
||||
}
|
||||
|
||||
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
|
||||
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
|
||||
mock := &MockCloudClient{}
|
||||
remote.Cloud = mock
|
||||
|
||||
return mock
|
||||
}
|
||||
|
||||
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
|
||||
func SetupGatewayClientMock(t *testing.T) *MockGatewayClient {
|
||||
mock := &MockGatewayClient{}
|
||||
remote.GatewayInitializer = func(url, username, password string) remote.GatewayClient {
|
||||
return mock
|
||||
}
|
||||
return mock
|
||||
}
|
||||
|
||||
// NewMockGatewayClient 创建一个新的MockGatewayClient
|
||||
// 保留此函数以保持向后兼容性
|
||||
func NewMockGatewayClient() *MockGatewayClient {
|
||||
return &MockGatewayClient{}
|
||||
}
|
||||
Reference in New Issue
Block a user