Files
syops/backend/tests/test_receipt_excel_empty.py
T
hugo 911afa0f73 feat(freight-rpa): 增加出站报表配置与回调支持,重构相关服务
- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。
- 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。
- 更新 ORM 模型,增加出站报表相关的持久化模型。
- 统一命名规范,确保在手、入站与出站的概念一致性。
2026-10-19 18:45:16 +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_inbound_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_inbound_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_inbound_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_inbound_data'):
enforce_inbound_excel_not_empty(target)