实现审计覆盖门禁与外部集成日志闭环
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 10m19s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 10m19s
This commit is contained in:
@@ -15,6 +15,19 @@ import (
|
||||
|
||||
// NewPostgresTransaction 创建自动回滚的 PostgreSQL 测试事务。
|
||||
func NewPostgresTransaction(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := NewPostgresDatabase(t)
|
||||
tx := db.Begin()
|
||||
if tx.Error != nil {
|
||||
t.Fatalf("开启测试事务失败:%v", tx.Error)
|
||||
}
|
||||
t.Cleanup(func() { _ = tx.Rollback().Error })
|
||||
return tx
|
||||
}
|
||||
|
||||
// NewPostgresDatabase 创建自动关闭、可使用多个连接的 PostgreSQL 测试数据库。
|
||||
// 仅在需要验证真实并发条件更新时使用;普通集成测试仍优先使用自动回滚事务。
|
||||
func NewPostgresDatabase(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
if os.Getenv("JUNHONG_DATABASE_HOST") == "" {
|
||||
t.Skip("未加载 .env.local,跳过依赖真实 PostgreSQL 的集成测试")
|
||||
@@ -27,17 +40,12 @@ func NewPostgresTransaction(t *testing.T) *gorm.DB {
|
||||
if err != nil {
|
||||
t.Fatalf("连接 PostgreSQL 失败:%v", err)
|
||||
}
|
||||
tx := db.Begin()
|
||||
if tx.Error != nil {
|
||||
t.Fatalf("开启测试事务失败:%v", tx.Error)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = tx.Rollback().Error
|
||||
if sqlDB, dbErr := db.DB(); dbErr == nil {
|
||||
_ = sqlDB.Close()
|
||||
}
|
||||
})
|
||||
return tx
|
||||
return db
|
||||
}
|
||||
|
||||
// NewRedisClient 创建真实 Redis 测试客户端并在测试结束时关闭。
|
||||
|
||||
Reference in New Issue
Block a user