优化测试数据库连接管理
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:
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
// TestPersonalCustomerStore_Create 测试创建个人客户
|
||||
func TestPersonalCustomerStore_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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
@@ -66,10 +67,11 @@ func TestPersonalCustomerStore_Create(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByID 测试根据 ID 查询个人客户
|
||||
func TestPersonalCustomerStore_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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -97,10 +99,11 @@ func TestPersonalCustomerStore_GetByID(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByPhone 测试根据手机号查询
|
||||
func TestPersonalCustomerStore_GetByPhone(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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -120,7 +123,7 @@ func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
||||
IsPrimary: true,
|
||||
Status: constants.StatusEnabled,
|
||||
}
|
||||
err = db.Create(customerPhone).Error
|
||||
err = tx.Create(customerPhone).Error
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("根据手机号查询", func(t *testing.T) {
|
||||
@@ -138,10 +141,11 @@ func TestPersonalCustomerStore_GetByPhone(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_GetByWxOpenID 测试根据微信 OpenID 查询
|
||||
func TestPersonalCustomerStore_GetByWxOpenID(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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -169,10 +173,11 @@ func TestPersonalCustomerStore_GetByWxOpenID(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_Update 测试更新个人客户
|
||||
func TestPersonalCustomerStore_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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -224,10 +229,11 @@ func TestPersonalCustomerStore_Update(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_Delete 测试软删除个人客户
|
||||
func TestPersonalCustomerStore_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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
@@ -252,10 +258,11 @@ func TestPersonalCustomerStore_Delete(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_List 测试查询客户列表
|
||||
func TestPersonalCustomerStore_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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试客户
|
||||
@@ -291,10 +298,11 @@ func TestPersonalCustomerStore_List(t *testing.T) {
|
||||
|
||||
// TestPersonalCustomerStore_UniqueConstraints 测试唯一约束
|
||||
func TestPersonalCustomerStore_UniqueConstraints(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)
|
||||
|
||||
store := postgres.NewPersonalCustomerStore(db, redisClient)
|
||||
store := postgres.NewPersonalCustomerStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试客户
|
||||
|
||||
Reference in New Issue
Block a user