test(chat): 补充固资图片解析回归用例
This commit is contained in:
1 parent
bf18a7ef1d
commit
dfaa55b365
1 file changed
+94
@@ -0,0 +1,94 @@
|
||||
"""
|
||||
backend/tests/test_fixed_asset_image.py
|
||||
|
||||
用途:验证固定资产图片服务的多图排序与图片抓取兜底逻辑。
|
||||
依赖:pytest、Flask app fixture、FixedAssetImageService。
|
||||
逻辑关系:覆盖“按资产编号选中最佳 jpg”与“预览代理读取外部图片二进制”两个核心场景。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from app.services.fixed_asset_image import FixedAssetImageService
|
||||
|
||||
|
||||
def test_resolve_images_returns_sorted_matches(app, monkeypatch):
|
||||
app.config.update({
|
||||
"FIXED_ASSET_IMAGE_BASE_URL": "http://192.168.8.158:8071/Web/#/login",
|
||||
"FIXED_ASSET_IMAGE_USERNAME": "26040006",
|
||||
"FIXED_ASSET_IMAGE_PASSWORD": "26040006",
|
||||
"FIXED_ASSET_IMAGE_LIBRARY_ID": "lib-1",
|
||||
"FIXED_ASSET_IMAGE_FOLDER_ID": "folder-1",
|
||||
})
|
||||
|
||||
rows = [
|
||||
{
|
||||
"ID": "item-1",
|
||||
"LeafName": "A082700482.",
|
||||
"Path": "总务/固定资产/A082700482.",
|
||||
"VersionID": "ver-1",
|
||||
"MainFileAttachment": {
|
||||
"ID": "att-1",
|
||||
"Extension": ".jpg",
|
||||
"FileSize": 12,
|
||||
},
|
||||
},
|
||||
{
|
||||
"ID": "item-2",
|
||||
"LeafName": "A082700482_备份",
|
||||
"Path": "总务/固定资产/A082700482_备份",
|
||||
"VersionID": "ver-2",
|
||||
"MainFileAttachment": {
|
||||
"ID": "att-2",
|
||||
"Extension": ".png",
|
||||
"FileSize": 20,
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
def fake_request_json(method: str, path: str, **_: object):
|
||||
assert method == "POST"
|
||||
assert path == "/Api/_api/APS/Public/Item/Page"
|
||||
return {"Code": 0, "Data": {"Rows": rows}}
|
||||
|
||||
recorded: list[tuple[str, str]] = []
|
||||
monkeypatch.setattr(FixedAssetImageService, "_request_json", classmethod(lambda cls, method, path, **kwargs: fake_request_json(method, path, **kwargs)))
|
||||
monkeypatch.setattr(FixedAssetImageService, "_record_browse", classmethod(lambda cls, item_id, version_id: recorded.append((item_id, version_id))))
|
||||
|
||||
with app.app_context():
|
||||
result = FixedAssetImageService.resolve_images(asset_id="A082700482", asset_name="攻牙治具")
|
||||
|
||||
assert result["total"] == 2
|
||||
assert result["item"]["attachment_id"] == "att-1"
|
||||
assert [item["attachment_id"] for item in result["items"]] == ["att-1", "att-2"]
|
||||
assert result["items"][0]["name"] == "A082700482.jpg"
|
||||
assert result["items"][0]["url"] == "http://192.168.8.158:8071/Api/_api/DMS/Public/Attachment/Embed?attachmentID=att-1"
|
||||
assert recorded == [("item-1", "ver-1")]
|
||||
|
||||
|
||||
def test_fetch_preview_bytes_reads_external_image(app, monkeypatch):
|
||||
app.config.update({
|
||||
"FIXED_ASSET_IMAGE_BASE_URL": "http://192.168.8.158:8071/Web/#/login",
|
||||
"FIXED_ASSET_IMAGE_USERNAME": "26040006",
|
||||
"FIXED_ASSET_IMAGE_PASSWORD": "26040006",
|
||||
"FIXED_ASSET_IMAGE_LIBRARY_ID": "lib-1",
|
||||
"FIXED_ASSET_IMAGE_FOLDER_ID": "folder-1",
|
||||
})
|
||||
|
||||
response = SimpleNamespace(
|
||||
status_code=200,
|
||||
headers={"content-type": "image/jpeg", "content-length": "4"},
|
||||
content=b"test",
|
||||
url="http://192.168.8.158:8071/Api/_api/DMS/Public/Attachment/Embed?attachmentID=att-1",
|
||||
)
|
||||
monkeypatch.setattr(FixedAssetImageService, "_request", classmethod(lambda cls, method, url_or_path, **kwargs: response))
|
||||
|
||||
with app.app_context():
|
||||
content, content_type, err = FixedAssetImageService.fetch_preview_bytes(
|
||||
"http://192.168.8.158:8071/Api/_api/DMS/Public/Attachment/Embed?attachmentID=att-1",
|
||||
max_size=16,
|
||||
)
|
||||
|
||||
assert err is None
|
||||
assert content == b"test"
|
||||
assert content_type == "image/jpeg"
|
||||
Reference in new issue
Block a user