Files
syops/backend/tests/test_receipt_excel_empty.py
T
hugoandCursor ffc0d4a430 feat(freight-rpa): 批次R解耦编排层并切换 onhand/inbound 契约
将 RPA 编排收编至 freight_rpa,回调迁 /api/freight-rpa/*。
共享目录与 Worker 契约一次切换至 onhand/inbound/upload,
并补 outbound 骨架与 tt16 排程 job key 迁移。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-17 23:18:48 +08:00

107 lines
3.4 KiB
Python

"""
backend/tests/test_receipt_excel_empty.py
用途:收料 Excel「有表头无明细」判空回归;钉死标题行不得当表头。
依赖:openpyxl、scripts/freight_rpa/inbound/inbound_excel_empty。
逻辑:构造 DSV 标题+表头无数据簿;复制生产 drop 样例断言 empty。
"""
from __future__ import annotations
import shutil
import sys
from pathlib import Path
import pytest
from openpyxl import Workbook
_REPO_ROOT = Path(__file__).resolve().parents[2]
_INBOUND_DIR = _REPO_ROOT / 'scripts' / 'freight_rpa' / 'inbound'
if str(_INBOUND_DIR) not in sys.path:
sys.path.insert(0, str(_INBOUND_DIR))
from inbound_excel_empty import ( # noqa: E402
NoInboundDataError,
enforce_inbound_excel_not_empty,
inbound_excel_has_detail_rows,
)
_DROP_SAMPLE = (
_REPO_ROOT
/ 'backend'
/ 'data'
/ 'freight_rpa'
/ 'receipt'
/ 'drop'
/ 'receipt_RUN202609290016193A2E_DSV360_AWSBI_BYAWSPRD01_NA_20260929002248.xlsx'
)
def _write_dsv_title_header_only(path: Path) -> None:
"""写入与生产一致的标题行 + 表头行、无明细。"""
workbook = Workbook()
sheet = workbook.active
assert sheet is not None
sheet.title = 'LognetAWSReceiptDetail3lv'
sheet.append(['AWS Receipt Detail', '1 | + | | | + | + | CBIS-0000000100 | | | '])
sheet.append([
'Supplier',
'External\nASN #',
'WMS\nReceipt #',
'Mode of\nTransport',
'Carrier\nCode',
'Waybill #',
'Type',
'ASN Status',
'Date\nExpected',
'Date\nReceived',
'External\nLine #',
'Supplier ID',
'Carton ID',
'Pallet ID',
'AWS Sku',
])
workbook.save(path)
def test_title_row_must_not_count_as_detail(tmp_path: Path):
"""
标题含 receipt 时旧逻辑会把真表头当成明细 → 误 success。
[断言意图] 仅标题+表头必须判定为无明细(False),enforce 抛 no_receipt_data。
"""
path = tmp_path / 'header_only.xlsx'
_write_dsv_title_header_only(path)
assert inbound_excel_has_detail_rows(path) is False
with pytest.raises(NoInboundDataError, match='no_receipt_data'):
enforce_inbound_excel_not_empty(path)
def test_header_plus_data_row_is_not_empty(tmp_path: Path):
"""有表头且下一行有业务单元格时不得 empty。"""
path = tmp_path / 'with_data.xlsx'
_write_dsv_title_header_only(path)
from openpyxl import load_workbook
workbook = load_workbook(path)
sheet = workbook.active
assert sheet is not None
sheet.append(['Well Trust', 'HOX1', '0001', 'O', 'KN', 'WB1', 'Inbound', 'Closed', '01/01/2026', '', '0010', 'CBIS', '', '', 'SKU-1'])
workbook.save(path)
assert inbound_excel_has_detail_rows(path) is True
enforce_inbound_excel_not_empty(path)
@pytest.mark.skipif(not _DROP_SAMPLE.is_file(), reason='production drop sample not present')
def test_production_header_only_sample_is_empty(tmp_path: Path):
"""
生产样例 receipt_RUN202609290016193A2E_*_BYAWSPRD01_* 仅表头却曾 success。
[断言意图] 复制该文件后必须判 empty / no_receipt_data。
"""
target = tmp_path / _DROP_SAMPLE.name
shutil.copy2(_DROP_SAMPLE, target)
assert inbound_excel_has_detail_rows(target) is False
with pytest.raises(NoInboundDataError, match='no_receipt_data'):
enforce_inbound_excel_not_empty(target)