外部单号进入异动档自然键,圈档按 T-2 成熟封顶,当前归档可单独重解析。同批带上 Crane 禁整页刷新、ODM 白名单降级、排程抽屉与执行记录日期窗,以及 YAML 库存源下线。 Co-authored-by: Cursor <cursoragent@cursor.com>
111 lines
4.5 KiB
Python
111 lines
4.5 KiB
Python
"""在途调拨测试固定夹具:时钟、入站文件样例、外部替身与数据库隔离。
|
||
|
||
用途:为 tests/services/in_transit_transfer 提供与 TDD 文档一致的固定输入
|
||
(fixed_clock 默认 2026-08-06 09:00:00+08:00、dbs/crane ready 文件、
|
||
same_day_inbound_file),并组装 FakeCapabilityClient/FakeRPA/FakeWeCom。
|
||
依赖:根 tests/conftest 的 app fixture(SQLite 内存库)、fakes 模块。
|
||
逻辑:纯单元用例(marker pure_unit 且未请求 app)不创建 Flask/DB;
|
||
请求 transfer_db 的用例复用 app fixture 的 create_all/drop_all 隔离。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import pytest
|
||
|
||
from tests.services.in_transit_transfer.fakes import FakeClock, FakeCapabilityClient, FakeRPA, FakeWeCom
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def _stub_doc_sequence(monkeypatch):
|
||
"""单据级执行单号序列默认替身(autouse)。
|
||
|
||
用途:execution_service 生成 doc_no 默认读 PG 全局序列(transfer_doc_no_seq),
|
||
SQLite 测试库无序列后端;本替身按用例递增取值,需要精确序号的用例可
|
||
自行覆盖 execution_service._sequence_loader。
|
||
"""
|
||
counter = {"n": 0}
|
||
|
||
def _next() -> int:
|
||
counter["n"] += 1
|
||
return counter["n"]
|
||
|
||
monkeypatch.setattr(
|
||
"app.services.in_transit_transfer.execution_service._sequence_loader", _next,
|
||
)
|
||
|
||
|
||
@pytest.fixture(autouse=True)
|
||
def _seed_default_transfer_type(request):
|
||
"""默认启用类型配置种子(autouse,出生翻转后匹配创建必填且配置驱动)。
|
||
|
||
用途:Task 11 切换后 run_matching/create_matching 必填 transfer_type 且类型
|
||
合法值=配置表 code;本 fixture 为请求 app 的用例预置一条与旧「类型 1
|
||
硬编码」语义对齐的启用配置(doc_no 前缀 HOM/HOX/WTP + WTP→HOW 转换 +
|
||
from/to 全 mapped),存量用例随语义平移后行为等价。专项用例可按 code
|
||
覆盖/停用/补建其它类型。
|
||
"""
|
||
if "app" in getattr(request, "fixturenames", []):
|
||
request.getfixturevalue("app")
|
||
from tests.services.in_transit_transfer.factories import seed_transfer_type_config
|
||
|
||
seed_transfer_type_config()
|
||
|
||
|
||
@pytest.fixture
|
||
def fixed_clock() -> FakeClock:
|
||
"""固定时钟:默认 2026-08-06 09:00:00+08:00(业务日 T=2026-08-06)。"""
|
||
return FakeClock()
|
||
|
||
|
||
@pytest.fixture
|
||
def erp_fake() -> FakeCapabilityClient:
|
||
"""FakeCapabilityClient:账面库存与写响应全部可注入,调用记录可断言。"""
|
||
return FakeCapabilityClient()
|
||
|
||
|
||
@pytest.fixture
|
||
def rpa_fake() -> FakeRPA:
|
||
"""FakeRPA:按来源实例独立注入失败/延迟。"""
|
||
return FakeRPA()
|
||
|
||
|
||
@pytest.fixture
|
||
def wecom_fake() -> FakeWeCom:
|
||
"""FakeWeCom:记录投递事实,可注入附件失败。"""
|
||
return FakeWeCom()
|
||
|
||
|
||
@pytest.fixture
|
||
def patch_source_file_status(monkeypatch):
|
||
"""执行文件对账门禁替身工厂:替换 execution_service 的来源文件状态投影。
|
||
|
||
用途:execute_items 门禁②按 (来源, record_kind) 调 resolve_source_file_status
|
||
(真实查询收料/出站文件表);单元测试注入替身避免触碰表数据。缺省全部
|
||
来源投影为 ready 且 sha=dbs 夹具 checksum("d"*64,与 dbs_ready_file
|
||
行版本戳同源)。record_kind 入参接受但不区分,存量用例仍按来源给 sha。
|
||
|
||
Args:
|
||
mapping: source_code → sha 映射;值为 None 表示该来源无可用文件
|
||
(file_status=none,触发 FILE_CHANGED);未列出的来源回落 ready/"d"*64。
|
||
"""
|
||
READY_SHA = "d" * 64
|
||
|
||
def _patch(mapping: dict[str, str | None] | None = None) -> None:
|
||
table = mapping or {}
|
||
|
||
def _resolve(*, source_code: str, record_kind: str = "inbound") -> dict:
|
||
del record_kind # 本替身按来源给 sha;出站分流用例自行覆盖本函数。
|
||
if source_code in table:
|
||
sha = table[source_code]
|
||
if sha is None:
|
||
return {"file_status": "none", "has_ready_file": False, "file_no": None, "file_sha256": None}
|
||
return {"file_status": "ready", "has_ready_file": True, "file_no": "REC-TEST", "file_sha256": sha}
|
||
return {"file_status": "ready", "has_ready_file": True, "file_no": "REC-TEST", "file_sha256": READY_SHA}
|
||
|
||
monkeypatch.setattr(
|
||
"app.services.in_transit_transfer.execution_service.resolve_source_file_status",
|
||
_resolve,
|
||
)
|
||
|
||
return _patch
|