test: test_config 用 monkeypatch 替代 importlib.reload
This commit is contained in:
+4
-11
@@ -87,20 +87,13 @@ class TestEmbedConfigEnvVar:
|
|||||||
"""api_key 从环境变量读取."""
|
"""api_key 从环境变量读取."""
|
||||||
|
|
||||||
def test_api_key_from_env(self, monkeypatch):
|
def test_api_key_from_env(self, monkeypatch):
|
||||||
|
"""从环境变量读取 API Key."""
|
||||||
monkeypatch.setenv("EMBED_API_KEY", "sk-env-test")
|
monkeypatch.setenv("EMBED_API_KEY", "sk-env-test")
|
||||||
import importlib
|
cfg = EmbedConfig(mode="api")
|
||||||
from src.core import config
|
|
||||||
importlib.reload(config)
|
|
||||||
cfg = config.EmbedConfig(mode="api")
|
|
||||||
assert cfg.api_key == "sk-env-test"
|
assert cfg.api_key == "sk-env-test"
|
||||||
# 清理
|
|
||||||
importlib.reload(config)
|
|
||||||
|
|
||||||
def test_api_key_empty_when_not_set(self, monkeypatch):
|
def test_api_key_empty_when_not_set(self, monkeypatch):
|
||||||
|
"""未设置时返回空字符串."""
|
||||||
monkeypatch.delenv("EMBED_API_KEY", raising=False)
|
monkeypatch.delenv("EMBED_API_KEY", raising=False)
|
||||||
import importlib
|
cfg = EmbedConfig(mode="api")
|
||||||
from src.core import config
|
|
||||||
importlib.reload(config)
|
|
||||||
cfg = config.EmbedConfig(mode="api")
|
|
||||||
assert cfg.api_key == ""
|
assert cfg.api_key == ""
|
||||||
importlib.reload(config)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user