187 lines
6.8 KiB
Python
187 lines
6.8 KiB
Python
"""
|
|
backend/tests/voucher_check/test_dms_archive.py
|
|
|
|
用途:销售一期第 5 步归档(建文件夹、复制、同名跳过)单测。
|
|
依赖:pytest;不连文档库。
|
|
逻辑:mock SalesDmsClient 的 Page/SaveFolder/Copy;断言同名不覆盖、缺月目录为技术失败。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
|
|
import pytest
|
|
|
|
from app.core.errors import ServiceUnavailableError
|
|
from app.services.voucher_check.adapters.base import ScannedFile
|
|
from app.services.voucher_check.adapters.dms_archive import DmsArchiveAdapter
|
|
from app.services.voucher_check.dms_client import (
|
|
SalesDmsClient,
|
|
_extract_item_id,
|
|
_is_name_conflict,
|
|
)
|
|
|
|
|
|
def test_extract_item_id_from_nested_data():
|
|
assert _extract_item_id({"Data": {"ID": "folder-1"}}) == "folder-1"
|
|
assert _extract_item_id({"Data": "folder-2"}) == "folder-2"
|
|
assert _extract_item_id({"Data": {"Item": {"ID": "folder-3"}}}) == "folder-3"
|
|
assert _extract_item_id({"Data": {}}) == ""
|
|
|
|
|
|
def test_name_conflict_detects_same_name_messages():
|
|
assert _is_name_conflict("文件夹已存在")
|
|
assert _is_name_conflict("同名文件")
|
|
assert _is_name_conflict("The name already exists")
|
|
assert _is_name_conflict("网络超时") is False
|
|
|
|
|
|
def test_ensure_folder_returns_existing_without_save(monkeypatch):
|
|
monkeypatch.setattr(SalesDmsClient, "find_child_folder", lambda **_kwargs: "folder-1")
|
|
calls: list[object] = []
|
|
monkeypatch.setattr(SalesDmsClient, "_post_json", lambda *_args, **_kwargs: calls.append(1) or {})
|
|
assert SalesDmsClient.ensure_folder(parent_folder_id="month", leaf_name="所有归档") == "folder-1"
|
|
assert calls == []
|
|
|
|
|
|
def test_ensure_folder_creates_when_missing(monkeypatch):
|
|
monkeypatch.setattr(SalesDmsClient, "find_child_folder", lambda **_kwargs: None)
|
|
monkeypatch.setattr(SalesDmsClient, "library_id", lambda: "lib-1")
|
|
monkeypatch.setattr(
|
|
SalesDmsClient,
|
|
"_post_json",
|
|
lambda path, payload, **_kwargs: {"Code": 0, "Data": {"ID": "new-folder"}},
|
|
)
|
|
assert SalesDmsClient.ensure_folder(parent_folder_id="month", leaf_name="所有归档") == "new-folder"
|
|
|
|
|
|
def test_copy_skips_when_dest_has_same_name(monkeypatch):
|
|
monkeypatch.setattr(
|
|
SalesDmsClient,
|
|
"find_child_file",
|
|
lambda **_kwargs: {"ID": "dest-1", "LeafName": "INV001.pdf"},
|
|
)
|
|
calls: list[object] = []
|
|
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **kwargs: calls.append(kwargs))
|
|
row, skipped = SalesDmsClient.copy_file_skip_same_name(
|
|
source_item_id="src-1",
|
|
dest_folder_id="inv-folder",
|
|
leaf_name="INV001.pdf",
|
|
)
|
|
assert skipped is True
|
|
assert row["ID"] == "dest-1"
|
|
assert calls == []
|
|
|
|
|
|
def test_copy_when_dest_has_no_same_name(monkeypatch):
|
|
listing = {"n": 0}
|
|
|
|
def find_child_file(**_kwargs):
|
|
listing["n"] += 1
|
|
if listing["n"] == 1:
|
|
return None
|
|
return {"ID": "dest-2", "LeafName": "INV001.pdf"}
|
|
|
|
monkeypatch.setattr(SalesDmsClient, "find_child_file", find_child_file)
|
|
copied: list[dict] = []
|
|
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **kwargs: copied.append(kwargs) or {"Code": 0})
|
|
row, skipped = SalesDmsClient.copy_file_skip_same_name(
|
|
source_item_id="src-1",
|
|
dest_folder_id="inv-folder",
|
|
leaf_name="INV001.pdf",
|
|
)
|
|
assert skipped is False
|
|
assert row["ID"] == "dest-2"
|
|
assert copied == [{"item_ids": ["src-1"], "dest_folder_id": "inv-folder"}]
|
|
|
|
|
|
def test_copy_treats_api_same_name_error_as_skip(monkeypatch):
|
|
listing = {"n": 0}
|
|
|
|
def find_child_file(**_kwargs):
|
|
listing["n"] += 1
|
|
if listing["n"] == 1:
|
|
return None
|
|
return {"ID": "dest-3", "LeafName": "INV001.pdf"}
|
|
|
|
monkeypatch.setattr(SalesDmsClient, "find_child_file", find_child_file)
|
|
monkeypatch.setattr(
|
|
SalesDmsClient,
|
|
"copy_items",
|
|
lambda **_kwargs: (_ for _ in ()).throw(
|
|
ServiceUnavailableError("目标已存在同名文件", code="VOUCHER_CHECK.ARCHIVE_FAILED")
|
|
),
|
|
)
|
|
row, skipped = SalesDmsClient.copy_file_skip_same_name(
|
|
source_item_id="src-1",
|
|
dest_folder_id="inv-folder",
|
|
leaf_name="INV001.pdf",
|
|
)
|
|
assert skipped is True
|
|
assert row["ID"] == "dest-3"
|
|
|
|
|
|
def test_archive_adapter_is_wired_and_skips_same_name(monkeypatch):
|
|
monkeypatch.setattr(SalesDmsClient, "ensure_month_archive_root", lambda **_kwargs: "archive-root")
|
|
monkeypatch.setattr(SalesDmsClient, "ensure_folder", lambda **_kwargs: "inv-folder")
|
|
monkeypatch.setattr(
|
|
SalesDmsClient,
|
|
"copy_file_skip_same_name",
|
|
lambda **_kwargs: ({"ID": "dest-9", "LeafName": "INV001-PO9.pdf"}, True),
|
|
)
|
|
adapter = DmsArchiveAdapter()
|
|
assert adapter.wired is True
|
|
archived = adapter.archive(
|
|
period_month=date(2026, 8, 1),
|
|
invoice_no="INV001",
|
|
file=ScannedFile(filename="INV001-PO9.pdf", item_id="src-9", fingerprint="fp1"),
|
|
rule={"code": "po_order"},
|
|
)
|
|
assert archived.item_id == "dest-9"
|
|
assert archived.folder_id == "inv-folder"
|
|
assert archived.extra["skipped_same_name"] is True
|
|
assert archived.path == "销售开票附件/2026/08/所有归档/INV001/INV001-PO9.pdf"
|
|
|
|
|
|
def test_archive_adapter_missing_source_id_is_technical():
|
|
adapter = DmsArchiveAdapter()
|
|
with pytest.raises(ServiceUnavailableError) as exc:
|
|
adapter.archive(
|
|
period_month=date(2026, 8, 1),
|
|
invoice_no="INV001",
|
|
file=ScannedFile(filename="INV001.pdf"),
|
|
rule={"code": "tax_invoice"},
|
|
)
|
|
assert exc.value.code == "VOUCHER_CHECK.ARCHIVE_FAILED"
|
|
|
|
|
|
def test_month_archive_root_missing_month_is_technical(monkeypatch):
|
|
monkeypatch.setattr(SalesDmsClient, "resolve_period_folder", lambda **_kwargs: (None, None))
|
|
with pytest.raises(ServiceUnavailableError) as exc:
|
|
SalesDmsClient.ensure_month_archive_root(period_month=date(2026, 8, 1))
|
|
assert exc.value.code == "VOUCHER_CHECK.ARCHIVE_FAILED"
|
|
|
|
|
|
def test_page_walks_until_short_batch(monkeypatch):
|
|
"""「所有归档」超过一页时必须翻页,否则同名跳过会漏检已有发票目录。"""
|
|
calls = {"n": 0}
|
|
|
|
def post(_path, payload, **_kwargs):
|
|
calls["n"] += 1
|
|
index = payload["Page"]["Index"]
|
|
if index == 1:
|
|
return {
|
|
"Code": 0,
|
|
"Data": {"Items": [{"ID": "a", "IsFolder": True, "LeafName": "x"}] * 200, "Total": 201},
|
|
}
|
|
return {
|
|
"Code": 0,
|
|
"Data": {"Items": [{"ID": "b", "IsFolder": True, "LeafName": "y"}], "Total": 201},
|
|
}
|
|
|
|
monkeypatch.setattr(SalesDmsClient, "_post_json", post)
|
|
monkeypatch.setattr(SalesDmsClient, "library_id", lambda: "lib-1")
|
|
rows = SalesDmsClient.page(parent_folder_id="archive-root")
|
|
assert len(rows) == 201
|
|
assert calls["n"] == 2
|