Files
2026-11-09 08:43:30 +03:00

54 lines
1.4 KiB
Python

import os
import pytest
from tests.helpers import create_schema
@pytest.fixture(autouse=True)
def _clean_env(monkeypatch):
"""Isolate tests from the developer's environment."""
import os
for key in list(os.environ):
if key.startswith("EST_"):
monkeypatch.delenv(key, raising=False)
monkeypatch.chdir(__import__("pathlib").Path(__file__).resolve().parent.parent)
@pytest.fixture
def dsn(tmp_path) -> str:
"""A fresh SQLite database with the schema applied."""
url = f"sqlite:///{tmp_path / 'test.db'}"
create_schema(url)
return url
PG_DSN = os.getenv("TEST_PG_DSN") # e.g. postgresql://postgres@127.0.0.1:5433/estimator_test
@pytest.fixture(params=["sqlite", "postgres"])
def backend_dsn(request, tmp_path) -> str:
"""Run a test on SQLite and, when TEST_PG_DSN is set, on PostgreSQL too."""
if request.param == "postgres":
if not PG_DSN:
pytest.skip("set TEST_PG_DSN to run on PostgreSQL")
url = PG_DSN
else:
url = f"sqlite:///{tmp_path / 'test.db'}"
create_schema(url)
return url
@pytest.fixture
def sleeps():
"""A fake `sleep` that records the pauses a retry loop asks for instead of waiting."""
calls: list[float] = []
async def fake_sleep(seconds: float) -> None:
calls.append(seconds)
fake_sleep.calls = calls
return fake_sleep