Files
syops/backend/tests/services/in_transit_transfer/conftest.py
T
hugo 52c71ee46a feat(in-transit-transfer): 调拨类型配置化与库存异动档原子切换
调拨类型从四处硬编码升级为管理页可配置:范围规则(受控字段白名单,规则间且/值间或)、
仓库定义(映射档/固定仓双模式角色解析)、字段转换(批号前缀替换,匹配时快照固化到 scope_json)。

新建 transfer_movement_ledger 库存异动档(参照 ERP inaj_t):自然键幂等增量落档
(插入/换证更新/跳过/缺席不删/执行封存永不触碰),圈档互斥保证一条档记录一生只绑一次明细;
收料证据落地改写档、停写 items。transfer_invoice_items 出生翻转为匹配时从档圈定创建
(一次匹配永久写入 matching_id+ledger_record_id),回查对账/取消释放/执行门禁/待匹配桶全链路适配。

迁移 tt10→tt13 单 head:类型配置表、异动档、items 档外键、transfer_runs.transfer_type 放宽 String(64)。

随行披露:设计文档与实施计划、ADR 0001/0002、in-transit-transfer 术语表、
demand/funclist/technical 文档更新同批入库。
2026-10-15 13:02:17 +08:00

125 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""在途调拨测试固定夹具:时钟、收料文件样例、外部替身与数据库隔离。
用途:为 tests/services/in_transit_transfer 提供与 TDD 文档一致的固定输入
(fixed_clock 默认 2026-08-06 09:00:00+08:00、dbs/crane ready 文件、
same_day_receipt_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 _pin_maturity_lag_legacy(request):
"""在途套件钉成熟滞后=1(T+1)。
用途:存量夹具生态按 T+1 语义构建(fixed_clock 业务日 2026-08-06、收料日期
2026-08-05=T-1);生产默认 T+2(settings IN_TRANSIT_TRANSFER_MATURITY_
LAG_DAYS=2)的边界行为由 test_matching_service / test_date_policy 专项
用例显式覆盖,不被本钉值掩盖。
逻辑:仅对已请求 app 的用例钉值(request.fixturenames 判定),pure_unit 用例
(如 test_date_policy)不触碰 Flask,继续走 date_policy 的无上下文回退。
"""
if "app" in getattr(request, "fixturenames", []):
app = request.getfixturevalue("app")
app.config["IN_TRANSIT_TRANSFER_MATURITY_LAG_DAYS"] = 1
@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 门禁②按去重来源调 resolve_source_file_status(真实查询
收料文件表);单元测试注入替身避免触碰表数据。缺省全部来源投影为
ready 且 sha=dbs 夹具 checksum("d"*64,与 dbs_ready_file 行版本戳同源)。
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) -> dict:
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