feat(freight-rpa): 库存/收料改名在手/入站,报表归属RPA,类名统一FreightRpa
把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
ffc0d4a430
commit
39fe3c05f2
453 files changed
+17945
-8646
No files matched your search
@@ -0,0 +1,277 @@
|
||||
"""
|
||||
tests/test_line_inventory_match_persistence.py
|
||||
|
||||
用途:锁定 disposition → match_result 映射、not_found/invalid_key 语义,
|
||||
及行级匹配事实落库契约(含料号兜底总量 part_available_qty)。
|
||||
依赖:pytest、line_onhand_match_persistence、app 夹具(persist 用例)。
|
||||
逻辑:映射用例纯单元;persist 用例写库(内存测试库),不触外部集成。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.services.shipping_notice.line_onhand_match_persistence import (
|
||||
map_disposition_to_match_codes,
|
||||
persist_line_inventory_match_facts,
|
||||
)
|
||||
|
||||
|
||||
def test_complete_key_not_found_is_ready_not_found():
|
||||
"""键完整且 not_found → ready + not_found。"""
|
||||
match_result, status = map_disposition_to_match_codes(
|
||||
disposition='not_found',
|
||||
part_number='PN-1',
|
||||
reservation_id='RSV-1',
|
||||
)
|
||||
assert match_result == 'not_found'
|
||||
assert status == 'ready'
|
||||
|
||||
|
||||
def test_missing_key_is_invalid_not_zero_stock():
|
||||
"""缺料号/预约单 → invalid_key,不得当零库存。"""
|
||||
match_result, status = map_disposition_to_match_codes(
|
||||
disposition='not_found',
|
||||
part_number='',
|
||||
reservation_id='RSV-1',
|
||||
)
|
||||
assert match_result == 'invalid_key'
|
||||
assert status == 'invalid_key'
|
||||
|
||||
|
||||
def test_multi_file_conflict_maps_to_source_conflict():
|
||||
"""多源冲突 → source_conflict。"""
|
||||
match_result, status = map_disposition_to_match_codes(
|
||||
disposition='multi_file_conflict',
|
||||
part_number='PN-1',
|
||||
reservation_id='RSV-1',
|
||||
)
|
||||
assert match_result == 'source_conflict'
|
||||
assert status == 'source_conflict'
|
||||
|
||||
|
||||
def test_ok_and_shortage_are_matched_ready():
|
||||
"""ok/shortage 均为 ready+matched(数量差由投影比较)。"""
|
||||
assert map_disposition_to_match_codes(
|
||||
disposition='ok', part_number='A', reservation_id='B',
|
||||
) == ('matched', 'ready')
|
||||
assert map_disposition_to_match_codes(
|
||||
disposition='shortage', part_number='A', reservation_id='B',
|
||||
) == ('matched', 'ready')
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# persist_line_inventory_match_facts 落库契约(依赖 app 夹具,写内存测试库)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_snapshot_task(task_no: str):
|
||||
"""
|
||||
构造含两行冻结快照的 OT 任务,供 persist 落库测试复用。
|
||||
|
||||
核心逻辑:持久化按 order_snapshot_id → ShippingOrderSnapshot.lines 的 line_no
|
||||
解析 snapshot_line_id;第 1 行料号/预约号齐全,第 2 行缺料号
|
||||
(对齐匹配器「缺料号行不下发 part_available_qty 键」的真实产出,见
|
||||
test_shipping_stp_phase4_line_inventory_match.py 的兜底总量用例)。
|
||||
"""
|
||||
from app import db
|
||||
from app.models import ShippingOrderSnapshot, ShippingOrderSnapshotLine, ShippingOrderTask
|
||||
|
||||
task = ShippingOrderTask(task_no=task_no, source_type='direct_pdf', user_id=1)
|
||||
db.session.add(task)
|
||||
db.session.flush()
|
||||
snapshot = ShippingOrderSnapshot(
|
||||
task_id=task.id,
|
||||
extraction_id=1,
|
||||
schema_version_id=1,
|
||||
order_data_version=1,
|
||||
rule_versions_json={},
|
||||
source_manifest_json={},
|
||||
header_values_json={},
|
||||
)
|
||||
db.session.add(snapshot)
|
||||
db.session.flush()
|
||||
lines = [
|
||||
{'line_no': 1, 'part_number': 'PN-1', 'reservation_id': '3000092917', 'qty': 8},
|
||||
{'line_no': 2, 'part_number': None, 'reservation_id': '3000092918', 'qty': 1},
|
||||
]
|
||||
for position, line in enumerate(lines):
|
||||
db.session.add(
|
||||
ShippingOrderSnapshotLine(
|
||||
snapshot_id=snapshot.id,
|
||||
position=position,
|
||||
line_no=line['line_no'],
|
||||
part_number=line['part_number'],
|
||||
reservation_id=line['reservation_id'],
|
||||
qty=line['qty'],
|
||||
field_values_json={},
|
||||
)
|
||||
)
|
||||
task.order_snapshot_id = snapshot.id
|
||||
db.session.flush()
|
||||
return task
|
||||
|
||||
|
||||
def _not_found_result(*, part_available_qty, line_index: int = 0) -> dict:
|
||||
"""
|
||||
构造 not_found 行结果桩。
|
||||
|
||||
核心逻辑:part_available_qty 传 None 时**不下发该键**(而非显式 None),
|
||||
对齐匹配器对 conflict/缺料号行「无键即无兜底」的契约;持久化必须按
|
||||
无键 → NULL 处理。
|
||||
"""
|
||||
result = {
|
||||
'line_index': line_index,
|
||||
'disposition': 'not_found',
|
||||
'part_number': 'PN-1',
|
||||
'reservation_id': '3000092917',
|
||||
'required_qty': 8.0,
|
||||
'available_qty': 0.0,
|
||||
}
|
||||
if part_available_qty is not None:
|
||||
result['part_available_qty'] = part_available_qty
|
||||
return result
|
||||
|
||||
|
||||
def test_persist_writes_part_available_qty_for_not_found_line(app):
|
||||
"""预约不匹配行的料号兜底总量必须随匹配事实落库。"""
|
||||
with app.app_context():
|
||||
task = _make_snapshot_task('OT-PART-AVAILABLE-NEW')
|
||||
match_payload = {
|
||||
'line_results': [
|
||||
_not_found_result(part_available_qty=8.0),
|
||||
_not_found_result(part_available_qty=None, line_index=1),
|
||||
],
|
||||
'resolutions': [],
|
||||
}
|
||||
rows = persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload=match_payload,
|
||||
inventory_snapshot_id=None,
|
||||
)
|
||||
# 断言意图:兜底总量字符串化落列
|
||||
assert str(rows[0].part_available_qty) == '8.0'
|
||||
# 断言意图:无键/None 时必须落 NULL 而非 'None'
|
||||
assert rows[1].part_available_qty is None
|
||||
|
||||
|
||||
def test_persist_updates_part_available_qty_on_existing_row(app):
|
||||
"""同快照幂等更新时,料号兜底总量必须以最新匹配结果为准。"""
|
||||
with app.app_context():
|
||||
task = _make_snapshot_task('OT-PART-AVAILABLE-UPDATE')
|
||||
persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload={
|
||||
'line_results': [_not_found_result(part_available_qty=8.0)],
|
||||
'resolutions': [],
|
||||
},
|
||||
inventory_snapshot_id=None,
|
||||
)
|
||||
rows = persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload={
|
||||
'line_results': [_not_found_result(part_available_qty=13.0)],
|
||||
'resolutions': [],
|
||||
},
|
||||
inventory_snapshot_id=None,
|
||||
)
|
||||
# 断言意图:更新分支同样把新兜底总量字符串化落列
|
||||
assert str(rows[0].part_available_qty) == '13.0'
|
||||
# 断言意图:更新时键缺失 → 回写 NULL,不得残留旧值或 'None'
|
||||
rows = persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload={
|
||||
'line_results': [_not_found_result(part_available_qty=None)],
|
||||
'resolutions': [],
|
||||
},
|
||||
inventory_snapshot_id=None,
|
||||
)
|
||||
assert rows[0].part_available_qty is None
|
||||
# 断言意图:同快照幂等更新,始终只有一条 current 事实
|
||||
assert len(rows) == 1
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 行级文件归因契约(多源池:锚点文件 ≠ 命中文件)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_inventory_file(app, file_no: str, warehouse_id: str) -> int:
|
||||
"""构造最小可用库存快照文件,返回主键。"""
|
||||
from app import db
|
||||
from app.models import FreightRpaOnhandReportFile
|
||||
|
||||
file_row = FreightRpaOnhandReportFile(
|
||||
onhand_file_no=file_no,
|
||||
storage_path=f'/tmp/{file_no}.xlsx',
|
||||
source_code='DSV360_TEST',
|
||||
warehouse_id=warehouse_id,
|
||||
warehouse_code=warehouse_id,
|
||||
snapshot_status='ready',
|
||||
line_count=1,
|
||||
share_mode='warehouse_pool',
|
||||
)
|
||||
db.session.add(file_row)
|
||||
db.session.flush()
|
||||
return int(file_row.id)
|
||||
|
||||
|
||||
def _ok_result(*, line_index: int = 0, inventory_file_no: str | None = None) -> dict:
|
||||
"""构造命中库存的行结果桩;inventory_file_no 模拟匹配器回填的命中文件身份。"""
|
||||
result = {
|
||||
'line_index': line_index,
|
||||
'disposition': 'ok',
|
||||
'part_number': 'PN-1',
|
||||
'reservation_id': '3000092917',
|
||||
'required_qty': 8.0,
|
||||
'available_qty': 8.0,
|
||||
}
|
||||
if inventory_file_no is not None:
|
||||
result['inventory_file_no'] = inventory_file_no
|
||||
return result
|
||||
|
||||
|
||||
def test_persist_attributes_line_to_hit_file_not_anchor(app):
|
||||
"""多源池下行命中 B 文件时,inventory_snapshot_id 必须归因 B,而非任务锚点 A。
|
||||
|
||||
历史缺陷:持久化把任务级锚点文件 ID 盖到所有行,Excel 仓库列因此写成
|
||||
锚点仓(AWSCV 单写成 CMH01)。
|
||||
"""
|
||||
with app.app_context():
|
||||
from app import db
|
||||
from app.models import FreightRpaOnhandReportFile
|
||||
|
||||
task = _make_snapshot_task('OT-LINE-FILE-ATTR')
|
||||
anchor_id = _make_inventory_file(app, 'SIF-TEST-ANCHOR', 'BYAWSCMH01')
|
||||
hit_id = _make_inventory_file(app, 'SIF-TEST-HIT', 'AWSCV')
|
||||
|
||||
rows = persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload={
|
||||
'line_results': [_ok_result(inventory_file_no='SIF-TEST-HIT')],
|
||||
'resolutions': [],
|
||||
},
|
||||
inventory_snapshot_id=anchor_id,
|
||||
)
|
||||
# 断言意图:行事实挂到命中文件,而不是任务级锚点文件
|
||||
assert rows[0].onhand_snapshot_id == hit_id
|
||||
assert rows[0].matched_file_no == 'SIF-TEST-HIT'
|
||||
# 断言意图:仓库身份可由命中文件直接推出(Excel 仓库列数据源)
|
||||
hit_file = db.session.get(FreightRpaOnhandReportFile, rows[0].onhand_snapshot_id)
|
||||
assert hit_file.warehouse_id == 'AWSCV'
|
||||
|
||||
|
||||
def test_persist_falls_back_to_anchor_without_line_file_no(app):
|
||||
"""行结果未回填文件号(如 not_found)时,保持任务级锚点回退,行为不变。"""
|
||||
with app.app_context():
|
||||
task = _make_snapshot_task('OT-LINE-FILE-FALLBACK')
|
||||
anchor_id = _make_inventory_file(app, 'SIF-TEST-ANCHOR-FB', 'BYAWSCMH01')
|
||||
|
||||
rows = persist_line_inventory_match_facts(
|
||||
task=task,
|
||||
match_payload={
|
||||
'line_results': [_not_found_result(part_available_qty=None)],
|
||||
'resolutions': [],
|
||||
},
|
||||
inventory_snapshot_id=anchor_id,
|
||||
)
|
||||
# 断言意图:无命中文件身份的行回退锚点快照(历史行为兼容)
|
||||
assert rows[0].onhand_snapshot_id == anchor_id
|
||||
Reference in new issue
Block a user