test: update test_config.py for load_bootstrap API

This commit is contained in:
2026-03-18 22:31:17 -04:00
parent 242398dc52
commit f9ea87187b
+22 -18
View File
@@ -1,35 +1,39 @@
import os
import textwrap
import pytest
from fablednetmon.config import load_config
from fablednetmon.config import load_bootstrap
def test_load_minimal_config(tmp_path):
def test_load_bootstrap_from_yaml(tmp_path):
cfg_file = tmp_path / "config.yaml"
cfg_file.write_text(textwrap.dedent("""\
database:
url: postgresql+asyncpg://user:pass@localhost/fablednetmon
secret_key: test-secret
"""))
cfg = load_config(cfg_file)
assert cfg["database"]["url"] == "postgresql+asyncpg://user:pass@localhost/fablednetmon"
cfg = load_bootstrap(cfg_file)
assert cfg["database_url"] == "postgresql+asyncpg://user:pass@localhost/fablednetmon"
assert cfg["secret_key"] == "test-secret"
def test_env_var_overrides_scalar(tmp_path, monkeypatch):
def test_env_var_database_url_overrides_yaml(tmp_path, monkeypatch):
cfg_file = tmp_path / "config.yaml"
cfg_file.write_text("database:\n url: original\nsecret_key: s\n")
monkeypatch.setenv("FABLEDNETMON_DATABASE__URL", "overridden")
cfg = load_config(cfg_file)
assert cfg["database"]["url"] == "overridden"
monkeypatch.setenv("FABLEDNETMON_DATABASE_URL", "overridden")
cfg = load_bootstrap(cfg_file)
assert cfg["database_url"] == "overridden"
def test_env_var_does_not_override_list(tmp_path, monkeypatch):
cfg_file = tmp_path / "config.yaml"
cfg_file.write_text("smtp:\n recipients:\n - a@b.com\nsecret_key: s\ndatabase:\n url: x\n")
monkeypatch.setenv("FABLEDNETMON_SMTP__RECIPIENTS", "bad")
cfg = load_config(cfg_file)
assert cfg["smtp"]["recipients"] == ["a@b.com"]
def test_missing_required_key_raises(tmp_path):
def test_env_var_secret_key_overrides_yaml(tmp_path, monkeypatch):
cfg_file = tmp_path / "config.yaml"
cfg_file.write_text("{}")
with pytest.raises(ValueError, match="secret_key"):
load_config(cfg_file)
cfg_file.write_text("database:\n url: x\nsecret_key: from-yaml\n")
monkeypatch.setenv("FABLEDNETMON_SECRET_KEY", "from-env")
cfg = load_bootstrap(cfg_file)
assert cfg["secret_key"] == "from-env"
def test_missing_database_url_raises(tmp_path):
cfg_file = tmp_path / "config.yaml"
cfg_file.write_text("secret_key: s\n")
with pytest.raises(ValueError, match="Database URL is required"):
load_bootstrap(cfg_file)