优化测试数据库连接管理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 15s
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:
@@ -26,12 +26,13 @@ import (
|
||||
)
|
||||
|
||||
func TestPlatformAccountAPI_ListPlatformAccounts(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 := 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)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -57,7 +58,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
UserType: constants.UserTypeSuperAdmin,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(superAdmin)
|
||||
tx.Create(superAdmin)
|
||||
|
||||
platformUser := &model.Account{
|
||||
Username: testutils.GenerateUsername("platform_user", 2),
|
||||
@@ -66,7 +67,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(platformUser)
|
||||
tx.Create(platformUser)
|
||||
|
||||
agentUser := &model.Account{
|
||||
Username: testutils.GenerateUsername("agent_user", 3),
|
||||
@@ -75,7 +76,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
UserType: constants.UserTypeAgent,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(agentUser)
|
||||
tx.Create(agentUser)
|
||||
|
||||
t.Run("列表只返回平台账号和超级管理员", func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/api/admin/platform-accounts?page=1&page_size=20", nil)
|
||||
@@ -93,7 +94,7 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
assert.GreaterOrEqual(t, len(items), 2)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.Account{}).Where("user_type IN ?", []int{1, 2}).Count(&count)
|
||||
tx.Model(&model.Account{}).Where("user_type IN ?", []int{1, 2}).Count(&count)
|
||||
assert.GreaterOrEqual(t, count, int64(2))
|
||||
})
|
||||
|
||||
@@ -114,12 +115,13 @@ func TestPlatformAccountAPI_ListPlatformAccounts(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPlatformAccountAPI_UpdatePassword(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 := 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)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -145,7 +147,7 @@ func TestPlatformAccountAPI_UpdatePassword(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(testAccount)
|
||||
tx.Create(testAccount)
|
||||
|
||||
t.Run("成功修改密码", func(t *testing.T) {
|
||||
reqBody := dto.UpdatePasswordRequest{
|
||||
@@ -165,7 +167,7 @@ func TestPlatformAccountAPI_UpdatePassword(t *testing.T) {
|
||||
assert.Equal(t, 0, result.Code)
|
||||
|
||||
var updated model.Account
|
||||
db.First(&updated, testAccount.ID)
|
||||
tx.First(&updated, testAccount.ID)
|
||||
assert.NotEqual(t, "old_hashed_password", updated.Password)
|
||||
})
|
||||
|
||||
@@ -188,12 +190,13 @@ func TestPlatformAccountAPI_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPlatformAccountAPI_UpdateStatus(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 := 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)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -219,7 +222,7 @@ func TestPlatformAccountAPI_UpdateStatus(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(testAccount)
|
||||
tx.Create(testAccount)
|
||||
|
||||
t.Run("成功禁用账号", func(t *testing.T) {
|
||||
reqBody := dto.UpdateStatusRequest{
|
||||
@@ -234,7 +237,7 @@ func TestPlatformAccountAPI_UpdateStatus(t *testing.T) {
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var updated model.Account
|
||||
db.First(&updated, testAccount.ID)
|
||||
tx.First(&updated, testAccount.ID)
|
||||
assert.Equal(t, constants.StatusDisabled, updated.Status)
|
||||
})
|
||||
|
||||
@@ -251,18 +254,19 @@ func TestPlatformAccountAPI_UpdateStatus(t *testing.T) {
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var updated model.Account
|
||||
db.First(&updated, testAccount.ID)
|
||||
tx.First(&updated, testAccount.ID)
|
||||
assert.Equal(t, constants.StatusEnabled, updated.Status)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPlatformAccountAPI_AssignRoles(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 := 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)
|
||||
accountHandler := admin.NewAccountHandler(accService)
|
||||
|
||||
@@ -288,7 +292,7 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
UserType: constants.UserTypeSuperAdmin,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(superAdmin)
|
||||
tx.Create(superAdmin)
|
||||
|
||||
platformUser := &model.Account{
|
||||
Username: testutils.GenerateUsername("platform_user_role", 31),
|
||||
@@ -297,14 +301,14 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
UserType: constants.UserTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(platformUser)
|
||||
tx.Create(platformUser)
|
||||
|
||||
testRole := &model.Role{
|
||||
RoleName: testutils.GenerateUsername("测试角色", 30),
|
||||
RoleType: constants.RoleTypePlatform,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
db.Create(testRole)
|
||||
tx.Create(testRole)
|
||||
|
||||
t.Run("超级管理员禁止分配角色", func(t *testing.T) {
|
||||
reqBody := dto.AssignRolesRequest{
|
||||
@@ -337,7 +341,7 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", platformUser.ID, testRole.ID).Count(&count)
|
||||
tx.Model(&model.AccountRole{}).Where("account_id = ? AND role_id = ?", platformUser.ID, testRole.ID).Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
})
|
||||
|
||||
@@ -354,7 +358,7 @@ func TestPlatformAccountAPI_AssignRoles(t *testing.T) {
|
||||
assert.Equal(t, fiber.StatusOK, resp.StatusCode)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.AccountRole{}).Where("account_id = ?", platformUser.ID).Count(&count)
|
||||
tx.Model(&model.AccountRole{}).Where("account_id = ?", platformUser.ID).Count(&count)
|
||||
assert.Equal(t, int64(0), count)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user