2026-04-24 11:05:17 +08:00
|
|
|
|
"""effective_candidate_consumables / build_name_mapping:YAML 与分类模型,无 Excel。"""
|
2026-04-23 20:42:21 +08:00
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
from app.services.consumable_vision_algorithm import ConsumableVisionAlgorithmService
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_effective_preserves_non_empty_request() -> None:
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService()
|
2026-04-23 20:42:21 +08:00
|
|
|
|
got = svc.effective_candidate_consumables([" 纱布 ", "缝线", "纱布"])
|
|
|
|
|
|
assert got == ["纱布", "缝线"]
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-24 11:05:17 +08:00
|
|
|
|
def test_effective_empty_uses_model_when_yaml_has_no_names(
|
|
|
|
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
yml = tmp_path / "empty.yaml"
|
|
|
|
|
|
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
|
2026-04-23 20:42:21 +08:00
|
|
|
|
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"]
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-24 11:05:17 +08:00
|
|
|
|
def test_effective_empty_prefers_yaml_class_names(tmp_path: Path) -> None:
|
|
|
|
|
|
yml = tmp_path / "lab.yaml"
|
|
|
|
|
|
yml.write_text(
|
|
|
|
|
|
"names:\n 0: 商品甲\n 1: 商品乙\nlabel_id:\n 0: a\n 1: b\n",
|
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
|
)
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
|
2026-04-24 11:05:17 +08:00
|
|
|
|
assert svc.effective_candidate_consumables([]) == ["商品甲", "商品乙"]
|
2026-04-23 20:42:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_effective_whitespace_only_treated_as_empty(
|
2026-04-24 11:05:17 +08:00
|
|
|
|
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
2026-04-23 20:42:21 +08:00
|
|
|
|
) -> None:
|
2026-04-24 11:05:17 +08:00
|
|
|
|
yml = tmp_path / "empty.yaml"
|
|
|
|
|
|
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
|
2026-04-23 20:42:21 +08:00
|
|
|
|
mock_cls = MagicMock()
|
|
|
|
|
|
mock_cls.names = {0: "x"}
|
|
|
|
|
|
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
|
|
|
|
|
|
assert svc.effective_candidate_consumables(["", " "]) == ["x"]
|
2026-04-24 11:05:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_name_mapping_from_label_id(tmp_path: Path) -> None:
|
|
|
|
|
|
yml = tmp_path / "lab.yaml"
|
|
|
|
|
|
yml.write_text(
|
|
|
|
|
|
"names:\n 0: 商品A\nlabel_id:\n 0: y1/y2\n",
|
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
|
)
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
|
2026-04-24 11:05:17 +08:00
|
|
|
|
m = svc.build_name_mapping(["商品A"])
|
|
|
|
|
|
assert m["商品A"] == "y1/y2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_name_mapping_uses_name_when_no_id_in_yaml(
|
|
|
|
|
|
tmp_path: Path,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
yml = tmp_path / "lab.yaml"
|
|
|
|
|
|
yml.write_text(
|
|
|
|
|
|
"names:\n 0: 仅表内有的\nlabel_id: {}\n",
|
|
|
|
|
|
encoding="utf-8",
|
|
|
|
|
|
)
|
2026-04-24 15:33:22 +08:00
|
|
|
|
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
|
2026-04-24 11:05:17 +08:00
|
|
|
|
m = svc.build_name_mapping(["仅表内有的"])
|
|
|
|
|
|
assert m["仅表内有的"] == "仅表内有的"
|