Files
operating-room-monitor-server/tests/test_effective_candidate_consumables.py
Kevin 8a4bad99d3 feat: 配置写死与 baked 模块,Alembic 建表,百度仅 BAIDU_*
- 新增 app/baked/algorithm|pipeline,非部署参数不再走 env;Settings 保留 DB/HTTP/RTSP/海康/百度/MinIO/Demo
- 移除 init_db_schema 与 reload 配置;main 仅 check_database;start*.sh 在 uvicorn 前执行 alembic upgrade head
- 依赖 psycopg[binary] 供 Alembic 同步 URL;alembic/env 注释与预发清单更新
- 撕段门控消费管线、各视频/语音/归档调用改为 baked
- 百度环境变量仅 BAIDU_APP_ID、BAIDU_API_KEY、BAIDU_SECRET_KEY 与 BAIDU_* 超时/ASR;人脸脚本与 baidu_speech 文案同步
- 全量单测与 .env.example 更新;.gitignore 忽略 refs/(本地权重/视频不入库)

Made-with: Cursor
2026-04-24 15:33:22 +08:00

75 lines
2.6 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_mappingYAML 与分类模型,无 Excel。"""
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:
svc = ConsumableVisionAlgorithmService()
got = svc.effective_candidate_consumables([" 纱布 ", "缝线", "纱布"])
assert got == ["纱布", "缝线"]
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")
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
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_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",
)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
assert svc.effective_candidate_consumables([]) == ["商品甲", "商品乙"]
def test_effective_whitespace_only_treated_as_empty(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
yml = tmp_path / "empty.yaml"
yml.write_text("names: {}\nlabel_id: {}\n", encoding="utf-8")
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
mock_cls = MagicMock()
mock_cls.names = {0: "x"}
monkeypatch.setattr(svc, "_get_cls", lambda: mock_cls)
assert svc.effective_candidate_consumables(["", " "]) == ["x"]
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",
)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
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",
)
svc = ConsumableVisionAlgorithmService(labels_yaml_path=str(yml))
m = svc.build_name_mapping(["仅表内有的"])
assert m["仅表内有的"] == "仅表内有的"