""" backend/tests/test_receipt_excel_empty.py 用途:收料 Excel「有表头无明细」判空回归;钉死标题行不得当表头。 依赖:openpyxl、scripts/freight_rpa/receipt/receipt_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] _RECEIPT_DIR = _REPO_ROOT / 'scripts' / 'freight_rpa' / 'receipt' if str(_RECEIPT_DIR) not in sys.path: sys.path.insert(0, str(_RECEIPT_DIR)) from receipt_excel_empty import ( # noqa: E402 NoReceiptDataError, enforce_receipt_excel_not_empty, receipt_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 receipt_excel_has_detail_rows(path) is False with pytest.raises(NoReceiptDataError, match='no_receipt_data'): enforce_receipt_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 receipt_excel_has_detail_rows(path) is True enforce_receipt_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 receipt_excel_has_detail_rows(target) is False with pytest.raises(NoReceiptDataError, match='no_receipt_data'): enforce_receipt_excel_not_empty(target)