优化测试数据库连接管理
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:
@@ -23,14 +23,15 @@ func createCustomerAccountTestContext(userID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_List(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)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("查询账号列表-空结果", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -59,7 +60,7 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -96,7 +97,7 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -122,14 +123,15 @@ func TestCustomerAccountService_List(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Create(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)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("新增代理商账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -144,7 +146,7 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.CreateCustomerAccountReq{
|
||||
@@ -176,7 +178,7 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req1 := &dto.CreateCustomerAccountReq{
|
||||
@@ -228,14 +230,15 @@ func TestCustomerAccountService_Create(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_Update(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)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("编辑账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -250,7 +253,7 @@ func TestCustomerAccountService_Update(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -286,14 +289,15 @@ func TestCustomerAccountService_Update(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_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 := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("修改密码", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -308,7 +312,7 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -324,7 +328,7 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, "OldPass123", account.Password)
|
||||
assert.NotEqual(t, "NewPass456", account.Password)
|
||||
@@ -339,14 +343,15 @@ func TestCustomerAccountService_UpdatePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCustomerAccountService_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 := postgres.NewAccountStore(db, redisClient)
|
||||
shopStore := postgres.NewShopStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
accountStore := postgres.NewAccountStore(tx, rdb)
|
||||
shopStore := postgres.NewShopStore(tx, rdb)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
|
||||
service := customer_account.New(db, accountStore, shopStore, enterpriseStore)
|
||||
service := customer_account.New(tx, accountStore, shopStore, enterpriseStore)
|
||||
|
||||
t.Run("禁用账号", func(t *testing.T) {
|
||||
ctx := createCustomerAccountTestContext(1)
|
||||
@@ -361,7 +366,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -377,7 +382,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusDisabled, account.Status)
|
||||
})
|
||||
@@ -395,7 +400,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
}
|
||||
shop.Creator = 1
|
||||
shop.Updater = 1
|
||||
err := db.Create(shop).Error
|
||||
err := tx.Create(shop).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
createReq := &dto.CreateCustomerAccountReq{
|
||||
@@ -414,7 +419,7 @@ func TestCustomerAccountService_UpdateStatus(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var account model.Account
|
||||
err = db.First(&account, created.ID).Error
|
||||
err = tx.First(&account, created.ID).Error
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, constants.StatusEnabled, account.Status)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user