""" tests/test_inventory_download_name.py 用途:锁定 build_inventory_download_name 的下载展示名规范化契约—— 复用企微压缩包短名规则({seq}_{source}_{warehouse})并附加 exported_at 数据截止时间点;worker 原始登记名不受影响。 依赖:models.FreightRpaOnhandReportFile、onhand_file_store.build_inventory_download_name。 逻辑:pure_unit;长名规范化、规范名幂等、exported_at → rpa_run_no → mtime 回退链、 warehouse 连续重复段去重(AWSDU_AWSDU → AWSDU)、无 seq 前缀省略、 source/warehouse/时间点任一缺失时保持原名(禁止凭空造名)、空名回退 onhand_file_no。 """ from __future__ import annotations import os from datetime import datetime import pytest from app.models import FreightRpaOnhandReportFile from app.services.shipping_notice.onhand_file_store import ( build_inventory_download_name, ) pytestmark = pytest.mark.pure_unit _WORKER_LONG_NAME = '006_inventory_RUN2026101423006F54E_DSV360_AWSDU_AWSDU_20261014231148.xlsx' def _inv_file(**overrides) -> FreightRpaOnhandReportFile: """构造不落库的快照行;默认字段对齐 prod 4510042199 批次的真实登记值。""" defaults = dict( onhand_file_no='INV-20261014-0001', source_file_name=_WORKER_LONG_NAME, source_code='DSV360', warehouse_id='AWSDU', warehouse_code='AWSDU', exported_at=datetime(2026, 10, 14, 23, 11, 48), rpa_run_no='RUN2026101423006F54E', ) defaults.update(overrides) return FreightRpaOnhandReportFile(**defaults) def test_worker_long_name_normalized_with_exported_at(): """worker 长名 + exported_at → 短名 + 数据截止时间点(到分钟)。""" name = build_inventory_download_name( _inv_file(), source_file_name=_WORKER_LONG_NAME, ) # 关键断言:seq 保留与企微 zip 可对应;时间点来自 exported_at 而非尾部保存时刻 assert name == '006_DSV360_AWSDU_202610142311.xlsx' def test_canonical_name_is_idempotent(): """已是规范名的输入必须原样返回(幂等,不二次追加时间戳)。""" canonical = '006_DSV360_AWSDU_202610142311.xlsx' # 关键断言:规范形态命中 _DOWNLOAD_CANONICAL_RE 后零变化 assert build_inventory_download_name( _inv_file(), source_file_name=canonical, ) == canonical def test_exported_at_missing_falls_back_to_run_no_timestamp(): """exported_at 缺失时回退 rpa_run_no 内嵌 RUN 时间戳(到分钟)。""" name = build_inventory_download_name( _inv_file(exported_at=None), source_file_name=_WORKER_LONG_NAME, ) # 关键断言:RUN2026101423006F54E → 202610142300(截取 12 位到分钟) assert name == '006_DSV360_AWSDU_202610142300.xlsx' def test_time_falls_back_to_file_mtime(tmp_path): """exported_at 与 rpa_run_no 均缺失时回退托管文件 mtime。""" target = tmp_path / 'worker.xlsx' target.write_bytes(b'x') os.utime(target, (1770000000, 1770000000)) name = build_inventory_download_name( _inv_file(exported_at=None, rpa_run_no=None), source_file_name=_WORKER_LONG_NAME, path=target, ) # 关键断言:不抛错且生成带 12 位时间点的规范名(具体值随 mtime) assert name.count('_') >= 3 and name.endswith('.xlsx') assert 'DSV360' in name and 'AWSDU' in name def test_warehouse_duplicated_segments_collapsed(): """warehouse 值自身含连续重复段(AWSDU_AWSDU)时折叠为单段。""" name = build_inventory_download_name( _inv_file(warehouse_id='AWSDU_AWSDU'), source_file_name=_WORKER_LONG_NAME, ) # 关键断言:token 级连续重复去重后不出现 AWSDU_AWSDU assert name == '006_DSV360_AWSDU_202610142311.xlsx' def test_missing_seq_prefix_omits_seq(): """worker 原名无数字前缀时规范名省略 seq 段而非补 000。""" name = build_inventory_download_name( _inv_file(source_file_name='inventory_export.xlsx'), source_file_name='inventory_export.xlsx', ) # 关键断言:无 seq 时以 source 开头且不产生空段 assert name == 'DSV360_AWSDU_202610142311.xlsx' def test_insufficient_info_keeps_original_name(): """source 缺失等信息不足时保持 worker 原名(禁止凭空造名)。""" original = _WORKER_LONG_NAME name = build_inventory_download_name( _inv_file(source_code=None), source_file_name=original, ) # 关键断言:缺 source 时零变化,避免生成无法归因的文件名 assert name == original def test_empty_source_file_name_falls_back_to_file_no(): """空登记名回退 onhand_file_no + 小写后缀(既有行为不变)。""" name = build_inventory_download_name( _inv_file(source_file_name=None), source_file_name=None, ) # 关键断言:与改造前 resolve_task_inventory_download 的兜底行为逐字一致 assert name == 'INV-20261014-0001.xlsx'