优化测试数据库连接管理
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:
@@ -24,18 +24,19 @@ func createMyCommissionTestContext(userID uint, shopID uint, userType int) conte
|
||||
}
|
||||
|
||||
func TestMyCommissionService_GetCommissionSummary(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)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -51,7 +52,7 @@ func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -79,18 +80,19 @@ func TestMyCommissionService_GetCommissionSummary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_CreateWithdrawalRequest(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)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -106,7 +108,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -133,7 +135,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
setting := &model.CommissionWithdrawalSetting{
|
||||
@@ -144,7 +146,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
setting.Creator = 1
|
||||
setting.Updater = 1
|
||||
err = db.Create(setting).Error
|
||||
err = tx.Create(setting).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -171,7 +173,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
wallet := &model.Wallet{
|
||||
@@ -180,7 +182,7 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
WalletType: constants.WalletTypeCommission,
|
||||
Balance: 5000,
|
||||
}
|
||||
err = db.Create(wallet).Error
|
||||
err = tx.Create(wallet).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -212,18 +214,19 @@ func TestMyCommissionService_CreateWithdrawalRequest(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyWithdrawalRequests(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)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -239,7 +242,7 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -266,7 +269,7 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -297,18 +300,19 @@ func TestMyCommissionService_ListMyWithdrawalRequests(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMyCommissionService_ListMyCommissionRecords(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)
|
||||
walletStore := postgres.NewWalletStore(db, redisClient)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(db, redisClient)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(db, redisClient)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(db, redisClient)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
walletStore := postgres.NewWalletStore(tx, rdb)
|
||||
commissionWithdrawalRequestStore := postgres.NewCommissionWithdrawalRequestStore(tx, rdb)
|
||||
commissionWithdrawalSettingStore := postgres.NewCommissionWithdrawalSettingStore(tx, rdb)
|
||||
commissionRecordStore := postgres.NewCommissionRecordStore(tx, rdb)
|
||||
walletTransactionStore := postgres.NewWalletTransactionStore(tx, rdb)
|
||||
|
||||
service := my_commission.New(
|
||||
db, shopStore, walletStore,
|
||||
tx, shopStore, walletStore,
|
||||
commissionWithdrawalRequestStore, commissionWithdrawalSettingStore,
|
||||
commissionRecordStore, walletTransactionStore,
|
||||
)
|
||||
@@ -324,7 +328,7 @@ func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
@@ -351,7 +355,7 @@ func TestMyCommissionService_ListMyCommissionRecords(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := createMyCommissionTestContext(1, shop.ID, constants.UserTypeAgent)
|
||||
|
||||
Reference in New Issue
Block a user