feat(in-transit-transfer): inbound/outbound 幂等重命名与解析用途分层
- receipt→inbound 三件套改名(回调桥/文件门禁/触发服务),回调链幂等去重,迁移 tt14 同步台账键与测试基建 - 库存解析用途分层 onhand/inbound/outbound(迁移 tt15):解析档案、文件存储与下载名按用途隔离,管理端表单同步 - 「成熟」概念退役(迁移 tt16):数据无成熟属性,日期边界由调用方入参严格判定 - backlog 新增重解析服务,在途重识别投影 processing,EML 父项不再停留处理中 - 收料 RPA 回调/归档/排程链路同步改名挂点,运维脚本复位假完成状态 - 文档随批:ADR 0003 模块边界决策、10-15/10-16 计划与设计、调拨文档族与 demand/tec/funclist - 同步 chat/errors 文案四语言、调拨前端卡片与 e2e、测试全家
This commit is contained in:
1 parent
43dceaf027
commit
8102a1fe44
151 files changed
+8057
-2382
No files matched your search
@@ -1,14 +1,17 @@
|
||||
"""在途调拨匹配 API 契约测试(L2,Flask test client)。
|
||||
|
||||
用途:验证 POST /matchings 创建(含 envelope/权限/参数校验)与
|
||||
GET /matchings/{id} 进度恢复;无权限 403、未认证 401。
|
||||
依赖:app fixture、client、issue_local_access_token、fakes/repo_fakes。
|
||||
GET /matchings/{id} 进度恢复;无权限 403、未认证 401;
|
||||
待匹配桶「清空并重新解析」预检/执行端点(spec §6,Task 7)。
|
||||
依赖:app fixture、client、issue_local_access_token、fakes/repo_fakes、factories。
|
||||
逻辑:API 依赖经 deps._override 注入 Fake 替身;权限按 admin 放行或
|
||||
in_transit_transfer Agent 授权校验。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
@@ -422,18 +425,18 @@ def test_exceptions_excel_download(app, transfer_db, client, admin_headers, tran
|
||||
assert audit.run_no == run_no
|
||||
|
||||
|
||||
def test_receipt_files_download_records_access_audit(app, transfer_db, client, admin_headers, transfer_deps):
|
||||
"""下载收料来源文件 → 200(zip),记录访问审计。"""
|
||||
def test_inbound_files_download_records_access_audit(app, transfer_db, client, admin_headers, transfer_deps):
|
||||
"""下载出入站来源文件 → 200(zip),记录访问审计。"""
|
||||
_create_matching(client, admin_headers)
|
||||
executed = _execute_ready_items(app, client, admin_headers)
|
||||
run_no = executed.get_json()["data"]["run_no"]
|
||||
response = client.get(f"{API_PREFIX}/runs/{run_no}/receipt-files", headers=admin_headers)
|
||||
response = client.get(f"{API_PREFIX}/runs/{run_no}/inbound-files", headers=admin_headers)
|
||||
assert response.status_code == 200
|
||||
assert response.content_type.startswith("application/zip")
|
||||
with app.app_context():
|
||||
from app.models import TransferAuditEvent
|
||||
|
||||
audit = TransferAuditEvent.query.filter_by(event_type="TRANSFER_RECEIPT_FILES_DOWNLOADED").first()
|
||||
audit = TransferAuditEvent.query.filter_by(event_type="TRANSFER_INBOUND_FILES_DOWNLOADED").first()
|
||||
assert audit is not None
|
||||
|
||||
|
||||
@@ -458,7 +461,7 @@ def test_downloads_forbidden_without_permission(app, transfer_db, client, admin_
|
||||
_create_matching(client, admin_headers)
|
||||
executed = _execute_ready_items(app, client, admin_headers)
|
||||
run_no = executed.get_json()["data"]["run_no"]
|
||||
for path in (f"{API_PREFIX}/runs/{run_no}/receipt-files",
|
||||
for path in (f"{API_PREFIX}/runs/{run_no}/inbound-files",
|
||||
f"{API_PREFIX}/runs/{run_no}/external-inventory",
|
||||
f"{API_PREFIX}/runs/{run_no}/exceptions.xlsx"):
|
||||
response = client.get(path, headers=unauthorized_headers)
|
||||
@@ -512,6 +515,118 @@ def test_backlog_forbidden_without_permission(app, transfer_db, client, unauthor
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_backlog_pagination_envelope_and_compat(app, transfer_db, client, admin_headers, transfer_deps):
|
||||
"""GET /backlog 分页 envelope 与无参兼容:带 page/page_size 回
|
||||
{items,total,page,page_size},无参保持历史全量数组(向后兼容口径)。"""
|
||||
_create_matching(client, admin_headers)
|
||||
with app.app_context():
|
||||
from app.models import TransferInvoiceItem
|
||||
|
||||
item = TransferInvoiceItem.query.filter_by(invoice_no="HOM-001").first()
|
||||
item.status = "blocked_recoverable"
|
||||
item.result_code = "blocked_quantity_mismatch"
|
||||
db.session.commit()
|
||||
|
||||
# 无参 → 历史数组形状(向后兼容;现有前端/测试依赖不破)。
|
||||
legacy = client.get(f"{API_PREFIX}/backlog", headers=admin_headers)
|
||||
assert legacy.status_code == 200
|
||||
assert isinstance(legacy.get_json()["data"], list)
|
||||
|
||||
# 带分页参数 → envelope;单行池:首页 1 行、total=1。
|
||||
paged = client.get(
|
||||
f"{API_PREFIX}/backlog?page=1&page_size=1&result_filter=recoverable",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert paged.status_code == 200
|
||||
data = paged.get_json()["data"]
|
||||
assert set(data.keys()) == {"items", "total", "page", "page_size"}
|
||||
assert len(data["items"]) == 1 and data["items"][0]["invoice_no"] == "HOM-001"
|
||||
assert data["total"] == 1 and data["page"] == 1 and data["page_size"] == 1
|
||||
# 越界页:offset 超出 total → 空切片(分页推进语义)。
|
||||
page2 = client.get(
|
||||
f"{API_PREFIX}/backlog?page=2&page_size=1&result_filter=recoverable",
|
||||
headers=admin_headers,
|
||||
).get_json()["data"]
|
||||
assert page2["items"] == [] and page2["total"] == 1
|
||||
|
||||
|
||||
def test_backlog_result_filter_and_keyword_pushdown(app, transfer_db, client, admin_headers, transfer_deps):
|
||||
"""GET /backlog 筛选下推:result_filter 白名单切片、keyword 跨四字段子串、
|
||||
白名单外 400(fail-closed)。"""
|
||||
_create_matching(client, admin_headers)
|
||||
with app.app_context():
|
||||
from app.models import TransferInvoiceItem
|
||||
|
||||
first = TransferInvoiceItem.query.filter_by(invoice_no="HOM-001").first()
|
||||
first.status = "blocked_recoverable"
|
||||
first.result_code = "blocked_quantity_mismatch"
|
||||
# 第二种池内状态:把可执行行翻成人工关闭(进池、落 closed 切片)。
|
||||
second = TransferInvoiceItem.query.filter_by(invoice_no="HOX-002").first()
|
||||
second.status = "manually_closed"
|
||||
second.result_message = "线下处理"
|
||||
db.session.commit()
|
||||
|
||||
recoverable = client.get(
|
||||
f"{API_PREFIX}/backlog?page=1&page_size=50&result_filter=recoverable",
|
||||
headers=admin_headers,
|
||||
).get_json()["data"]
|
||||
assert {entry["invoice_no"] for entry in recoverable["items"]} == {"HOM-001"}
|
||||
assert recoverable["total"] == 1
|
||||
|
||||
closed = client.get(
|
||||
f"{API_PREFIX}/backlog?page=1&page_size=50&result_filter=closed",
|
||||
headers=admin_headers,
|
||||
).get_json()["data"]
|
||||
assert {entry["invoice_no"] for entry in closed["items"]} == {"HOX-002"}
|
||||
|
||||
keyword = client.get(
|
||||
f"{API_PREFIX}/backlog?page=1&page_size=50&keyword=HOX-002",
|
||||
headers=admin_headers,
|
||||
).get_json()["data"]
|
||||
assert {entry["invoice_no"] for entry in keyword["items"]} == {"HOX-002"}
|
||||
|
||||
invalid = client.get(
|
||||
f"{API_PREFIX}/backlog?page=1&result_filter=bogus",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert invalid.status_code == 400
|
||||
|
||||
|
||||
def test_pending_match_pagination_envelope_and_compat(app, transfer_db, client, admin_headers, monkeypatch):
|
||||
"""GET /backlog/pending-match 分页 envelope 与无参兼容:带参回
|
||||
{records,total,page,page_size},无参保持历史数组(圈档伪造 25 行验证切片)。"""
|
||||
from types import SimpleNamespace
|
||||
|
||||
rows = [
|
||||
SimpleNamespace(
|
||||
ledger_record_id=None, id=i + 1, doc_no=f"HOM-{900 + i}",
|
||||
material_code=f"MAT-{i}", material_name="未匹配物料",
|
||||
movement_date="2026-08-05", source_code="DBS-AWSBI", quantity="1",
|
||||
)
|
||||
for i in range(25)
|
||||
]
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.backlog_service.circle_ledger_records_scope_union",
|
||||
lambda: rows,
|
||||
)
|
||||
|
||||
legacy = client.get(f"{API_PREFIX}/backlog/pending-match", headers=admin_headers)
|
||||
assert legacy.status_code == 200
|
||||
assert isinstance(legacy.get_json()["data"], list)
|
||||
assert len(legacy.get_json()["data"]) == 25
|
||||
|
||||
paged = client.get(
|
||||
f"{API_PREFIX}/backlog/pending-match?page=2&page_size=20",
|
||||
headers=admin_headers,
|
||||
)
|
||||
assert paged.status_code == 200
|
||||
data = paged.get_json()["data"]
|
||||
assert set(data.keys()) == {"records", "total", "page", "page_size"}
|
||||
assert len(data["records"]) == 5 # 25 行第 2 页余量
|
||||
assert data["total"] == 25 and data["page"] == 2 and data["page_size"] == 20
|
||||
assert data["records"][0]["doc_no"] == "HOM-920" # 第 21 行(稳定排序切片)
|
||||
|
||||
|
||||
def test_list_transfer_sources(app, client, admin_headers):
|
||||
"""来源实例列表:返回启用来源(平台=connector_type,来源实例为执行边界)。"""
|
||||
response = client.get(f"{API_PREFIX}/sources", headers=admin_headers)
|
||||
@@ -537,7 +652,7 @@ def test_list_transfer_sources_forbidden(app, client, unauthorized_headers):
|
||||
# 两条路径均不触发 RPA、不返回 202/fetching)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _seed_parse_failed_receipt_file(source_code: str):
|
||||
def _seed_parse_failed_inbound_file(source_code: str):
|
||||
"""造一条「当日可用但解析失败」的收料归档文件(success 单元 + parse_failed 三列)。
|
||||
|
||||
与 tests/services/in_transit_transfer/test_source_file_status.py 同款最小造数:
|
||||
@@ -579,13 +694,13 @@ def _seed_parse_failed_receipt_file(source_code: str):
|
||||
def test_retry_rpa_parse_failed_reparses_existing_file(app, client, admin_headers, monkeypatch):
|
||||
"""parse_failed 且有可用文件 → retry 先本地重解析(sync),不触发 RPA(200 非 202)。"""
|
||||
called = {"sync": 0, "rpa": 0}
|
||||
_seed_parse_failed_receipt_file("DSV360_AWSDU")
|
||||
_seed_parse_failed_inbound_file("DSV360_AWSDU")
|
||||
|
||||
def fake_sync(*, source_code, business_date, now):
|
||||
# 断言意图:重解析路径 sync 恰好 1 次;成功链路清除解析失败标记使投影回
|
||||
# ready(请求会话内重查后清除并 commit,保证端点再解析可见)。
|
||||
from app.models import ShippingReceiptReportFile
|
||||
from app.services.in_transit_transfer.source_file_status import clear_receipt_parse_failure
|
||||
from app.services.in_transit_transfer.source_file_status import clear_inbound_parse_failure
|
||||
|
||||
called["sync"] += 1
|
||||
file = (
|
||||
@@ -595,7 +710,7 @@ def test_retry_rpa_parse_failed_reparses_existing_file(app, client, admin_header
|
||||
.first()
|
||||
)
|
||||
if file is not None:
|
||||
clear_receipt_parse_failure(report=file)
|
||||
clear_inbound_parse_failure(report=file)
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
@@ -604,7 +719,7 @@ def test_retry_rpa_parse_failed_reparses_existing_file(app, client, admin_header
|
||||
fake_sync,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.receipt_trigger_service.trigger_receipt_rpa",
|
||||
"app.services.in_transit_transfer.inbound_trigger_service.trigger_inbound_rpa",
|
||||
lambda **kw: called.__setitem__("rpa", called["rpa"] + 1) or {"ok": True, "callback_id": "x"},
|
||||
)
|
||||
response = client.post(f"{API_PREFIX}/sources/DSV360_AWSDU/retry-rpa", headers=admin_headers)
|
||||
@@ -623,7 +738,7 @@ def test_retry_rpa_parse_failed_reparses_existing_file(app, client, admin_header
|
||||
def test_retry_rpa_parse_failed_still_failed_reports_not_ok(app, client, admin_headers, monkeypatch):
|
||||
"""sync 返回 True 但投影仍 parse_failed → ok=False(不触发 RPA、不回 202)。"""
|
||||
called = {"sync": 0, "rpa": 0}
|
||||
_seed_parse_failed_receipt_file("DSV360_AWSDU")
|
||||
_seed_parse_failed_inbound_file("DSV360_AWSDU")
|
||||
|
||||
def fake_sync(*, source_code, business_date, now):
|
||||
# 断言意图:sync 返回 True 不代表投影恢复——标记未清除时端点必须如实报
|
||||
@@ -636,7 +751,7 @@ def test_retry_rpa_parse_failed_still_failed_reports_not_ok(app, client, admin_h
|
||||
fake_sync,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.receipt_trigger_service.trigger_receipt_rpa",
|
||||
"app.services.in_transit_transfer.inbound_trigger_service.trigger_inbound_rpa",
|
||||
lambda **kw: called.__setitem__("rpa", called["rpa"] + 1) or {"ok": True, "callback_id": "x"},
|
||||
)
|
||||
response = client.post(f"{API_PREFIX}/sources/DSV360_AWSDU/retry-rpa", headers=admin_headers)
|
||||
@@ -660,7 +775,7 @@ def test_retry_rpa_batch_requires_source_codes(app, client, admin_headers):
|
||||
|
||||
|
||||
def test_retry_rpa_batch_delegates_to_batch_trigger_service(app, client, admin_headers, monkeypatch):
|
||||
"""批量端点把 source_codes 原样交给 trigger_receipt_rpa_batch 并回传结果 envelope。"""
|
||||
"""批量端点把 source_codes 原样交给 trigger_inbound_rpa_batch 并回传结果 envelope。"""
|
||||
captured: dict = {}
|
||||
|
||||
def fake_batch(*, source_codes, business_date, reason):
|
||||
@@ -671,7 +786,7 @@ def test_retry_rpa_batch_delegates_to_batch_trigger_service(app, client, admin_h
|
||||
}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.services.in_transit_transfer.receipt_trigger_service.trigger_receipt_rpa_batch", fake_batch,
|
||||
"app.services.in_transit_transfer.inbound_trigger_service.trigger_inbound_rpa_batch", fake_batch,
|
||||
)
|
||||
response = client.post(
|
||||
f"{API_PREFIX}/sources/retry-rpa-batch", headers=admin_headers,
|
||||
@@ -681,7 +796,7 @@ def test_retry_rpa_batch_delegates_to_batch_trigger_service(app, client, admin_h
|
||||
body = response.get_json()["data"]
|
||||
# [断言意图] 端点只做参数卡控与转发:来源列表原样下传、reason 固定业务语义。
|
||||
assert captured["source_codes"] == ["DSV360_AWSDU", "DSV360_AWSSG"]
|
||||
assert captured["reason"] == "transfer_retry_receipt"
|
||||
assert captured["reason"] == "transfer_retry_inbound"
|
||||
assert body["ok"] is True and body["triggered"] is True
|
||||
assert [item["source_code"] for item in body["results"]] == ["DSV360_AWSDU", "DSV360_AWSSG"]
|
||||
|
||||
@@ -833,7 +948,7 @@ def test_retrigger_endpoint_requires_authentication(client):
|
||||
# T11:202 契约收口 + GET /runs/{run_no} 调拨单清单(orders)投影(D9 融合卡数据源)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _receipt_row(row_no: int, doc_no: str, lot_no: str, material: str,
|
||||
def _movement_row(row_no: int, doc_no: str, lot_no: str, material: str,
|
||||
movement_date: str, quantity: str, *, unit: str = "PCS",
|
||||
material_name: str | None = None) -> dict:
|
||||
"""构造一条收料文件行(movement 字段键;多单场景自定义行集用)。"""
|
||||
@@ -853,7 +968,7 @@ def _custom_transfer_deps(monkeypatch, rows: list[dict], stocks: list[tuple[str,
|
||||
"""
|
||||
file_payload = {
|
||||
"source_code": "DBS-AWSBI", "business_date": "2026-08-06",
|
||||
"file_purpose": "receipt", "file_no": "REC-T11", "file_version": "v11",
|
||||
"file_purpose": "inbound", "file_no": "REC-T11", "file_version": "v11",
|
||||
"checksum": "d" * 64, "status": "ready", "rows": rows,
|
||||
}
|
||||
deps, erp, _ = make_transfer_deps(file_payload)
|
||||
@@ -913,9 +1028,9 @@ def test_run_projection_returns_order_list_with_multi_status(
|
||||
monkeypatch,
|
||||
rows=[
|
||||
# 三张单:收料日期入 D1 组键 → 三个组各一单(组建单按组键升序)。
|
||||
_receipt_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个"),
|
||||
_receipt_row(2, "HOX-B", "LOT-20", "MAT-002", "2026-08-04", "700", unit="PCS"),
|
||||
_receipt_row(3, "HOX-C", "LOT-30", "MAT-003", "2026-08-03", "300.25", unit="KG"),
|
||||
_movement_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个"),
|
||||
_movement_row(2, "HOX-B", "LOT-20", "MAT-002", "2026-08-04", "700", unit="PCS"),
|
||||
_movement_row(3, "HOX-C", "LOT-30", "MAT-003", "2026-08-03", "300.25", unit="KG"),
|
||||
],
|
||||
stocks=[("MAT-001", "LOT-10", "500"), ("MAT-002", "LOT-20", "700"),
|
||||
("MAT-003", "LOT-30", "300.25")],
|
||||
@@ -937,7 +1052,7 @@ def test_run_projection_returns_order_list_with_multi_status(
|
||||
assert len(orders) == 3
|
||||
# 断言意图:单序稳定(2026-09-29 投影排序=收料日期→数据源→库位;本例同数据源,
|
||||
# 收料日期升序,与建单的组键序一致)。
|
||||
assert [t["receipt_date"] for t in orders] == ["2026-08-03", "2026-08-04", "2026-08-05"]
|
||||
assert [t["movement_date"] for t in orders] == ["2026-08-03", "2026-08-04", "2026-08-05"]
|
||||
|
||||
by_status = {t["status"]: t for t in orders}
|
||||
assert set(by_status) == {"success", "business_failed", "result_unknown"}
|
||||
@@ -947,11 +1062,11 @@ def test_run_projection_returns_order_list_with_multi_status(
|
||||
# (+invoice_no:行级溯源收料通知号 2026-09-29 契约扩充;含 reason:框线图
|
||||
# 失败行原因展示契约,控制器裁决扩充,T13-3)。
|
||||
assert set(success.keys()) == {
|
||||
"doc_no", "receipt_date", "from_warehouse", "to_warehouse",
|
||||
"doc_no", "movement_date", "from_warehouse", "to_warehouse",
|
||||
"status", "erp_document_no", "error_message", "lines",
|
||||
}
|
||||
assert success["doc_no"].startswith("HX-CN01-OR-")
|
||||
assert success["receipt_date"] == "2026-08-05"
|
||||
assert success["movement_date"] == "2026-08-05"
|
||||
assert success["from_warehouse"] == "DBS-TRANSIT"
|
||||
assert success["to_warehouse"] == "HUB-CN01"
|
||||
assert success["erp_document_no"]
|
||||
@@ -997,7 +1112,7 @@ def test_run_projection_order_lines_include_reused_order_old_and_new_rows(
|
||||
"""复用单行集全量投影:失败单复用后,旧行与新行都在 orders.lines(D7①②)。"""
|
||||
deps, erp = _custom_transfer_deps(
|
||||
monkeypatch,
|
||||
rows=[_receipt_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个")],
|
||||
rows=[_movement_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个")],
|
||||
stocks=[("MAT-001", "LOT-10", "500")],
|
||||
)
|
||||
erp.write_scenarios["HOM-A"] = "business_failed"
|
||||
@@ -1014,8 +1129,8 @@ def test_run_projection_order_lines_include_reused_order_old_and_new_rows(
|
||||
_custom_transfer_deps(
|
||||
monkeypatch,
|
||||
rows=[
|
||||
_receipt_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个"),
|
||||
_receipt_row(3, "HOM-C", "LOT-30", "MAT-003", "2026-08-05", "300", unit="PCS"),
|
||||
_movement_row(1, "HOM-A", "LOT-10", "MAT-001", "2026-08-05", "500", unit="个"),
|
||||
_movement_row(3, "HOM-C", "LOT-30", "MAT-003", "2026-08-05", "300", unit="PCS"),
|
||||
],
|
||||
stocks=[("MAT-001", "LOT-10", "500"), ("MAT-003", "LOT-30", "300")],
|
||||
)
|
||||
@@ -1072,7 +1187,7 @@ def _seed_document(
|
||||
five = {key: (group or {}).get(key) or default for key, default in (
|
||||
("source_code", "DBS-AWSBI"),
|
||||
("external_org", "ORGA"),
|
||||
("receipt_date", "2026-08-05"),
|
||||
("movement_date", "2026-08-05"),
|
||||
("from_warehouse", "DBS-TRANSIT"),
|
||||
("to_warehouse", f"HUB-{doc_no}"),
|
||||
)}
|
||||
@@ -1096,10 +1211,10 @@ def _seed_document(
|
||||
material_no=spec.get("material_no", f"MAT-{index}"),
|
||||
material_name=spec.get("material_name", f"物料{index}"),
|
||||
lot_no=spec.get("lot_no", "LOT-1"),
|
||||
receipt_date=five["receipt_date"],
|
||||
movement_date=five["movement_date"],
|
||||
from_warehouse=five["from_warehouse"],
|
||||
to_warehouse=five["to_warehouse"],
|
||||
receipt_quantity=spec.get("quantity", "10"),
|
||||
movement_quantity=spec.get("quantity", "10"),
|
||||
unit=spec.get("unit", "PCS"),
|
||||
status=spec.get("status", "executing"),
|
||||
result_code=spec.get("result_code"),
|
||||
@@ -1195,7 +1310,7 @@ def test_list_documents_order_projection_and_items(app, transfer_db, client, adm
|
||||
assert data["total"] == 1
|
||||
order = data["orders"][0]
|
||||
# 断言意图:计划 Task 17 响应契约逐键——单头字段 + items(invoice 粒度)。
|
||||
for key in ("doc_no", "submit_timestamp", "status", "status_group", "receipt_date",
|
||||
for key in ("doc_no", "submit_timestamp", "status", "status_group", "movement_date",
|
||||
"from_warehouse", "to_warehouse", "erp_document_no", "error_message",
|
||||
"created_at", "items"):
|
||||
assert key in order
|
||||
@@ -1262,10 +1377,10 @@ def test_list_documents_date_range_filters_list_only(app, transfer_db, client, a
|
||||
with app.app_context():
|
||||
now = datetime(2026, 9, 24, 10, 0, 0)
|
||||
_seed_document(transfer_db, docs_matching, "D-OLD-1", "2026-08-06 09:00:00", "business_failed",
|
||||
group={"receipt_date": "2026-08-25"},
|
||||
group={"movement_date": "2026-08-25"},
|
||||
created_at=now - timedelta(days=30))
|
||||
_seed_document(transfer_db, docs_matching, "D-NEW-1", "2026-08-06 10:00:00", "business_failed",
|
||||
group={"receipt_date": "2026-09-23"},
|
||||
group={"movement_date": "2026-09-23"},
|
||||
created_at=now - timedelta(days=1))
|
||||
transfer_db.session.commit()
|
||||
|
||||
@@ -1310,7 +1425,7 @@ def test_close_document_detaches_items_and_releases_group_lock(
|
||||
from app.models import TransferAuditEvent, TransferDocument, TransferInvoiceItem
|
||||
|
||||
group = {
|
||||
"source_code": "DBS-AWSBI", "external_org": "ORGA", "receipt_date": "2026-08-05",
|
||||
"source_code": "DBS-AWSBI", "external_org": "ORGA", "movement_date": "2026-08-05",
|
||||
"from_warehouse": "DBS-TRANSIT", "to_warehouse": "HUB-CN01",
|
||||
}
|
||||
with app.app_context():
|
||||
@@ -1449,3 +1564,118 @@ def test_document_run_no_projection_and_audit_with_real_run(
|
||||
event = TransferAuditEvent.query.filter_by(event_type="TRANSFER_DOCUMENT_CLOSED").one()
|
||||
assert event.aggregate_id == "D-RUN-1"
|
||||
assert event.run_no == "RUN-DOC-REAL-1"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 待匹配桶「清空并重新解析」(spec §6,Task 7):预检 + 执行端点
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
REPARSE_SOURCE = "CRANE_API"
|
||||
REPARSE_OLD_SHA = "9" * 64
|
||||
REPARSE_SHA_A = "a1" + "0" * 62
|
||||
|
||||
|
||||
def _seed_reparse_endpoint_world(app, monkeypatch):
|
||||
"""端点用例种子:来源+已发布入站规则+一份可用归档+一条旧映射档行。
|
||||
|
||||
归档字节经 FakeArchiveStorage 注入并替换 file_asset_storage.get_bytes
|
||||
(单例对象级替换,service/repository 引用同源生效)。
|
||||
Returns:
|
||||
(archive_file, fake_storage) 元组。
|
||||
"""
|
||||
from tests.services.in_transit_transfer import factories
|
||||
|
||||
fake = factories.FakeArchiveStorage()
|
||||
monkeypatch.setattr(
|
||||
"app.services.file_assets.storage.file_asset_storage.get_bytes", fake.get_bytes)
|
||||
source, _profile = factories.seed_inbound_reparse_source(REPARSE_SOURCE)
|
||||
archive = factories.seed_reparse_archive_file(
|
||||
source, sha=REPARSE_SHA_A, storage_key=f"archive/{REPARSE_SOURCE}/a.xlsx",
|
||||
created_at=datetime(2026, 10, 10, 8, 0, 0))
|
||||
fake.objects[f"archive/{REPARSE_SOURCE}/a.xlsx"] = factories.build_movement_xlsx([
|
||||
["ASN-P1", "1", "I", "MAT-P1", "连接器", "LOT-P1", 500, "2026-08-05"],
|
||||
])
|
||||
with app.app_context():
|
||||
factories.seed_reparse_ledger_row(
|
||||
REPARSE_SOURCE, "ASN-P1", source_file_id=archive.id, sha=REPARSE_OLD_SHA)
|
||||
return archive, fake
|
||||
|
||||
|
||||
def test_pending_match_reparse_preview_endpoint(app, transfer_db, client, admin_headers, monkeypatch):
|
||||
"""GET /backlog/pending-match/reparse-preview → 200 envelope,预检四键形状。"""
|
||||
_archive, _fake = _seed_reparse_endpoint_world(app, monkeypatch)
|
||||
|
||||
response = client.get(
|
||||
f"{API_PREFIX}/backlog/pending-match/reparse-preview?source_code={REPARSE_SOURCE}",
|
||||
headers=admin_headers,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
body = response.get_json()
|
||||
assert body["success"] is True and body.get("request_id")
|
||||
data = body["data"]
|
||||
assert set(data.keys()) == {"files", "profile", "clearable_rows", "kept_rows"}
|
||||
assert data["profile"]["version"] == 1 and data["profile"]["code"]
|
||||
assert data["clearable_rows"] == 1 and data["kept_rows"] == 0
|
||||
assert data["files"][0]["sha256"] == REPARSE_SHA_A
|
||||
assert data["files"][0]["rows"] == 1
|
||||
|
||||
|
||||
def test_pending_match_reparse_preview_requires_source_code(app, client, admin_headers):
|
||||
"""缺 source_code → 400(参数校验,envelope error)。"""
|
||||
response = client.get(
|
||||
f"{API_PREFIX}/backlog/pending-match/reparse-preview", headers=admin_headers)
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_pending_match_reparse_endpoint_executes_and_audits(app, transfer_db, client, admin_headers, monkeypatch):
|
||||
"""POST /backlog/pending-match/reparse → 200 envelope,清空重落计数 + 审计事件。"""
|
||||
from app.models import TransferAuditEvent, TransferMovementLedger
|
||||
|
||||
archive, _fake = _seed_reparse_endpoint_world(app, monkeypatch)
|
||||
|
||||
response = client.post(
|
||||
f"{API_PREFIX}/backlog/pending-match/reparse", headers=admin_headers,
|
||||
json={"source_code": REPARSE_SOURCE},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
body = response.get_json()
|
||||
assert body["success"] is True
|
||||
data = body["data"]
|
||||
assert (data["cleared"], data["reinserted"]) == (1, 1)
|
||||
assert data["kept_unrebuildable"] == 0
|
||||
assert data["collapses"] == 0 # [断言意图] 坍缩计数随端点透出(无坍缩=0)
|
||||
assert data["files"][0]["sha256"] == REPARSE_SHA_A
|
||||
with app.app_context():
|
||||
transfer_db.session.expire_all()
|
||||
row = TransferMovementLedger.query.filter_by(
|
||||
source_code=REPARSE_SOURCE, doc_no="ASN-P1").one()
|
||||
# [断言意图] 新映射值随执行生效且溯源换新(真实 source_file_id/sha256)。
|
||||
assert row.quantity == "500" and row.file_sha256 == REPARSE_SHA_A
|
||||
assert row.source_file_id == archive.id
|
||||
event = TransferAuditEvent.query.filter_by(
|
||||
event_type="TRANSFER_PENDING_MATCH_REPARSED").one()
|
||||
assert event.aggregate_id == REPARSE_SOURCE
|
||||
assert event.after_json["operator"] == "admin"
|
||||
assert event.after_json["cleared"] == 1 and event.after_json["reinserted"] == 1
|
||||
|
||||
|
||||
def test_pending_match_reparse_requires_authentication(client):
|
||||
"""未认证:预检/执行均 401(envelope error)。"""
|
||||
preview = client.get(f"{API_PREFIX}/backlog/pending-match/reparse-preview?source_code=X")
|
||||
execute = client.post(f"{API_PREFIX}/backlog/pending-match/reparse", json={"source_code": "X"})
|
||||
assert preview.status_code == 401
|
||||
assert execute.status_code == 401
|
||||
|
||||
|
||||
def test_pending_match_reparse_forbidden_without_permission(app, client, unauthorized_headers):
|
||||
"""无 in_transit_transfer Agent 权限:预检/执行均 403(越权检查)。"""
|
||||
preview = client.get(
|
||||
f"{API_PREFIX}/backlog/pending-match/reparse-preview?source_code=X",
|
||||
headers=unauthorized_headers)
|
||||
execute = client.post(
|
||||
f"{API_PREFIX}/backlog/pending-match/reparse", headers=unauthorized_headers,
|
||||
json={"source_code": "X"})
|
||||
assert preview.status_code == 403
|
||||
assert execute.status_code == 403
|
||||
Reference in new issue
Block a user