feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务
- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。 - 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。 - 更新 ORM 模型,增加出站报表相关的持久化模型。 - 统一命名规范,确保在手、入站与出站的概念一致性。
This commit is contained in:
1 parent
39fe3c05f2
commit
911afa0f73
152 files changed
+9861
-2984
No files matched your search
@@ -7,30 +7,17 @@ import hashlib
|
||||
import pytest
|
||||
|
||||
|
||||
def _register_legacy_terminal_handler(unit):
|
||||
"""注册测试用 inbound 事实处理函数,复刻拆分前的终态收尾(桥 + 证据同步 + finalize)。
|
||||
def _register_inbound_report_handler():
|
||||
"""注册生产用 inbound 事实处理函数(调拨侧 handle_inbound_report)。
|
||||
|
||||
核心逻辑:Task 6 起 ingest_inbound_callback 不再收 on_terminal 钩子,本文件
|
||||
的桥接/进度断言改经 register_report_handler 注入留守的
|
||||
inbound_report_storage._finish_inbound_callback_terminal(Task 7 将平移为
|
||||
in_transit_transfer 的 handle_inbound_report);fact → 旧钩子实参逐字段换算,
|
||||
行为与拆分前逐点一致。
|
||||
核心逻辑:归档入口终态 commit 后经 dispatch_report_fact 投递;测试在调用
|
||||
ingest_inbound_callback 前显式注册,避免依赖 create_app 全局状态顺序。
|
||||
"""
|
||||
from app.services.freight_rpa.reports.dispatch import register_report_handler
|
||||
from app.services.shipping_notice.inbound_report_storage import _finish_inbound_callback_terminal
|
||||
from app.services.in_transit_transfer.inbound_report_handler import handle_inbound_report
|
||||
|
||||
def _handle(fact) -> None:
|
||||
_finish_inbound_callback_terminal(
|
||||
unit,
|
||||
ok=fact.ok,
|
||||
file_no=str(fact.file_id) if fact.file_id is not None else None,
|
||||
file_sha256=fact.sha256,
|
||||
error_code=fact.error_code,
|
||||
error_message=fact.error_message,
|
||||
)
|
||||
|
||||
register_report_handler("inbound", _handle)
|
||||
return _handle
|
||||
register_report_handler('inbound', handle_inbound_report)
|
||||
return handle_inbound_report
|
||||
|
||||
|
||||
def test_receipt_unit_processing_and_batch_progress(app):
|
||||
@@ -38,8 +25,8 @@ def test_receipt_unit_processing_and_batch_progress(app):
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun
|
||||
from app.services.schedule.freight_rpa_inbound_finalize import (
|
||||
mark_receipt_unit_processing,
|
||||
report_receipt_batch_progress,
|
||||
mark_inbound_unit_processing,
|
||||
report_inbound_batch_progress,
|
||||
)
|
||||
|
||||
with app.app_context():
|
||||
@@ -53,7 +40,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-RECEIPT-LIVE', schedule_task_id=task.id, status='running',
|
||||
total_count=2, success_count=0, failed_count=0, skipped_count=0,
|
||||
response_summary_json={'awaiting_receipt_rpa': True},
|
||||
response_summary_json={'awaiting_inbound_rpa': True},
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.flush()
|
||||
@@ -65,7 +52,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
|
||||
db.session.add(unit)
|
||||
db.session.commit()
|
||||
|
||||
marked = mark_receipt_unit_processing({
|
||||
marked = mark_inbound_unit_processing({
|
||||
'schema_version': 'freight_rpa.inbound.v1',
|
||||
'callback_id': unit.callback_id,
|
||||
'status': 'processing',
|
||||
@@ -74,7 +61,7 @@ def test_receipt_unit_processing_and_batch_progress(app):
|
||||
db.session.expire_all()
|
||||
assert db.session.get(FreightRpaInboundUnitRun, unit.id).status == 'processing'
|
||||
|
||||
progressed = report_receipt_batch_progress({
|
||||
progressed = report_inbound_batch_progress({
|
||||
'schema_version': 'freight_rpa.inbound.v1',
|
||||
'schedule_run_id': run.id,
|
||||
'batch_run_no': run.run_no,
|
||||
@@ -93,7 +80,7 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
|
||||
"""success 回调在 finalize 前须回写 ScheduleRun 进度计数(对齐 empty/failed)。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun
|
||||
from app.services.shipping_notice.inbound_report_storage import ingest_receipt_callback
|
||||
from app.services.freight_rpa.inbound.report_archive import ingest_inbound_callback
|
||||
|
||||
with app.app_context():
|
||||
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
@@ -106,7 +93,7 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
|
||||
run = ScheduleRun(
|
||||
run_no='RUN-RECEIPT-PROG', schedule_task_id=task.id, status='running',
|
||||
total_count=1, success_count=0, failed_count=0, skipped_count=0,
|
||||
response_summary_json={'awaiting_receipt_rpa': True},
|
||||
response_summary_json={'awaiting_inbound_rpa': True},
|
||||
)
|
||||
db.session.add(run)
|
||||
db.session.flush()
|
||||
@@ -125,7 +112,7 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
|
||||
'app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes',
|
||||
lambda **_kwargs: None,
|
||||
)
|
||||
_register_legacy_terminal_handler(unit)
|
||||
_register_inbound_report_handler()
|
||||
payload = {
|
||||
'schema_version': 'freight_rpa.inbound.v1',
|
||||
'callback_id': unit.callback_id,
|
||||
@@ -134,7 +121,7 @@ def test_receipt_callback_success_refreshes_schedule_progress(app, monkeypatch):
|
||||
'original_filename': 'receipt.xlsx',
|
||||
'sha256': hashlib.sha256(b'receipt-bytes').hexdigest(),
|
||||
}
|
||||
ingest_receipt_callback(payload)
|
||||
ingest_inbound_callback(payload)
|
||||
db.session.expire_all()
|
||||
refreshed = db.session.get(ScheduleRun, run.id)
|
||||
# [断言意图] success 后进度至少抬升 success_count;终态可能已 finalize 清 awaiting。
|
||||
@@ -145,7 +132,7 @@ def test_receipt_callback_archives_once_without_inventory_file(app, monkeypatch)
|
||||
"""同一 callback 重放只归档一条收料文件,绝不创建库存快照。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaOnhandReportFile, FreightRpaSource, FreightRpaInboundUnitRun
|
||||
from app.services.shipping_notice.inbound_report_storage import ingest_receipt_callback
|
||||
from app.services.freight_rpa.inbound.report_archive import ingest_inbound_callback
|
||||
|
||||
with app.app_context():
|
||||
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
@@ -157,10 +144,10 @@ def test_receipt_callback_archives_once_without_inventory_file(app, monkeypatch)
|
||||
db.session.add(unit); db.session.commit()
|
||||
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive._read_inbound_callback_file', lambda _path: b'receipt-bytes')
|
||||
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes', lambda **_kwargs: None)
|
||||
_register_legacy_terminal_handler(unit)
|
||||
_register_inbound_report_handler()
|
||||
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
|
||||
first = ingest_receipt_callback(payload)
|
||||
second = ingest_receipt_callback(payload)
|
||||
first = ingest_inbound_callback(payload)
|
||||
second = ingest_inbound_callback(payload)
|
||||
assert first['file_id'] == second['file_id']
|
||||
assert FreightRpaOnhandReportFile.query.count() == 0
|
||||
|
||||
@@ -169,7 +156,7 @@ def test_ingest_success_advances_transfer_waiting_job(app, monkeypatch):
|
||||
"""收料回调 success 落归档后,必须推进调拨 waiting 来源任务(B3 接线)。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun, TransferSourceJob, TransferRun
|
||||
from app.services.shipping_notice.inbound_report_storage import ingest_receipt_callback
|
||||
from app.services.freight_rpa.inbound.report_archive import ingest_inbound_callback
|
||||
|
||||
with app.app_context():
|
||||
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
@@ -185,10 +172,10 @@ def test_ingest_success_advances_transfer_waiting_job(app, monkeypatch):
|
||||
db.session.add(job); db.session.commit()
|
||||
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive._read_inbound_callback_file', lambda _path: b'receipt-bytes')
|
||||
monkeypatch.setattr('app.services.freight_rpa.inbound.report_archive.file_asset_storage.upload_bytes', lambda **_kwargs: None)
|
||||
_register_legacy_terminal_handler(unit)
|
||||
_register_inbound_report_handler()
|
||||
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'success', 'file_path': 'Z:/receipt.xlsx', 'original_filename': 'receipt.xlsx', 'sha256': hashlib.sha256(b'receipt-bytes').hexdigest()}
|
||||
|
||||
result = ingest_receipt_callback(payload)
|
||||
result = ingest_inbound_callback(payload)
|
||||
# [断言意图] 桥接发生:收料归档后调拨任务状态推进 file_ready,file_no 为归档文件 id
|
||||
db.session.expire_all()
|
||||
advanced = db.session.get(TransferSourceJob, job.id)
|
||||
@@ -200,7 +187,7 @@ def test_ingest_failed_advance_is_durable(app):
|
||||
"""failed 回调推进调拨任务后必须已 commit——rollback 不得丢弃该推进(B3 持久性)。"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun, TransferSourceJob, TransferRun
|
||||
from app.services.shipping_notice.inbound_report_storage import ingest_receipt_callback
|
||||
from app.services.freight_rpa.inbound.report_archive import ingest_inbound_callback
|
||||
|
||||
with app.app_context():
|
||||
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
@@ -214,10 +201,10 @@ def test_ingest_failed_advance_is_durable(app):
|
||||
db.session.add(transfer_run); db.session.flush()
|
||||
job = TransferSourceJob(run_id=transfer_run.id, source_code=source.source_code, status='waiting_rpa_callback')
|
||||
db.session.add(job); db.session.commit()
|
||||
_register_legacy_terminal_handler(unit)
|
||||
_register_inbound_report_handler()
|
||||
payload = {'schema_version': 'freight_rpa.inbound.v1', 'callback_id': unit.callback_id, 'status': 'failed', 'error_code': 'RPA_TEST_FAIL', 'error_message': 'boom'}
|
||||
|
||||
result = ingest_receipt_callback(payload)
|
||||
result = ingest_inbound_callback(payload)
|
||||
assert result['status'] == 'failed'
|
||||
# [断言意图] 推进必须已落库而非仅 flush:rollback 丢弃会话内未提交变更,
|
||||
# 只有真正 commit 过的推进才能在 expire_all 后被新查询读到。
|
||||
@@ -240,19 +227,19 @@ def test_receipt_callback_rejects_file_outside_receipt_drop_root(app, tmp_path):
|
||||
outside = tmp_path / 'outside.xlsx'
|
||||
outside.write_bytes(b'not-a-real-xlsx')
|
||||
|
||||
with pytest.raises(ValidationError, match='receipt callback file'):
|
||||
with pytest.raises(ValidationError, match='inbound callback file'):
|
||||
_read_inbound_callback_file(str(outside))
|
||||
|
||||
|
||||
def test_ingest_empty_persists_no_receipt_data(app):
|
||||
def test_ingest_empty_persists_no_inbound_data(app):
|
||||
"""
|
||||
empty 回调须落库 no_receipt_data(对齐库存 no_inventory_data)。
|
||||
empty 回调须落库 no_inbound_data(对齐库存 no_inventory_data)。
|
||||
|
||||
[断言意图] 单元 status=empty 且 error_code/error_message 为 no_receipt_data,供管理端展示跳过原因。
|
||||
[断言意图] 单元 status=empty 且 error_code/error_message 为 no_inbound_data,供管理端展示跳过原因。
|
||||
"""
|
||||
from app import db
|
||||
from app.models import ScheduleRun, ScheduleTask, FreightRpaSource, FreightRpaInboundUnitRun
|
||||
from app.services.shipping_notice.inbound_report_storage import ingest_receipt_callback
|
||||
from app.services.freight_rpa.inbound.report_archive import ingest_inbound_callback
|
||||
|
||||
with app.app_context():
|
||||
source = FreightRpaSource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
@@ -281,22 +268,22 @@ def test_ingest_empty_persists_no_receipt_data(app):
|
||||
)
|
||||
db.session.add(unit)
|
||||
db.session.commit()
|
||||
_register_legacy_terminal_handler(unit)
|
||||
_register_inbound_report_handler()
|
||||
|
||||
result = ingest_receipt_callback({
|
||||
result = ingest_inbound_callback({
|
||||
'schema_version': 'freight_rpa.inbound.v1',
|
||||
'callback_id': unit.callback_id,
|
||||
'status': 'empty',
|
||||
'error_code': 'no_receipt_data',
|
||||
'error_message': 'no_receipt_data',
|
||||
'error_code': 'no_inbound_data',
|
||||
'error_message': 'no_inbound_data',
|
||||
})
|
||||
assert result['status'] == 'empty'
|
||||
db.session.expire_all()
|
||||
saved = db.session.get(FreightRpaInboundUnitRun, unit.id)
|
||||
assert saved is not None
|
||||
assert saved.status == 'empty'
|
||||
assert saved.error_code == 'no_receipt_data'
|
||||
assert saved.error_message == 'no_receipt_data'
|
||||
assert saved.error_code == 'no_inbound_data'
|
||||
assert saved.error_message == 'no_inbound_data'
|
||||
|
||||
|
||||
def test_receipt_progress_routes_are_registered(app):
|
||||
@@ -343,5 +330,5 @@ def test_read_receipt_callback_file_resolves_drop_root_filename(app, tmp_path):
|
||||
) == b'receipt-bytes'
|
||||
outside = tmp_path / 'outside.xlsx'
|
||||
outside.write_bytes(b'x')
|
||||
with pytest.raises(ValidationError, match='receipt callback file'):
|
||||
with pytest.raises(ValidationError, match='inbound callback file'):
|
||||
_read_inbound_callback_file(str(outside))
|
||||
Reference in new issue
Block a user