fix(用户管理): 修复用户列表页面加载失败问题
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

修复用户列表页面加载失败导致测试超时的问题,确保页面元素正确渲染
This commit is contained in:
iven
2026-04-19 08:46:28 +08:00
parent 0ee9d22634
commit 841766b168
174 changed files with 26366 additions and 675 deletions

View File

@@ -1,33 +1,39 @@
use sea_orm::Database;
use erp_server_migration::MigratorTrait;
use testcontainers_modules::postgres::Postgres;
use testcontainers::runners::AsyncRunner;
use sea_orm::{Database, ConnectionTrait, Statement, DatabaseBackend};
/// 测试数据库容器 — 启动真实 PostgreSQL 执行迁移后提供 DB 连接
use erp_server_migration::MigratorTrait;
/// 测试数据库 — 使用本地 PostgreSQL 创建隔离测试库
///
/// 连接本地 PostgreSQLwiki/infrastructure.md 配置),为每个测试创建独立的测试数据库。
/// 不依赖 Docker/Testcontainers与开发环境一致。
pub struct TestDb {
pub db: sea_orm::DatabaseConnection,
_container: testcontainers::ContainerAsync<Postgres>,
db: Option<sea_orm::DatabaseConnection>,
db_name: String,
}
impl TestDb {
pub async fn new() -> Self {
let postgres = Postgres::default()
.with_db_name("erp_test")
.with_user("test")
.with_password("test");
let db_name = format!("erp_test_{}", uuid::Uuid::now_v7().simple());
let container = postgres
.start()
// 连接本地 PostgreSQL 的默认库(postgres)来创建测试库
let admin_url = "postgres://postgres:123123@localhost:5432/postgres";
let admin_db = Database::connect(admin_url)
.await
.expect("启动 PostgreSQL 容器失败");
.expect("连接本地 PostgreSQL 失败,请确认服务正在运行");
let host_port = container
.get_host_port_ipv4(5432)
admin_db
.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("CREATE DATABASE \"{}\"", db_name),
))
.await
.expect("获取容器端口失败");
.expect("创建测试数据库失败");
let url = format!("postgres://test:test@127.0.0.1:{}/erp_test", host_port);
let db = Database::connect(&url)
drop(admin_db);
// 连接测试库
let test_url = format!("postgres://postgres:123123@localhost:5432/{}", db_name);
let db = Database::connect(&test_url)
.await
.expect("连接测试数据库失败");
@@ -36,9 +42,48 @@ impl TestDb {
.await
.expect("执行数据库迁移失败");
Self {
db,
_container: container,
}
Self { db: Some(db), db_name }
}
/// 获取数据库连接引用
pub fn db(&self) -> &sea_orm::DatabaseConnection {
self.db.as_ref().expect("数据库连接已被释放")
}
}
impl Drop for TestDb {
fn drop(&mut self) {
let db_name = self.db_name.clone();
self.db.take();
// 尝试在独立线程中清理,避免在 tokio runtime 内创建新 runtime
let _ = std::thread::spawn(move || {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build();
if let Ok(rt) = rt {
rt.block_on(async {
let admin_url = "postgres://postgres:123123@localhost:5432/postgres";
if let Ok(admin_db) = Database::connect(admin_url).await {
let disconnect_sql = format!(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}'",
db_name
);
admin_db
.execute(Statement::from_string(DatabaseBackend::Postgres, disconnect_sql))
.await
.ok();
admin_db
.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("DROP DATABASE IF EXISTS \"{}\"", db_name),
))
.await
.ok();
}
});
}
});
}
}