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

@@ -56,13 +56,13 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
// 连接数据库
db, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
tx, err := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
require.NoError(t, err)
// 自动迁移
err = db.AutoMigrate(
err = tx.AutoMigrate(
&model.Account{},
&model.Role{},
&model.AccountRole{},
@@ -70,14 +70,14 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
require.NoError(t, err)
// 连接 Redis
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort.Port(),
})
// 初始化 Store 和 Service
accountStore := postgresStore.NewAccountStore(db, redisClient)
roleStore := postgresStore.NewRoleStore(db)
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
accountStore := postgresStore.NewAccountStore(tx, rdb)
roleStore := postgresStore.NewRoleStore(tx)
accountRoleStore := postgresStore.NewAccountRoleStore(tx, rdb)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
// 创建测试用户上下文
@@ -92,7 +92,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
// 创建测试角色
role := &model.Role{
@@ -100,7 +100,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
// 分配角色
ars, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
@@ -119,7 +119,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
// 创建多个测试角色
roles := make([]*model.Role, 3)
@@ -130,7 +130,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(roles[i])
tx.Create(roles[i])
roleIDs[i] = roles[i].ID
}
@@ -149,7 +149,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
// 创建并分配角色
role := &model.Role{
@@ -157,7 +157,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
@@ -178,7 +178,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
// 创建并分配角色
role := &model.Role{
@@ -186,7 +186,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
require.NoError(t, err)
@@ -197,7 +197,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
// 验证角色已被软删除
var ar model.AccountRole
err = db.Unscoped().Where("account_id = ? AND role_id = ?", account.ID, role.ID).First(&ar).Error
err = tx.Unscoped().Where("account_id = ? AND role_id = ?", account.ID, role.ID).First(&ar).Error
require.NoError(t, err)
assert.NotNil(t, ar.DeletedAt)
})
@@ -211,7 +211,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
// 创建测试角色
role := &model.Role{
@@ -219,7 +219,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
// 第一次分配
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})
@@ -231,7 +231,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
// 验证只有一条记录
var count int64
db.Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", account.ID, role.ID).Count(&count)
tx.Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", account.ID, role.ID).Count(&count)
assert.Equal(t, int64(1), count)
})
@@ -241,7 +241,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
_, err := accService.AssignRoles(userCtx, 99999, []uint{role.ID})
assert.Error(t, err)
@@ -255,7 +255,7 @@ func TestAccountRoleAssociation_AssignRoles(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
_, err := accService.AssignRoles(userCtx, account.ID, []uint{99999})
assert.Error(t, err)
@@ -293,18 +293,18 @@ func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
redisPort, _ := redisContainer.MappedPort(ctx, "6379")
// 设置环境
db, _ := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
tx, _ := gorm.Open(postgres.Open(pgConnStr), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
_ = db.AutoMigrate(&model.Account{}, &model.Role{}, &model.AccountRole{})
_ = tx.AutoMigrate(&model.Account{}, &model.Role{}, &model.AccountRole{})
redisClient := redis.NewClient(&redis.Options{
rdb := redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort.Port(),
})
accountStore := postgresStore.NewAccountStore(db, redisClient)
roleStore := postgresStore.NewRoleStore(db)
accountRoleStore := postgresStore.NewAccountRoleStore(db, redisClient)
accountStore := postgresStore.NewAccountStore(tx, rdb)
roleStore := postgresStore.NewRoleStore(tx)
accountRoleStore := postgresStore.NewAccountRoleStore(tx, rdb)
accService := accountService.New(accountStore, roleStore, accountRoleStore)
userCtx := middleware.SetUserContext(ctx, middleware.NewSimpleUserContext(1, constants.UserTypeSuperAdmin, 0))
@@ -318,14 +318,14 @@ func TestAccountRoleAssociation_SoftDelete(t *testing.T) {
UserType: constants.UserTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(account)
tx.Create(account)
role := &model.Role{
RoleName: "恢复角色测试",
RoleType: constants.RoleTypePlatform,
Status: constants.StatusEnabled,
}
db.Create(role)
tx.Create(role)
// 分配角色
_, err := accService.AssignRoles(userCtx, account.ID, []uint{role.ID})