From c556bda82bcd0c6d8aa961da4193a6952a734799 Mon Sep 17 00:00:00 2001 From: iven Date: Tue, 28 Apr 2026 11:17:46 +0800 Subject: [PATCH] =?UTF-8?q?test(core):=20=E6=B7=BB=E5=8A=A0=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E5=9B=9E=E6=BB=9A=E6=B5=8B=E8=AF=95=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E8=AE=BE=E6=96=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/erp-core/src/lib.rs | 3 +++ crates/erp-core/src/test_helpers.rs | 41 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 crates/erp-core/src/test_helpers.rs diff --git a/crates/erp-core/src/lib.rs b/crates/erp-core/src/lib.rs index afbe95c..b857801 100644 --- a/crates/erp-core/src/lib.rs +++ b/crates/erp-core/src/lib.rs @@ -11,5 +11,8 @@ pub mod request_info; pub mod sanitize; pub mod types; +#[cfg(test)] +pub mod test_helpers; + // 便捷导出 pub use module::{ModuleContext, ModuleType, PermissionDescriptor}; diff --git a/crates/erp-core/src/test_helpers.rs b/crates/erp-core/src/test_helpers.rs new file mode 100644 index 0000000..69c6d82 --- /dev/null +++ b/crates/erp-core/src/test_helpers.rs @@ -0,0 +1,41 @@ +//! 测试基础设施 — 事务回滚模式解决并行化问题 +//! +//! 每个测试在独立事务中执行,测试结束自动回滚,无数据残留。 +//! 多个测试共享同一个数据库连接池,无连接竞争。 + +use sea_orm::{ConnectOptions, DatabaseConnection, DatabaseTransaction}; +use std::sync::OnceLock; +use tokio::sync::OnceCell; + +static DB_POOL: OnceCell = OnceCell::const_new(); +static DB_URL: OnceLock = OnceLock::new(); + +fn db_url() -> String { + DB_URL + .get_or_init(|| { + std::env::var("TEST_DATABASE_URL") + .unwrap_or_else(|_| "postgres://erp:erp@localhost:5432/erp_test".into()) + }) + .clone() +} + +async fn db_pool() -> &'static DatabaseConnection { + DB_POOL + .get_or_init(|| async { + let opt = ConnectOptions::new(db_url()) + .max_connections(5) + .to_owned(); + DatabaseConnection::connect(opt) + .await + .expect("测试数据库连接失败") + }) + .await +} + +/// 创建测试用事务。测试结束自动回滚,无数据残留。 +pub async fn test_txn() -> DatabaseTransaction { + let pool = db_pool().await; + pool.begin() + .await + .expect("测试事务创建失败") +}