52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
|
|
"""effective_candidate_consumables:空请求时回退到目录或模型类名。"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from pathlib import Path
|
|||
|
|
from unittest.mock import MagicMock
|
|||
|
|
|
|||
|
|
import pytest
|
|||
|
|
from openpyxl import Workbook
|
|||
|
|
|
|||
|
|
from app.config import Settings
|
|||
|
|
from app.services.consumable_vision_algorithm import ConsumableVisionAlgorithmService
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_effective_preserves_non_empty_request() -> None:
|
|||
|
|
svc = ConsumableVisionAlgorithmService(Settings())
|
|||
|
|
got = svc.effective_candidate_consumables([" 纱布 ", "缝线", "纱布"])
|
|||
|
|
assert got == ["纱布", "缝线"]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_effective_empty_uses_model_class_names(monkeypatch: pytest.MonkeyPatch) -> None:
|
|||
|
|
svc = ConsumableVisionAlgorithmService(Settings())
|
|||
|
|
mock_cls = MagicMock()
|
|||
|
|
mock_cls.names = {0: "ban", 1: "apple"}
|
|||
|
|
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
|
|||
|
|
assert svc.effective_candidate_consumables([]) == ["apple", "ban"]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_effective_empty_prefers_catalog_xlsx(tmp_path: Path) -> None:
|
|||
|
|
xlsx = tmp_path / "cat.xlsx"
|
|||
|
|
wb = Workbook()
|
|||
|
|
ws = wb.active
|
|||
|
|
ws.append(["产品编码", "商品名称"])
|
|||
|
|
ws.append(["C1", "商品乙"])
|
|||
|
|
ws.append(["C2", "商品甲"])
|
|||
|
|
wb.save(xlsx)
|
|||
|
|
|
|||
|
|
settings = Settings(consumable_catalog_xlsx_path=str(xlsx))
|
|||
|
|
svc = ConsumableVisionAlgorithmService(settings)
|
|||
|
|
got = svc.effective_candidate_consumables([])
|
|||
|
|
assert got == ["商品乙", "商品甲"]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_effective_whitespace_only_treated_as_empty(
|
|||
|
|
monkeypatch: pytest.MonkeyPatch,
|
|||
|
|
) -> None:
|
|||
|
|
svc = ConsumableVisionAlgorithmService(Settings())
|
|||
|
|
mock_cls = MagicMock()
|
|||
|
|
mock_cls.names = {0: "x"}
|
|||
|
|
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
|
|||
|
|
assert svc.effective_candidate_consumables(["", " "]) == ["x"]
|