优化测试数据库连接管理
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 (
|
||||
|
||||
// TestAccountModel_Create 测试创建账号
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("创建 root 账号", func(t *testing.T) {
|
||||
@@ -59,10 +60,11 @@ func TestAccountModel_Create(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByID 测试根据 ID 查询账号
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -92,10 +94,11 @@ func TestAccountModel_GetByID(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByUsername 测试根据用户名查询账号
|
||||
func TestAccountModel_GetByUsername(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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -123,10 +126,11 @@ func TestAccountModel_GetByUsername(t *testing.T) {
|
||||
|
||||
// TestAccountModel_GetByPhone 测试根据手机号查询账号
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -154,10 +158,11 @@ func TestAccountModel_GetByPhone(t *testing.T) {
|
||||
|
||||
// TestAccountModel_Update 测试更新账号
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
@@ -187,10 +192,11 @@ func TestAccountModel_Update(t *testing.T) {
|
||||
|
||||
// TestAccountModel_List 测试查询账号列表
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建多个测试账号
|
||||
@@ -227,10 +233,11 @@ func TestAccountModel_List(t *testing.T) {
|
||||
|
||||
// TestAccountModel_UniqueConstraints 测试唯一约束
|
||||
func TestAccountModel_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.NewAccountStore(db, redisClient)
|
||||
store := postgres.NewAccountStore(tx, rdb)
|
||||
ctx := context.Background()
|
||||
|
||||
// 创建测试账号
|
||||
|
||||
Reference in New Issue
Block a user