优化测试数据库连接管理
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

@@ -26,13 +26,14 @@ func createContextWithUserType(userID uint, userType int) context.Context {
}
func TestPermissionService_CheckPermission_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)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
permStore := postgres.NewPermissionStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
t.Run("超级管理员自动拥有所有权限", func(t *testing.T) {
ctx := createContextWithUserType(1, constants.UserTypeSuperAdmin)
@@ -44,14 +45,15 @@ func TestPermissionService_CheckPermission_SuperAdmin(t *testing.T) {
}
func TestPermissionService_CheckPermission_NormalUser(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)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
permStore := postgres.NewPermissionStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
ctx := createContextWithUserType(100, constants.UserTypePlatform)
@@ -164,13 +166,14 @@ func TestPermissionService_CheckPermission_NormalUser(t *testing.T) {
}
func TestPermissionService_CheckPermission_NoRole(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)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
permStore := postgres.NewPermissionStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
t.Run("用户无角色应返回false", func(t *testing.T) {
ctx := createContextWithUserType(200, constants.UserTypePlatform)
@@ -182,14 +185,15 @@ func TestPermissionService_CheckPermission_NoRole(t *testing.T) {
}
func TestPermissionService_CheckPermission_RoleNoPermission(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)
permStore := postgres.NewPermissionStore(db)
accountRoleStore := postgres.NewAccountRoleStore(db, redisClient)
rolePermStore := postgres.NewRolePermissionStore(db, redisClient)
roleStore := postgres.NewRoleStore(db)
service := permission.New(permStore, accountRoleStore, rolePermStore, redisClient)
permStore := postgres.NewPermissionStore(tx)
accountRoleStore := postgres.NewAccountRoleStore(tx, rdb)
rolePermStore := postgres.NewRolePermissionStore(tx, rdb)
roleStore := postgres.NewRoleStore(tx)
service := permission.New(permStore, accountRoleStore, rolePermStore, rdb)
ctx := createContextWithUserType(300, constants.UserTypePlatform)