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

@@ -22,16 +22,17 @@ func createWithdrawalTestContext(userID uint) context.Context {
}
func TestCommissionWithdrawalService_ListWithdrawalRequests(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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
walletStore := postgres.NewWalletStore(db, redisClient)
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
walletStore := postgres.NewWalletStore(tx, rdb)
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
t.Run("查询提现申请列表-空结果", func(t *testing.T) {
ctx := createWithdrawalTestContext(1)
@@ -79,16 +80,17 @@ func TestCommissionWithdrawalService_ListWithdrawalRequests(t *testing.T) {
}
func TestCommissionWithdrawalService_Approve(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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
walletStore := postgres.NewWalletStore(db, redisClient)
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
walletStore := postgres.NewWalletStore(tx, rdb)
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
t.Run("审批不存在的提现申请应失败", func(t *testing.T) {
ctx := createWithdrawalTestContext(1)
@@ -103,16 +105,17 @@ func TestCommissionWithdrawalService_Approve(t *testing.T) {
}
func TestCommissionWithdrawalService_Reject(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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
walletStore := postgres.NewWalletStore(db, redisClient)
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
walletStore := postgres.NewWalletStore(tx, rdb)
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
service := commission_withdrawal.New(db, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
service := commission_withdrawal.New(tx, shopStore, accountStore, walletStore, walletTransactionStore, commissionWithdrawalRequestStore)
t.Run("拒绝不存在的提现申请应失败", func(t *testing.T) {
ctx := createWithdrawalTestContext(1)