b9fcf17ae4
Co-Authored-By: Claude <noreply@anthropic.com>
70 lines
2.5 KiB
Python
70 lines
2.5 KiB
Python
"""Tests for app.config.get_settings — env-var binding and cache isolation."""
|
|
|
|
from pathlib import Path
|
|
|
|
from app.config import Settings, get_settings
|
|
|
|
|
|
def test_get_settings_reads_env_vars(monkeypatch):
|
|
"""get_settings() must reflect env vars set before it is called."""
|
|
# Clear any cached singleton from earlier tests / module imports.
|
|
get_settings.cache_clear()
|
|
|
|
monkeypatch.setenv("YUNKE_BASE_URL", "https://example.test/")
|
|
monkeypatch.setenv("YUNKE_USERNAME", "tester")
|
|
monkeypatch.setenv("YUNKE_PASSWORD", "shh")
|
|
monkeypatch.setenv(
|
|
"DB_URL",
|
|
"mysql+asyncmy://u:p@db.example:3306/yunke_test",
|
|
)
|
|
monkeypatch.setenv("REDIS_URL", "redis://redis.example:6379/2")
|
|
monkeypatch.setenv("MEDIA_DIR", "/tmp/yunke_media")
|
|
monkeypatch.setenv("ENCRYPTION_KEY", "abc123")
|
|
|
|
settings = get_settings()
|
|
|
|
assert settings.yunke_base_url == "https://example.test/"
|
|
assert settings.yunke_username == "tester"
|
|
assert settings.yunke_password == "shh"
|
|
assert settings.db_url == "mysql+asyncmy://u:p@db.example:3306/yunke_test"
|
|
assert settings.redis_url == "redis://redis.example:6379/2"
|
|
assert settings.media_dir == Path("/tmp/yunke_media")
|
|
assert settings.encryption_key == "abc123"
|
|
|
|
|
|
def test_get_settings_is_cached(monkeypatch):
|
|
"""get_settings() returns the same instance until the cache is cleared."""
|
|
get_settings.cache_clear()
|
|
|
|
first = get_settings()
|
|
second = get_settings()
|
|
assert first is second
|
|
|
|
# After clearing the cache, a new instance is built.
|
|
get_settings.cache_clear()
|
|
third = get_settings()
|
|
assert third is not first
|
|
|
|
|
|
def test_get_settings_picks_up_changes_after_cache_clear(monkeypatch):
|
|
"""After cache_clear(), the next call re-reads the environment."""
|
|
get_settings.cache_clear()
|
|
monkeypatch.setenv("DB_URL", "sqlite+aiosqlite:///:memory:")
|
|
initial = get_settings()
|
|
assert initial.db_url == "sqlite+aiosqlite:///:memory:"
|
|
|
|
monkeypatch.setenv("DB_URL", "mysql+asyncmy://other:3306/yunke")
|
|
# Cached value must NOT change just because env changed.
|
|
assert get_settings().db_url == "sqlite+aiosqlite:///:memory:"
|
|
|
|
# After clearing, the new env var is picked up.
|
|
get_settings.cache_clear()
|
|
assert get_settings().db_url == "mysql+asyncmy://other:3306/yunke"
|
|
|
|
|
|
def test_settings_constructable_without_env():
|
|
"""Settings() can be built with defaults (no env required)."""
|
|
s = Settings()
|
|
assert isinstance(s.db_url, str)
|
|
assert s.encryption_key == ""
|