61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from estimator.config.settings import Settings
|
|
|
|
CONFIG = None # filled by the fixture
|
|
|
|
|
|
@pytest.fixture
|
|
def workdir(tmp_path, monkeypatch):
|
|
from pathlib import Path
|
|
|
|
repo_config = Path(__file__).resolve().parents[3] / "config"
|
|
monkeypatch.chdir(tmp_path) # a different working directory, so a local .env can be written
|
|
monkeypatch.setenv("EST_CONFIG_DIR", str(repo_config))
|
|
return tmp_path
|
|
|
|
|
|
def write_env(path, **pairs):
|
|
(path / ".env").write_text("".join(f"{k}={v}\n" for k, v in pairs.items()))
|
|
|
|
|
|
def test_variables_for_other_tools_in_the_same_env_file_are_ignored(workdir):
|
|
write_env(
|
|
workdir,
|
|
POSTGRES_PASSWORD="abc", APP_PORT="8000", MIGRATE_ON_START="true", # docker compose and the entrypoint
|
|
EST_DATABASE__DSN="sqlite:///x.db", EST_LLM__ANTHROPIC__API_KEY="k",
|
|
) # fmt: skip
|
|
s = Settings()
|
|
assert s.database.dsn.get_secret_value() == "sqlite:///x.db"
|
|
|
|
|
|
def test_a_typo_with_the_prefix_is_still_rejected(workdir):
|
|
write_env(workdir, EST_DATABASE__DSN="sqlite:///x.db", EST_LLM__ANTRHOPIC__API_KEY="k")
|
|
with pytest.raises(ValidationError):
|
|
Settings()
|
|
|
|
|
|
def test_the_real_environment_beats_the_env_file(workdir, monkeypatch):
|
|
write_env(workdir, EST_DATABASE__DSN="sqlite:///from-file.db", EST_LLM__ANTHROPIC__API_KEY="k")
|
|
monkeypatch.setenv("EST_DATABASE__DSN", "sqlite:///from-environment.db")
|
|
assert Settings().database.dsn.get_secret_value() == "sqlite:///from-environment.db"
|
|
|
|
|
|
def test_prefix_matching_is_case_insensitive_like_the_environment(workdir):
|
|
write_env(workdir, est_database__dsn="sqlite:///lower.db", EST_LLM__ANTHROPIC__API_KEY="k")
|
|
assert Settings().database.dsn.get_secret_value() == "sqlite:///lower.db"
|
|
|
|
|
|
def test_the_shipped_env_example_loads_as_is(workdir):
|
|
"""A user copies .env.example to .env: it must work without edits to its structure."""
|
|
from pathlib import Path
|
|
|
|
example = Path(__file__).resolve().parents[3] / ".env.example"
|
|
(workdir / ".env").write_text(example.read_text())
|
|
s = Settings()
|
|
assert s.database.dsn.get_secret_value().startswith("postgresql+psycopg://")
|
|
assert s.api.key_map == {"key1": "client_a", "key2": "client_b"}
|
|
assert s.web.enabled is False # the optional web section is commented out
|
|
|