68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""`dwg_review.config` 单元测试(只验证纯函数与常量类型,不依赖真实环境变量)。"""
|
|||
|
|
|
||
|
|
import os
|
||
|
|
|
||
|
|
from dwg_review import config
|
||
|
|
|
||
|
|
|
||
|
|
def test_resolve_path_relative_uses_project_root():
|
||
|
|
assert config._resolve_path("reports") == config.PROJECT_ROOT / "reports"
|
||
|
|
|
||
|
|
|
||
|
|
def test_resolve_path_absolute_unchanged(tmp_path):
|
||
|
|
assert config._resolve_path(str(tmp_path)) == tmp_path
|
||
|
|
|
||
|
|
|
||
|
|
def test_project_root_is_repo_root():
|
||
|
|
assert (config.PROJECT_ROOT / "dwg_review" / "config.py").is_file()
|
||
|
|
|
||
|
|
|
||
|
|
def test_read_env_prefers_environ(monkeypatch):
|
||
|
|
monkeypatch.setenv("CAD_TEST_ENV_PROBE", "hello")
|
||
|
|
assert config._read_env("CAD_TEST_ENV_PROBE") == "hello"
|
||
|
|
|
||
|
|
|
||
|
|
def test_read_env_missing_returns_empty(monkeypatch):
|
||
|
|
monkeypatch.delenv("CAD_TEST_DEFINITELY_MISSING_XYZ", raising=False)
|
||
|
|
assert config._read_env("CAD_TEST_DEFINITELY_MISSING_XYZ") == ""
|
||
|
|
|
||
|
|
|
||
|
|
def test_has_api_key_reflects_module_value(monkeypatch):
|
||
|
|
monkeypatch.setattr(config, "API_KEY", "")
|
||
|
|
assert config.has_api_key() is False
|
||
|
|
monkeypatch.setattr(config, "API_KEY", "sk-x")
|
||
|
|
assert config.has_api_key() is True
|
||
|
|
|
||
|
|
|
||
|
|
def test_has_api_key_ignores_whitespace(monkeypatch):
|
||
|
|
monkeypatch.setattr(config, "API_KEY", " ")
|
||
|
|
assert config.has_api_key() is False
|
||
|
|
|
||
|
|
|
||
|
|
def test_constants_have_expected_types():
|
||
|
|
assert isinstance(config.HOST, str)
|
||
|
|
assert isinstance(config.PORT, int)
|
||
|
|
assert isinstance(config.CALL_TIMEOUT, float)
|
||
|
|
assert isinstance(config.CALL_RETRIES, int)
|
||
|
|
assert isinstance(config.MAX_FRAME_BYTES, int)
|
||
|
|
assert isinstance(config.IMAGE_MAX_EDGE, int)
|
||
|
|
assert isinstance(config.IMAGE_FORMAT, str)
|
||
|
|
assert isinstance(config.CHECKLIST_PATH, type(config.PROJECT_ROOT))
|
||
|
|
|
||
|
|
|
||
|
|
def test_checklist_path_resolves_relative_to_root():
|
||
|
|
assert config.CHECKLIST_PATH.name == "review_checklist.json"
|
||
|
|
assert config.CHECKLIST_PATH.parent == config.PROJECT_ROOT
|
||
|
|
|
||
|
|
|
||
|
|
def test_port_default_matches_baseline():
|
||
|
|
# 基座固定监听 127.0.0.1:8080;若此值被改动,请同步更新基座与文档。
|
||
|
|
assert config.HOST == "127.0.0.1"
|
||
|
|
assert config.PORT == 8080
|
||
|
|
|
||
|
|
|
||
|
|
def test_os_name_guard_in_read_env(monkeypatch):
|
||
|
|
# 非 Windows 平台不应触碰注册表
|
||
|
|
monkeypatch.setattr(os, "name", "posix")
|
||
|
|
monkeypatch.delenv("CAD_TEST_ENV_PROBE2", raising=False)
|
||
|
|
assert config._read_env("CAD_TEST_ENV_PROBE2") == ""
|