feat(in-transit-transfer): 收料成功同步调拨证据并落库调用日志留档
- 新增 evidence_sync:收料归档 success 终态桥接触发调拨证据同步,桥内异常只记日志不回滚归档终态 - 迁移 wd7e:integration_calls 新增 wire_json 线级事实留档与 business_ref 业务锚点列,完成调用日志闭环 - receipt_report_storage 在 success 分支挂接同步桥,收料链路正确性不受桥内异常影响 - 同步 demand/funclist/technical 跨域文档与证据同步实施计划 - test(sso):旧 Redis 协议下线断言由 tickets 改用 orders 端点措辞
This commit is contained in:
1 parent
9f2b023a10
commit
5412ddc7a8
9 files changed
+2216
-27
No files matched your search
@@ -0,0 +1,235 @@
|
||||
"""证据同步三分类写入回归。
|
||||
|
||||
用途:锁死 evidence_sync 的核心语义——新行插入(待查态)、既有行按文件刷新、
|
||||
值变重置库存侧、消失行失效、终态行绝不触碰、全程零 ERP 调用。
|
||||
依赖:repo_fakes(Fake 文件仓库注入文件行)、app fixture。
|
||||
逻辑:通过 Fake repository 的 latest_ready_file 提供文件;直接断言行级状态。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.models import TransferInvoiceItem
|
||||
from app.services.in_transit_transfer.evidence_sync import (
|
||||
SYNC_RESULT_PENDING,
|
||||
align_scope_rows,
|
||||
sync_source_rows,
|
||||
)
|
||||
from tests.services.in_transit_transfer.repo_fakes import make_transfer_deps
|
||||
|
||||
pytestmark = [pytest.mark.usefixtures("app")]
|
||||
|
||||
NOW = datetime(2026, 9, 25, 10, 0, 0)
|
||||
|
||||
|
||||
def _pending_count(source_code="DBS-AWSBI"):
|
||||
"""统计某来源处于待查态(pending_stock_check)的行数。"""
|
||||
return TransferInvoiceItem.query.filter_by(
|
||||
source_code=source_code, result_code=SYNC_RESULT_PENDING).count()
|
||||
|
||||
|
||||
def test_sync_inserts_pending_rows_without_erp(app, dbs_ready_file):
|
||||
# [断言意图] 首次同步:文件行全部落库为待查态;ERP 能力零调用。
|
||||
deps, erp, _ = make_transfer_deps(dbs_ready_file)
|
||||
|
||||
outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
|
||||
assert outcome is not None and outcome.inserted > 0 # [断言意图] 有新行
|
||||
assert outcome.removed == 0
|
||||
assert _pending_count() == outcome.inserted # [断言意图] 插入行全部待查态
|
||||
row = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").first()
|
||||
assert row.status == "matching" # [断言意图] 13 状态内的待查表达
|
||||
assert row.result_code == SYNC_RESULT_PENDING
|
||||
assert row.matching_id is None # [断言意图] 落地行尚无匹配归属
|
||||
assert erp.query_calls == 0 # [断言意图] 零 ERP 调用
|
||||
|
||||
|
||||
def test_sync_is_idempotent_when_sha_unchanged(app, dbs_ready_file):
|
||||
# [断言意图] 同文件重复同步:整行跳过,不续新鲜度、不产生新行。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
before = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").all()
|
||||
stamps = [r.updated_at for r in before]
|
||||
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
|
||||
after = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").all()
|
||||
assert [r.id for r in after] == [r.id for r in before] # [断言意图] 不产生新行
|
||||
assert [r.updated_at for r in after] == stamps # [断言意图] sha 一致零写入
|
||||
|
||||
|
||||
def test_sync_marks_vanished_rows_and_never_touches_terminal(app, dbs_ready_file):
|
||||
# [断言意图] 新版文件里消失的行 → blocked_file_removed;终态行保持既定事实。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
doomed = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").first()
|
||||
doomed.status = "ready_to_execute" # 模拟上一版可执行
|
||||
db.session.flush()
|
||||
terminal = TransferInvoiceItem(
|
||||
matching_id=None, source_code="DBS-AWSBI", invoice_no="HOX-TERMINAL",
|
||||
material_no="MAT-X", material_name="n", specification="", receipt_date="2026-09-19",
|
||||
status="success", result_code=None, from_warehouse="W", to_warehouse="H",
|
||||
receipt_quantity="1", lot_no="L", receipt_warehouse_code="W1", unit="PCS",
|
||||
raw_lines_json=[], updated_at=NOW,
|
||||
)
|
||||
db.session.add(terminal)
|
||||
db.session.flush()
|
||||
|
||||
deps2, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
# 换一版不含任何行的文件:经 Fake 仓库一次性覆盖注入(executor 按 repo_fakes
|
||||
# 实际结构用 set_next_file 提供空 rows 的 ReadyReceiptFile,checksum 用不同 sha)。
|
||||
repo2 = deps2.file_repository
|
||||
repo2.set_next_file(source_code="DBS-AWSBI", rows=(), checksum="e" * 64)
|
||||
outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps2,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
|
||||
assert outcome is not None and outcome.removed == 2 # [断言意图] 两个非终态行全部出清
|
||||
db.session.refresh(doomed)
|
||||
assert doomed.status == "blocked_recoverable" # [断言意图] 消失行失效
|
||||
assert doomed.result_code == "blocked_file_removed"
|
||||
db.session.refresh(terminal)
|
||||
assert terminal.status == "success" # [断言意图] 终态行不复活
|
||||
|
||||
|
||||
def test_sync_does_not_resurrect_terminal_rows_on_relanding(app, dbs_ready_file):
|
||||
# [断言意图] 来源全终态后同文件重落地(同日 RPA 重试 / align 兜底重解析):
|
||||
# 不重插新 pending 行、终态行原样保留——已执行调拨单不得复活回待办池。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
rows = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").all()
|
||||
assert len(rows) == 2 # [断言意图] 前置:文件聚合成两行
|
||||
for r in rows:
|
||||
r.status = "success" # 全部推到终态(模拟已执行)
|
||||
r.result_code = None # 执行终态清待查标记
|
||||
db.session.flush()
|
||||
frozen = {r.id: (r.status, r.result_code, r.updated_at) for r in rows}
|
||||
|
||||
# 同一文件重落地:set_next_file 重注同 rows / 同 checksum(一次有效覆盖)。
|
||||
deps.file_repository.set_next_file(
|
||||
source_code="DBS-AWSBI", rows=tuple(dbs_ready_file["rows"]), checksum="d" * 64)
|
||||
later = datetime(2026, 9, 25, 11, 0, 0) # 后续新鲜度基准:任何写入都会暴露
|
||||
outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps,
|
||||
business_date="2026-08-06", now=later)
|
||||
|
||||
assert outcome is not None
|
||||
assert outcome.inserted == 0 # [断言意图] 终态 quad 不重插新行
|
||||
assert outcome.skipped == 2 # [断言意图] 全部计入跳过口径
|
||||
assert _pending_count() == 0 # [断言意图] 已执行单据不重回待办池
|
||||
after = TransferInvoiceItem.query.filter_by(source_code="DBS-AWSBI").all()
|
||||
assert [r.id for r in after] == [r.id for r in rows] # [断言意图] 行集合不变
|
||||
assert {r.id: (r.status, r.result_code, r.updated_at) for r in after} == frozen
|
||||
# [断言意图] 终态行零写入(含 updated_at 未被续新)
|
||||
|
||||
|
||||
def test_sync_refreshes_display_fields_without_reset(app, dbs_ready_file):
|
||||
# [断言意图] 纯名称/规格变更(sha 变、三证据字段未变)→ refreshed 且展示值已更新。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
|
||||
# 同证据(数量/批号/仓库/单号/料号/日期不变),只改 material_name/specification。
|
||||
renamed_rows = [
|
||||
{**row, "material_name": "改名组件", "specification": "改规格 / NEW"}
|
||||
for row in dbs_ready_file["rows"]
|
||||
]
|
||||
deps.file_repository.set_next_file(source_code="DBS-AWSBI", rows=renamed_rows, checksum="f" * 64)
|
||||
outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
|
||||
assert outcome.refreshed == 2 and outcome.reset_pending == 0 # [断言意图] 展示变更不重置待查
|
||||
row = TransferInvoiceItem.query.filter_by(
|
||||
source_code="DBS-AWSBI", invoice_no="HOM-001").first()
|
||||
assert row.material_name == "改名组件" # [断言意图] 展示值随新文件刷新
|
||||
assert row.specification == "改规格 / NEW" # [断言意图] 规格同步刷新
|
||||
assert row.status == "matching" # [断言意图] 状态不受展示变更影响
|
||||
assert row.result_code == SYNC_RESULT_PENDING
|
||||
assert row.raw_lines_json[0]["file_sha256"] == "f" * 64 # [断言意图] 版本戳已换新
|
||||
|
||||
|
||||
def test_sync_restores_reappeared_removed_rows(app, dbs_ready_file):
|
||||
# [断言意图] file_removed 行重新出现且证据未变 → 无条件恢复待查态(不卡死隐藏)。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
deps.file_repository.set_next_file(source_code="DBS-AWSBI", rows=(), checksum="e" * 64)
|
||||
removed_outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
assert removed_outcome.removed == 2 # [断言意图] 前置:两行已标记消失
|
||||
|
||||
deps.file_repository.set_next_file(
|
||||
source_code="DBS-AWSBI", rows=tuple(dbs_ready_file["rows"]), checksum="a" * 64)
|
||||
outcome = sync_source_rows(source_code="DBS-AWSBI", deps=deps,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
|
||||
assert outcome.reset_pending == 2 # [断言意图] 复现行全部恢复待查
|
||||
row = TransferInvoiceItem.query.filter_by(
|
||||
source_code="DBS-AWSBI", invoice_no="HOX-002").first()
|
||||
assert row.status == "matching" # [断言意图] 脱离 blocked_recoverable
|
||||
assert row.result_code == SYNC_RESULT_PENDING
|
||||
assert row.result_message is None # [断言意图] 消失解释已清除
|
||||
assert row.external_quantity is None # [断言意图] 旧库存作废待重查
|
||||
assert row.raw_lines_json[0]["file_sha256"] == "a" * 64 # [断言意图] 复现行已换最新戳
|
||||
|
||||
|
||||
def test_vanished_rows_restamped_so_align_passes_through(app, dbs_ready_file, monkeypatch):
|
||||
# [断言意图] 消失行已换最新文件戳 → align 判据 row_shas == latest_sha 直通(None)。
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
deps.file_repository.set_next_file(source_code="DBS-AWSBI", rows=(), checksum="e" * 64)
|
||||
sync_source_rows(source_code="DBS-AWSBI", deps=deps, business_date="2026-08-06", now=NOW)
|
||||
|
||||
row = TransferInvoiceItem.query.filter_by(
|
||||
source_code="DBS-AWSBI", invoice_no="HOX-002").first()
|
||||
assert row.result_code == "blocked_file_removed"
|
||||
assert row.raw_lines_json[0]["file_sha256"] == "e" * 64 # [断言意图] 消失行已换新戳
|
||||
|
||||
# 文件状态投影返回空 files 版 sha(align 判据输入);测试注入替身避免触碰真实表。
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.evidence_sync.resolve_source_file_status",
|
||||
lambda *, source_code: {"file_status": "ready", "has_ready_file": True,
|
||||
"file_no": "REC-E", "file_sha256": "e" * 64},
|
||||
)
|
||||
outcomes = align_scope_rows(source_codes=["DBS-AWSBI"], deps=deps,
|
||||
business_date="2026-08-06", now=NOW)
|
||||
assert outcomes["DBS-AWSBI"] is None # [断言意图] 已对齐:直通零解析
|
||||
|
||||
|
||||
def test_receipt_bridge_is_failure_isolated(app, monkeypatch, caplog):
|
||||
# [断言意图] 同步崩溃只记日志:异常绝不向收料回调链路传播。
|
||||
from app.services.in_transit_transfer import evidence_sync
|
||||
|
||||
def _boom(**kwargs):
|
||||
raise RuntimeError("sync crashed")
|
||||
|
||||
monkeypatch.setattr(evidence_sync, "sync_source_rows", _boom)
|
||||
with caplog.at_level(logging.WARNING):
|
||||
result = evidence_sync.sync_and_commit_for_callback(
|
||||
source_code="CRANE-SZ", business_date="2026-09-25", now=NOW)
|
||||
|
||||
assert result is False # [断言意图] 失败被吞、返回 False
|
||||
assert any("TRANSFER_EVIDENCE_SYNC" in r.message for r in caplog.records)
|
||||
|
||||
|
||||
def test_receipt_bridge_commits_on_success(app, dbs_ready_file, monkeypatch):
|
||||
# [断言意图] 成功路径自行 commit(B3 教训:不依赖调用方提交时机)。
|
||||
from app.services.in_transit_transfer import evidence_sync
|
||||
deps, _, _ = make_transfer_deps(dbs_ready_file)
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.deps.get_transfer_dependencies", lambda: deps)
|
||||
|
||||
ok = evidence_sync.sync_and_commit_for_callback(
|
||||
source_code="DBS-AWSBI", business_date="2026-08-06", now=NOW)
|
||||
|
||||
assert ok is True
|
||||
assert _pending_count("DBS-AWSBI") > 0 # [断言意图] 已持久化(含 commit)
|
||||
|
||||
|
||||
def test_receipt_bridge_skips_when_no_file(app):
|
||||
# [断言意图] 来源无可用文件:静默返回 True(无事发生,非错误)。
|
||||
from app.services.in_transit_transfer import evidence_sync
|
||||
assert evidence_sync.sync_and_commit_for_callback(
|
||||
source_code="NO-SUCH", business_date="2026-09-25", now=NOW) is True
|
||||
Reference in new issue
Block a user