Files
home-automation/tests/conftest.py
T

143 lines
4.2 KiB
Python
Raw Normal View History

2026-04-19 23:02:43 +02:00
from pathlib import Path
2026-04-19 20:19:58 +02:00
import pytest
2026-04-19 23:02:43 +02:00
from alembic import command
from alembic.config import Config
2026-04-19 20:19:58 +02:00
from fastapi.testclient import TestClient
2026-04-20 10:42:35 +02:00
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
2026-04-19 20:19:58 +02:00
2026-04-20 15:16:47 +02:00
from app.auth_db import reset_auth_db_caches
2026-04-20 10:42:35 +02:00
import app.db as app_db
2026-04-19 23:02:43 +02:00
from app.config import get_settings
2026-04-19 20:19:58 +02:00
from app.main import create_app
2026-04-20 15:16:47 +02:00
def _make_app_alembic_config(database_url: str) -> Config:
config = Config("alembic_app.ini")
config.set_main_option("sqlalchemy.url", database_url)
return config
2026-04-19 23:02:43 +02:00
def _make_alembic_config(database_url: str) -> Config:
config = Config("alembic_location.ini")
config.set_main_option("sqlalchemy.url", database_url)
return config
def _make_poo_alembic_config(database_url: str) -> Config:
config = Config("alembic_poo.ini")
2026-04-19 23:02:43 +02:00
config.set_main_option("sqlalchemy.url", database_url)
return config
@pytest.fixture
def test_database_urls(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
2026-04-20 15:16:47 +02:00
app_database_path = tmp_path / "app_test.db"
2026-04-19 23:02:43 +02:00
location_database_path = tmp_path / "location_test.db"
poo_database_path = tmp_path / "poo_placeholder.db"
2026-04-20 15:16:47 +02:00
app_database_url = f"sqlite:///{app_database_path}"
2026-04-19 23:02:43 +02:00
location_database_url = f"sqlite:///{location_database_path}"
poo_database_url = f"sqlite:///{poo_database_path}"
2026-04-20 15:16:47 +02:00
monkeypatch.setenv("APP_DATABASE_URL", app_database_url)
2026-04-19 23:02:43 +02:00
monkeypatch.setenv("LOCATION_DATABASE_URL", location_database_url)
monkeypatch.setenv("POO_DATABASE_URL", poo_database_url)
2026-04-20 15:16:47 +02:00
monkeypatch.setenv("AUTH_BOOTSTRAP_USERNAME", "admin")
monkeypatch.setenv("AUTH_BOOTSTRAP_PASSWORD", "test-password")
2026-04-20 17:43:24 +02:00
monkeypatch.setenv("AUTH_COOKIE_SECURE_OVERRIDE", "false")
2026-04-19 23:02:43 +02:00
get_settings.cache_clear()
2026-04-20 15:16:47 +02:00
reset_auth_db_caches()
2026-04-19 23:02:43 +02:00
try:
yield {
2026-04-20 15:16:47 +02:00
"app_path": app_database_path,
"app_url": app_database_url,
2026-04-19 23:02:43 +02:00
"location_path": location_database_path,
"location_url": location_database_url,
"poo_path": poo_database_path,
"poo_url": poo_database_url,
}
finally:
get_settings.cache_clear()
2026-04-20 15:16:47 +02:00
reset_auth_db_caches()
2026-04-19 23:02:43 +02:00
@pytest.fixture
def ready_location_database(test_database_urls):
command.upgrade(_make_alembic_config(test_database_urls["location_url"]), "head")
return test_database_urls
2026-04-19 20:19:58 +02:00
@pytest.fixture
def ready_poo_database(test_database_urls):
command.upgrade(_make_poo_alembic_config(test_database_urls["poo_url"]), "head")
return test_database_urls
@pytest.fixture
2026-04-20 15:16:47 +02:00
def auth_database(test_database_urls, monkeypatch: pytest.MonkeyPatch):
database_url = test_database_urls["app_url"]
command.upgrade(_make_app_alembic_config(database_url), "head")
reset_auth_db_caches()
yield test_database_urls
reset_auth_db_caches()
@pytest.fixture
def app(ready_location_database, ready_poo_database, auth_database):
2026-04-19 23:02:43 +02:00
yield create_app()
2026-04-19 20:19:58 +02:00
@pytest.fixture
def client(app):
with TestClient(app) as test_client:
yield test_client
2026-04-20 10:42:35 +02:00
@pytest.fixture
2026-04-20 15:16:47 +02:00
def location_client(
ready_location_database,
ready_poo_database,
auth_database,
monkeypatch: pytest.MonkeyPatch,
):
2026-04-20 10:42:35 +02:00
database_url = ready_location_database["location_url"]
engine = create_engine(database_url, connect_args={"check_same_thread": False})
session_local = sessionmaker(bind=engine, autoflush=False, autocommit=False)
monkeypatch.setattr(app_db, "engine", engine)
monkeypatch.setattr(app_db, "SessionLocal", session_local)
fastapi_app = create_app()
with TestClient(fastapi_app) as client:
yield client, engine
engine.dispose()
@pytest.fixture
2026-04-20 15:16:47 +02:00
def poo_client(
ready_location_database,
ready_poo_database,
auth_database,
monkeypatch: pytest.MonkeyPatch,
):
database_url = ready_poo_database["poo_url"]
engine = create_engine(database_url, connect_args={"check_same_thread": False})
session_local = sessionmaker(bind=engine, autoflush=False, autocommit=False)
import app.poo_db as poo_db
monkeypatch.setattr(poo_db, "poo_engine", engine)
monkeypatch.setattr(poo_db, "PooSessionLocal", session_local)
fastapi_app = create_app()
with TestClient(fastapi_app) as client:
yield client, engine
engine.dispose()