"""pytest 共享夹具:确保数据与图谱存在(数据已随工程提交,缺失时现场生成)。""" from __future__ import annotations import subprocess import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) PY = sys.executable @pytest.fixture(scope="session", autouse=True) def ensure_data() -> None: if not (ROOT / "data" / "jd_raw").exists(): subprocess.run([PY, str(ROOT / "scripts" / "generate_dataset.py")], check=True) @pytest.fixture(scope="session") def graph_path() -> Path: path = ROOT / "data" / "graph.json" if not path.exists(): subprocess.run([PY, str(ROOT / "scripts" / "run_demo.py")], check=True) return path @pytest.fixture(scope="session", autouse=True) def isolated_state_db(tmp_path_factory): """整个测试会话把 SQLite 状态库重定向到临时目录,避免污染真实 data/careernexus.db。 audit_path/pending_path 仅作为旧文件迁移种子,指向不存在的临时路径即跳过迁移。 """ from app.api import routes tmp = tmp_path_factory.mktemp("state_db") keys = ("state_db_path", "audit_path", "pending_path") originals = {k: routes._cfg.get(k) for k in keys} routes._cfg["state_db_path"] = str(tmp / "state.db") routes._cfg["audit_path"] = str(tmp / "audit.jsonl") routes._cfg["pending_path"] = str(tmp / "pending.json") # 测试不加载本机附加数据源:真实语料会让 API 触发的重建超出任务等待窗口 extra_orig = routes._cfg.get("jd_extra_dirs") routes._cfg["jd_extra_dirs"] = "" yield for k, v in originals.items(): if v is None: routes._cfg.pop(k, None) else: routes._cfg[k] = v if extra_orig is None: routes._cfg.pop("jd_extra_dirs", None) else: routes._cfg["jd_extra_dirs"] = extra_orig