54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
"""配置加载约定测试。"""
|
|
|
|
from pathlib import Path
|
|
|
|
from app.core.config import BACKEND_ENV_FILE, Settings
|
|
|
|
|
|
def test_default_env_file_is_backend_env():
|
|
"""默认 env 文件应固定为 backend/.env 的绝对路径。"""
|
|
|
|
configured_env_file = Path(Settings.model_config["env_file"])
|
|
|
|
assert configured_env_file == BACKEND_ENV_FILE
|
|
assert configured_env_file.is_absolute()
|
|
assert configured_env_file.parent.name == "backend"
|
|
assert configured_env_file.name == ".env"
|
|
|
|
|
|
def test_explicit_env_file_ignores_current_working_directory_dotenv(monkeypatch, tmp_path):
|
|
"""显式 env 文件不应被当前目录 .env 污染。"""
|
|
|
|
root_env = tmp_path / ".env"
|
|
root_env.write_text(
|
|
"\n".join(
|
|
[
|
|
"SECRET_KEY=root-env-should-not-be-used",
|
|
"DATABASE_URL=sqlite+aiosqlite:///root-env.db",
|
|
"DEBUG=false",
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
backend_env = tmp_path / "backend.env"
|
|
backend_env.write_text(
|
|
"\n".join(
|
|
[
|
|
"SECRET_KEY=backend-env-secret",
|
|
"DATABASE_URL=sqlite+aiosqlite:///backend-env.db",
|
|
"DEBUG=true",
|
|
]
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("SECRET_KEY", raising=False)
|
|
monkeypatch.delenv("DATABASE_URL", raising=False)
|
|
|
|
settings = Settings(_env_file=backend_env)
|
|
|
|
assert settings.database_url == "sqlite+aiosqlite:///backend-env.db"
|
|
assert settings.secret_key == "backend-env-secret"
|
|
assert settings.debug is True
|