优化测试数据库连接管理
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,13 +24,14 @@ func createEnterpriseCardTestContext(userID uint, shopID uint) context.Context {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCardsPreview(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)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权预检-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -64,7 +65,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -87,7 +88,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -112,7 +113,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -122,7 +123,7 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsPreviewReq{
|
||||
@@ -138,13 +139,14 @@ func TestEnterpriseCardService_AllocateCardsPreview(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_AllocateCards(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)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("授权卡-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -167,7 +169,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -177,7 +179,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsReq{
|
||||
@@ -200,7 +202,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -210,7 +212,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.AllocateCardsReq{
|
||||
@@ -224,7 +226,7 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var count int64
|
||||
db.Model(&model.EnterpriseCardAuthorization{}).
|
||||
tx.Model(&model.EnterpriseCardAuthorization{}).
|
||||
Where("enterprise_id = ? AND iot_card_id = ?", ent.ID, card.ID).
|
||||
Count(&count)
|
||||
assert.Equal(t, int64(1), count)
|
||||
@@ -232,13 +234,14 @@ func TestEnterpriseCardService_AllocateCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_RecallCards(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)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("回收授权-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -261,7 +264,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -271,7 +274,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.RecallCardsReq{
|
||||
@@ -294,7 +297,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -304,7 +307,7 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -324,13 +327,14 @@ func TestEnterpriseCardService_RecallCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_ListCards(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)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("查询企业卡列表-企业不存在应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -354,7 +358,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
req := &dto.EnterpriseCardListReq{
|
||||
@@ -378,7 +382,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -388,7 +392,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -420,7 +424,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -430,7 +434,7 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -453,13 +457,14 @@ func TestEnterpriseCardService_ListCards(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEnterpriseCardService_SuspendAndResumeCard(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)
|
||||
|
||||
enterpriseStore := postgres.NewEnterpriseStore(db, redisClient)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(db, redisClient)
|
||||
enterpriseStore := postgres.NewEnterpriseStore(tx, rdb)
|
||||
enterpriseCardAuthStore := postgres.NewEnterpriseCardAuthorizationStore(tx, rdb)
|
||||
|
||||
service := enterprise_card.New(db, enterpriseStore, enterpriseCardAuthStore)
|
||||
service := enterprise_card.New(tx, enterpriseStore, enterpriseCardAuthStore)
|
||||
|
||||
t.Run("停机-未授权的卡应失败", func(t *testing.T) {
|
||||
ctx := createEnterpriseCardTestContext(1, 1)
|
||||
@@ -471,7 +476,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -481,7 +486,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
Status: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
err = service.SuspendCard(ctx, ent.ID, card.ID)
|
||||
@@ -498,7 +503,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
}
|
||||
ent.Creator = 1
|
||||
ent.Updater = 1
|
||||
err := db.Create(ent).Error
|
||||
err := tx.Create(ent).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
shopID := uint(1)
|
||||
@@ -509,7 +514,7 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
NetworkStatus: 1,
|
||||
ShopID: &shopID,
|
||||
}
|
||||
err = db.Create(card).Error
|
||||
err = tx.Create(card).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
allocReq := &dto.AllocateCardsReq{
|
||||
@@ -522,14 +527,14 @@ func TestEnterpriseCardService_SuspendAndResumeCard(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
var suspendedCard model.IotCard
|
||||
db.First(&suspendedCard, card.ID)
|
||||
tx.First(&suspendedCard, card.ID)
|
||||
assert.Equal(t, 0, suspendedCard.NetworkStatus)
|
||||
|
||||
err = service.ResumeCard(ctx, ent.ID, card.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
var resumedCard model.IotCard
|
||||
db.First(&resumedCard, card.ID)
|
||||
tx.First(&resumedCard, card.ID)
|
||||
assert.Equal(t, 1, resumedCard.NetworkStatus)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user