testutil

package
v4.0.0-alpha.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Factory = &MockFactory{}

Functions

func AssertJSON

func AssertJSON(t *testing.T, w *httptest.ResponseRecorder, expected map[string]any)

AssertJSON 断言 JSON 响应

func AssertStatus

func AssertStatus(t *testing.T, w *httptest.ResponseRecorder, expected int)

AssertStatus 断言状态码

func CleanupDB

func CleanupDB(db *gorm.DB)

CleanupDB 清理指定 GORM 连接中的所有表数据。 handler_test.go 和 service_test.go 可直接调用此函数。

func ParseResponse

func ParseResponse(t *testing.T, w *httptest.ResponseRecorder, v any)

ParseResponse 解析响应

func SetupTestRouter

func SetupTestRouter() *gin.Engine

SetupTestRouter 创建测试路由

func TestConfig

func TestConfig() *config.Config

TestConfig 创建测试配置

Types

type APITestClient

type APITestClient struct {
	Router *gin.Engine
	T      *testing.T
	Token  string
}

APITestClient API 测试客户端

func NewAPITestClient

func NewAPITestClient(t *testing.T, router *gin.Engine) *APITestClient

NewAPITestClient 创建 API 测试客户端

func (*APITestClient) DELETE

func (c *APITestClient) DELETE(path string) *httptest.ResponseRecorder

DELETE 发送 DELETE 请求

func (*APITestClient) GET

GET 发送 GET 请求

func (*APITestClient) POST

func (c *APITestClient) POST(path string, body any) *httptest.ResponseRecorder

POST 发送 POST 请求

func (*APITestClient) PUT

func (c *APITestClient) PUT(path string, body any) *httptest.ResponseRecorder

PUT 发送 PUT 请求

func (*APITestClient) Request

func (c *APITestClient) Request(method, path string, body any) *httptest.ResponseRecorder

Request 发送请求

func (*APITestClient) SetAuth

func (c *APITestClient) SetAuth(token string) *APITestClient

SetAuth 设置认证 Token

type MockFactory

type MockFactory struct{}

MockFactory 模拟数据工厂

func (*MockFactory) CreateAdminUser

func (f *MockFactory) CreateAdminUser(overrides ...func(*model.User)) *model.User

CreateAdminUser 创建管理员用户

func (*MockFactory) CreateAuthorizedKey

func (f *MockFactory) CreateAuthorizedKey(overrides ...func(*model.AuthorizedKey)) *model.AuthorizedKey

CreateAuthorizedKey 创建授权密钥

func (*MockFactory) CreateCoupon

func (f *MockFactory) CreateCoupon(overrides ...func(*model.Coupon)) *model.Coupon

CreateCoupon 创建优惠券

func (*MockFactory) CreateNode

func (f *MockFactory) CreateNode(overrides ...func(*model.Node)) *model.Node

CreateNode 创建测试节点

func (*MockFactory) CreateNodeProtocol

func (f *MockFactory) CreateNodeProtocol(nodeID uint, overrides ...func(*model.NodeProtocol)) *model.NodeProtocol

CreateNodeProtocol 创建节点协议配置

func (*MockFactory) CreateOrder

func (f *MockFactory) CreateOrder(userID, planID uint, overrides ...func(*model.Order)) *model.Order

CreateOrder 创建测试订单

func (*MockFactory) CreatePlan

func (f *MockFactory) CreatePlan(overrides ...func(*model.Plan)) *model.Plan

CreatePlan 创建测试套餐

func (*MockFactory) CreateSubscriptionGroup

func (f *MockFactory) CreateSubscriptionGroup(overrides ...func(*model.SubscriptionGroup)) *model.SubscriptionGroup

CreateSubscriptionGroup 创建订阅分组

func (*MockFactory) CreateUser

func (f *MockFactory) CreateUser(overrides ...func(*model.User)) *model.User

CreateUser 创建测试用户

func (*MockFactory) GenerateJWTToken

func (f *MockFactory) GenerateJWTToken(userID uint, isAdmin bool) string

GenerateJWTToken 生成测试 JWT Token

func (*MockFactory) HashPassword

func (f *MockFactory) HashPassword(password string) string

HashPassword 哈希密码

type TestDB

type TestDB struct {
	DB *gorm.DB
}

TestDB 测试数据库包装器

func SetupTestDB

func SetupTestDB() *TestDB

SetupTestDB 创建内存 SQLite 测试数据库

func (*TestDB) Cleanup

func (t *TestDB) Cleanup()

Cleanup 清理所有表数据,自动发现所有表并重置自增计数器。 不需要手动维护表名列表。

func (*TestDB) Close

func (t *TestDB) Close()

Close 关闭测试数据库

func (*TestDB) CloseWithError

func (t *TestDB) CloseWithError() error

CloseWithError closes the test database and returns cleanup failures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL