优化测试数据库连接管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s

- 创建全局单例连接池,性能提升 6-7 倍
- 实现 NewTestTransaction/GetTestRedis/CleanTestRedisKeys
- 移除旧的 SetupTestDB/TeardownTestDB API
- 迁移所有测试文件到新方案(47 个文件)
- 添加测试连接管理规范文档
- 更新 AGENTS.md 和 README.md

性能对比:
- 旧方案:~71 秒(204 测试)
- 新方案:~10.5 秒(首次初始化 + 后续复用)
- 内存占用降低约 80%
- 网络连接数从 204 降至 1
This commit is contained in:
2026-01-22 14:38:43 +08:00
parent 46e4e5f4f1
commit b68e7ec013
47 changed files with 2529 additions and 986 deletions

View File

@@ -17,12 +17,13 @@ import (
// TestRoleAssignmentLimit_PlatformUser 测试平台用户可以分配多个角色(无限制)
func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
db, redisClient := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, redisClient)
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
accountStore := postgres.NewAccountStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
service := account.New(accountStore, roleStore, accountRoleStore)
ctx := context.Background()
@@ -36,7 +37,7 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, db.Create(platformUser).Error)
require.NoError(t, tx.Create(platformUser).Error)
// 创建 3 个平台角色
roles := []*model.Role{
@@ -45,7 +46,7 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
{RoleName: "财务", RoleType: constants.RoleTypePlatform, Status: constants.StatusEnabled},
}
for _, role := range roles {
require.NoError(t, db.Create(role).Error)
require.NoError(t, tx.Create(role).Error)
}
// 为平台用户分配 3 个角色(应该成功,因为平台用户无限制)
@@ -57,12 +58,13 @@ func TestRoleAssignmentLimit_PlatformUser(t *testing.T) {
// TestRoleAssignmentLimit_AgentUser 测试代理账号只能分配一个角色
func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
db, redisClient := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, redisClient)
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
accountStore := postgres.NewAccountStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
service := account.New(accountStore, roleStore, accountRoleStore)
ctx := context.Background()
@@ -76,7 +78,7 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
UserType: constants.UserTypeAgent,
Status: constants.StatusEnabled,
}
require.NoError(t, db.Create(agentAccount).Error)
require.NoError(t, tx.Create(agentAccount).Error)
// 创建 2 个客户角色
roles := []*model.Role{
@@ -84,7 +86,7 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
{RoleName: "二级代理", RoleType: constants.RoleTypeCustomer, Status: constants.StatusEnabled},
}
for _, role := range roles {
require.NoError(t, db.Create(role).Error)
require.NoError(t, tx.Create(role).Error)
}
// 先分配第一个角色(应该成功)
@@ -100,12 +102,13 @@ func TestRoleAssignmentLimit_AgentUser(t *testing.T) {
// TestRoleAssignmentLimit_EnterpriseUser 测试企业账号只能分配一个角色
func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
db, redisClient := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, redisClient)
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
accountStore := postgres.NewAccountStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
service := account.New(accountStore, roleStore, accountRoleStore)
ctx := context.Background()
@@ -119,7 +122,7 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
UserType: constants.UserTypeEnterprise,
Status: constants.StatusEnabled,
}
require.NoError(t, db.Create(enterpriseAccount).Error)
require.NoError(t, tx.Create(enterpriseAccount).Error)
// 创建 2 个客户角色
roles := []*model.Role{
@@ -127,7 +130,7 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
{RoleName: "企业高级", RoleType: constants.RoleTypeCustomer, Status: constants.StatusEnabled},
}
for _, role := range roles {
require.NoError(t, db.Create(role).Error)
require.NoError(t, tx.Create(role).Error)
}
// 先分配第一个角色(应该成功)
@@ -143,12 +146,13 @@ func TestRoleAssignmentLimit_EnterpriseUser(t *testing.T) {
// TestRoleAssignmentLimit_SuperAdmin 测试超级管理员不允许分配角色
func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
db, redisClient := testutils.SetupTestDB(t)
defer testutils.TeardownTestDB(t, db, redisClient)
tx := testutils.NewTestTransaction(t)
rdb := testutils.GetTestRedis(t)
testutils.CleanTestRedisKeys(t, rdb)
accountStore := postgres.NewAccountStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
accountStore := postgres.NewAccountStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
service := account.New(accountStore, roleStore, accountRoleStore)
ctx := context.Background()
@@ -162,7 +166,7 @@ func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
UserType: constants.UserTypeSuperAdmin,
Status: constants.StatusEnabled,
}
require.NoError(t, db.Create(superAdmin).Error)
require.NoError(t, tx.Create(superAdmin).Error)
// 创建一个平台角色
role := &model.Role{
@@ -170,7 +174,7 @@ func TestRoleAssignmentLimit_SuperAdmin(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
require.NoError(t, db.Create(role).Error)
require.NoError(t, tx.Create(role).Error)
// 尝试为超级管理员分配角色(应该失败)
_, err := service.AssignRoles(ctx, superAdmin.ID, []uint{role.ID})