优化测试数据库连接管理
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:
@@ -16,10 +16,11 @@ import (
|
||||
|
||||
// TestAccountSoftDelete 测试账号软删除功能
|
||||
func TestAccountSoftDelete(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)
|
||||
|
||||
store := postgres.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -45,7 +46,7 @@ func TestAccountSoftDelete(t *testing.T) {
|
||||
|
||||
t.Run("使用 Unscoped 可以查到已删除账号", func(t *testing.T) {
|
||||
var found model.Account
|
||||
err := db.Unscoped().First(&found, account.ID).Error
|
||||
err := tx.Unscoped().First(&found, account.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, account.Username, found.Username)
|
||||
assert.NotNil(t, found.DeletedAt)
|
||||
@@ -68,10 +69,11 @@ func TestAccountSoftDelete(t *testing.T) {
|
||||
|
||||
// TestRoleSoftDelete 测试角色软删除功能
|
||||
func TestRoleSoftDelete(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)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试角色
|
||||
@@ -96,7 +98,7 @@ func TestRoleSoftDelete(t *testing.T) {
|
||||
|
||||
t.Run("使用 Unscoped 可以查到已删除角色", func(t *testing.T) {
|
||||
var found model.Role
|
||||
err := db.Unscoped().First(&found, role.ID).Error
|
||||
err := tx.Unscoped().First(&found, role.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, role.RoleName, found.RoleName)
|
||||
assert.NotNil(t, found.DeletedAt)
|
||||
@@ -105,10 +107,11 @@ func TestRoleSoftDelete(t *testing.T) {
|
||||
|
||||
// TestPermissionSoftDelete 测试权限软删除功能
|
||||
func TestPermissionSoftDelete(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)
|
||||
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试权限
|
||||
@@ -146,12 +149,13 @@ func TestPermissionSoftDelete(t *testing.T) {
|
||||
|
||||
// TestAccountRoleSoftDelete 测试账号-角色关联软删除功能
|
||||
func TestAccountRoleSoftDelete(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)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -216,12 +220,13 @@ func TestAccountRoleSoftDelete(t *testing.T) {
|
||||
|
||||
// TestRolePermissionSoftDelete 测试角色-权限关联软删除功能
|
||||
func TestRolePermissionSoftDelete(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)
|
||||
|
||||
roleStore := postgres.NewRoleStore(db)
|
||||
permissionStore := postgres.NewPermissionStore(db)
|
||||
rolePermissionStore := postgres.NewRolePermissionStore(db, redisClient)
|
||||
roleStore := postgres.NewRoleStore(tx)
|
||||
permissionStore := postgres.NewPermissionStore(tx)
|
||||
rolePermissionStore := postgres.NewRolePermissionStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试角色
|
||||
|
||||
Reference in New Issue
Block a user