Files
operating-room-monitor-server/tests/test_effective_candidate_consumables.py
2026-05-21 15:48:03 +08:00

71 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""effective_candidate_consumables / build_name_mapping仅 YAML无分类模型回退。"""
from __future__ import annotations
from pathlib import Path
import pytest
from app.consumable_catalog import (
build_name_mapping,
effective_candidate_consumables,
normalize_candidate_consumables_raw,
)
def test_normalize_candidate_consumables_raw_strings_and_export_objects() -> None:
assert normalize_candidate_consumables_raw([" a ", "b"]) == ["a", "b"]
raw = [
{"消耗品编号": "14764-2-4", "名称": "一次性使用手术单"},
{"name": "缝线"},
]
assert normalize_candidate_consumables_raw(raw) == ["一次性使用手术单", "缝线"]
assert normalize_candidate_consumables_raw([{}]) == []
assert normalize_candidate_consumables_raw("not-a-list") == []
def test_effective_preserves_non_empty_request() -> None:
got = effective_candidate_consumables([" 纱布 ", "缝线", "纱布"])
assert got == ["纱布", "缝线"]
def test_effective_empty_yaml_no_names_returns_empty(tmp_path: Path) -> None:
yml = tmp_path / "empty.yaml"
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
assert effective_candidate_consumables([], labels_yaml_path=yml) == []
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",
)
assert effective_candidate_consumables([], labels_yaml_path=yml) == ["商品甲", "商品乙"]
def test_effective_whitespace_only_requests_empty_yaml_returns_empty(tmp_path: Path) -> None:
yml = tmp_path / "empty.yaml"
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
assert effective_candidate_consumables(["", " "], labels_yaml_path=yml) == []
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",
)
m = build_name_mapping(["商品A"], labels_yaml_path=yml)
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",
)
m = build_name_mapping(["仅表内有的"], labels_yaml_path=yml)
assert m["仅表内有的"] == "仅表内有的"