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

@@ -18,11 +18,12 @@ import (
// TestShopService_Create 测试创建店铺
func TestShopService_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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("创建一级店铺成功", func(t *testing.T) {
@@ -236,11 +237,12 @@ func TestShopService_Create(t *testing.T) {
// TestShopService_Update 测试更新店铺
func TestShopService_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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("更新店铺信息成功", func(t *testing.T) {
@@ -365,11 +367,12 @@ func TestShopService_Update(t *testing.T) {
// TestShopService_Disable 测试禁用店铺
func TestShopService_Disable(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)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("禁用店铺成功", func(t *testing.T) {
@@ -428,11 +431,12 @@ func TestShopService_Disable(t *testing.T) {
// TestShopService_Enable 测试启用店铺
func TestShopService_Enable(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)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("启用店铺成功", func(t *testing.T) {
@@ -500,11 +504,12 @@ func TestShopService_Enable(t *testing.T) {
// TestShopService_GetByID 测试获取店铺详情
func TestShopService_GetByID(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)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("获取存在的店铺", func(t *testing.T) {
@@ -548,11 +553,12 @@ func TestShopService_GetByID(t *testing.T) {
// TestShopService_List 测试查询店铺列表
func TestShopService_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)
shopStore := postgres.NewShopStore(db, redisClient)
accountStore := postgres.NewAccountStore(db, redisClient)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("查询店铺列表", func(t *testing.T) {
@@ -584,11 +590,12 @@ func TestShopService_List(t *testing.T) {
// TestShopService_GetSubordinateShopIDs 测试获取下级店铺 ID 列表
func TestShopService_GetSubordinateShopIDs(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)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("获取下级店铺 ID 列表", func(t *testing.T) {
@@ -648,11 +655,12 @@ func TestShopService_GetSubordinateShopIDs(t *testing.T) {
// TestShopService_Delete 测试删除店铺
func TestShopService_Delete(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)
shopStore := postgres.NewShopStore(tx, rdb)
accountStore := postgres.NewAccountStore(tx, rdb)
service := shop.New(shopStore, accountStore)
t.Run("删除店铺成功", func(t *testing.T) {