feat(invoice): 立账凭证检核复制文件加重命名后缀优化

This commit is contained in:
sophie committed 2026-10-28 15:00:49 +08:00
1 parent 02aa00e913
commit 3710aa4d81
10 files changed
+424 -23

No files matched your search

+219 -1
View File
@@ -3,7 +3,7 @@ backend/tests/voucher_check/test_dms_archive.py
用途:销售一期第 5 步归档(建文件夹、复制、同名跳过)单测。
依赖:pytest;不连文档库。
逻辑:mock SalesDmsClient 的 Page/SaveFolder/Copy;断言同名不覆盖、缺月目录为技术失败。
逻辑:mock SalesDmsClient 的 Page/SaveFolder/Copy/Rename/Recycle;断言同名不覆盖、带「重命名」的落盘名改回原名、多余副本删除、缺月目录为技术失败。
"""
from __future__ import annotations
@@ -19,6 +19,8 @@ from app.services.voucher_check.dms_client import (
SalesDmsClient,
_extract_item_id,
_is_name_conflict,
canonical_leaf_name,
leaf_names_match,
)
@@ -55,6 +57,207 @@ def test_ensure_folder_creates_when_missing(monkeypatch):
assert SalesDmsClient.ensure_folder(parent_folder_id="month", leaf_name="所有归档") == "new-folder"
def test_recycle_items_posts_library_delete(monkeypatch):
# 与文档库列表「删除」同一接口:ItemID 数组,进入回收站。
seen: dict[str, object] = {}
def post(path, payload, **_kwargs):
seen["path"] = path
seen["payload"] = payload
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "_post_json", post)
SalesDmsClient.recycle_items(item_ids=["extra-1", "extra-2"])
assert seen["path"] == "/Api/_api/APS/Public/Item/Recycle"
assert seen["payload"] == {"ItemID": ["extra-1", "extra-2"]}
def test_rename_item_posts_library_rename(monkeypatch):
# 与文档库列表「重命名」同一接口:ItemID + 改回后的 LeafName。
seen: dict[str, object] = {}
def post(path, payload, **_kwargs):
seen["path"] = path
seen["payload"] = payload
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "_post_json", post)
SalesDmsClient.rename_item(item_id="dest-r", leaf_name="INV001.pdf")
assert seen["path"] == "/Api/_api/APS/Public/Item/Rename"
assert seen["payload"] == {"ItemID": "dest-r", "LeafName": "INV001.pdf"}
def test_canonical_leaf_name_strips_dms_rename_marker():
# 现网复制后的名字是「原名.pdf(重命名).pdf」,还原后必须等于业务文件名。
assert canonical_leaf_name("AK260929062-8501-2609290005.pdf(重命名).pdf") == (
"AK260929062-8501-2609290005.pdf"
)
assert canonical_leaf_name("INV001(重命名).pdf") == "INV001.pdf"
assert canonical_leaf_name("INV001.pdf") == "INV001.pdf"
assert leaf_names_match("AK260929062-8501-2609290005.pdf(重命名).pdf", "AK260929062-8501-2609290005.pdf")
assert leaf_names_match("OTHER.pdf(重命名).pdf", "AK260929062-8501-2609290005.pdf") is False
assert canonical_leaf_name("QCG260921187-8501-2609210008.pdf (重命名) .pdf") == (
"QCG260921187-8501-2609210008.pdf"
)
def test_find_child_file_accepts_renamed_copy(monkeypatch):
# 目录里只有带「重命名」的文件时,按原名查找应命中,避免再次复制。
monkeypatch.setattr(
SalesDmsClient,
"list_files",
lambda **_kwargs: [{
"ID": "dest-r",
"LeafName": "AK260929062-8501-2609290005.pdf(重命名).pdf",
"IsFolder": False,
}],
)
row = SalesDmsClient.find_child_file(
parent_folder_id="inv-folder",
leaf_name="AK260929062-8501-2609290005.pdf",
)
assert row is not None
assert row["ID"] == "dest-r"
def test_copy_renames_existing_suffix_back_to_original(monkeypatch):
# 目录里只有带「重命名」的文件时,改回原名,不再复制出新文件。
files = {"rows": [{"ID": "dest-r", "LeafName": "INV001.pdf(重命名).pdf"}]}
copies: list[object] = []
renames: list[dict[str, str]] = []
def list_files(**_kwargs):
return files["rows"]
def rename_item(**kwargs):
renames.append(kwargs)
files["rows"] = [{"ID": kwargs["item_id"], "LeafName": kwargs["leaf_name"]}]
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "list_files", list_files)
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **kwargs: copies.append(kwargs))
monkeypatch.setattr(SalesDmsClient, "rename_item", rename_item)
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["LeafName"] == "INV001.pdf"
assert renames == [{"item_id": "dest-r", "leaf_name": "INV001.pdf"}]
assert copies == []
def test_copy_strips_rename_suffix_after_copy(monkeypatch):
# 复制成功但落盘名带「重命名」时,立刻改回业务原名。
files: dict[str, list[dict[str, str]]] = {"rows": []}
renames: list[dict[str, str]] = []
def list_files(**_kwargs):
return files["rows"]
def copy_items(**_kwargs):
files["rows"] = [{"ID": "new-1", "LeafName": "INV001.pdf(重命名).pdf"}]
return {"Code": 0}
def rename_item(**kwargs):
renames.append(kwargs)
files["rows"] = [{"ID": kwargs["item_id"], "LeafName": kwargs["leaf_name"]}]
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "list_files", list_files)
monkeypatch.setattr(SalesDmsClient, "copy_items", copy_items)
monkeypatch.setattr(SalesDmsClient, "rename_item", rename_item)
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"] == "new-1"
assert row["LeafName"] == "INV001.pdf"
assert renames == [{"item_id": "new-1", "leaf_name": "INV001.pdf"}]
def test_copy_keeps_original_and_recycles_renamed_extras(monkeypatch):
# 原名已在时不再复制、不再改名,只删除仍带「重命名」的多余副本。
files = {
"rows": [
{"ID": "clean", "LeafName": "INV001.pdf"},
{"ID": "extra", "LeafName": "INV001.pdf(重命名).pdf"},
{"ID": "other", "LeafName": "别的资料.pdf"},
]
}
recycled: list[list[str]] = []
def list_files(**_kwargs):
return list(files["rows"])
def recycle_items(**kwargs):
recycled.append(list(kwargs["item_ids"]))
drop = set(kwargs["item_ids"])
files["rows"] = [row for row in files["rows"] if row["ID"] not in drop]
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "list_files", list_files)
monkeypatch.setattr(SalesDmsClient, "rename_item", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应改名")))
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应复制")))
monkeypatch.setattr(SalesDmsClient, "recycle_items", recycle_items)
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"] == "clean"
assert recycled == [["extra"]]
assert [item["ID"] for item in files["rows"]] == ["clean", "other"]
def test_copy_restores_one_of_three_renamed_copies_and_recycles_the_rest(monkeypatch):
# 9 月这类目录只有三份带后缀的同一文件时:改回一份原名,删除另外两份。
wanted = "QCG260921187-8501-2609210008.pdf"
renamed = f"{wanted}(重命名).pdf"
files = {
"rows": [
{"ID": "a", "LeafName": renamed},
{"ID": "b", "LeafName": renamed},
{"ID": "c", "LeafName": f"{wanted} (重命名) .pdf"},
]
}
recycled: list[list[str]] = []
def list_files(**_kwargs):
return list(files["rows"])
def rename_item(**kwargs):
for item in files["rows"]:
if item["ID"] == kwargs["item_id"]:
item["LeafName"] = kwargs["leaf_name"]
return {"Code": 0}
def recycle_items(**kwargs):
recycled.append(list(kwargs["item_ids"]))
drop = set(kwargs["item_ids"])
files["rows"] = [row for row in files["rows"] if row["ID"] not in drop]
return {"Code": 0}
monkeypatch.setattr(SalesDmsClient, "list_files", list_files)
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **_kwargs: (_ for _ in ()).throw(AssertionError("不应复制")))
monkeypatch.setattr(SalesDmsClient, "rename_item", rename_item)
monkeypatch.setattr(SalesDmsClient, "recycle_items", recycle_items)
row, skipped = SalesDmsClient.copy_file_skip_same_name(
source_item_id="src-1",
dest_folder_id="inv-folder",
leaf_name=wanted,
)
assert skipped is True
assert row["ID"] == "a"
assert row["LeafName"] == wanted
assert recycled == [["b", "c"]]
assert [item["ID"] for item in files["rows"]] == ["a"]
def test_copy_skips_when_dest_has_same_name(monkeypatch):
monkeypatch.setattr(
SalesDmsClient,
@@ -63,6 +266,11 @@ def test_copy_skips_when_dest_has_same_name(monkeypatch):
)
calls: list[object] = []
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **kwargs: calls.append(kwargs))
monkeypatch.setattr(
SalesDmsClient,
"list_files",
lambda **_kwargs: [{"ID": "dest-1", "LeafName": "INV001.pdf"}],
)
row, skipped = SalesDmsClient.copy_file_skip_same_name(
source_item_id="src-1",
dest_folder_id="inv-folder",
@@ -83,6 +291,11 @@ def test_copy_when_dest_has_no_same_name(monkeypatch):
return {"ID": "dest-2", "LeafName": "INV001.pdf"}
monkeypatch.setattr(SalesDmsClient, "find_child_file", find_child_file)
monkeypatch.setattr(
SalesDmsClient,
"list_files",
lambda **_kwargs: [{"ID": "dest-2", "LeafName": "INV001.pdf"}],
)
copied: list[dict] = []
monkeypatch.setattr(SalesDmsClient, "copy_items", lambda **kwargs: copied.append(kwargs) or {"Code": 0})
row, skipped = SalesDmsClient.copy_file_skip_same_name(
@@ -105,6 +318,11 @@ def test_copy_treats_api_same_name_error_as_skip(monkeypatch):
return {"ID": "dest-3", "LeafName": "INV001.pdf"}
monkeypatch.setattr(SalesDmsClient, "find_child_file", find_child_file)
monkeypatch.setattr(
SalesDmsClient,
"list_files",
lambda **_kwargs: [{"ID": "dest-3", "LeafName": "INV001.pdf"}],
)
monkeypatch.setattr(
SalesDmsClient,
"copy_items",