3d3c2bcc57
Collapse the three data layers into one. app/db.py now exposes a single Base, a cached engine bound to app_database_url with SQLite WAL enabled, and get_engine/get_session_local/reset_db_caches/get_db_session. Delete app/auth_db.py, app/poo_db.py and app/models/base.py. All models (auth, config, public_ip, location, poo) inherit the one Base and register on a single metadata. Dependencies converge to a single get_db; all routes use it. Also update the alembic env.py files (app/location/poo) and tests that imported the removed modules so the suite stays green, and drop the obsolete test_legacy_style_location_db test whose flow (app reading a separate location DB) no longer exists. Location/poo Alembic chains, adopt scripts and adoption tests remain for M1-T04; config fields remain for M1-T05. pytest 109 passed; ruff clean (pre-existing only); WAL verified; single Base.metadata holds all seven tables.
115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from alembic import command
|
|
from alembic.config import Config
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
|
|
from app.db import reset_db_caches
|
|
from app.config import get_settings
|
|
from app.main import create_app
|
|
|
|
|
|
def _make_app_alembic_config(database_url: str) -> Config:
|
|
config = Config("alembic_app.ini")
|
|
config.set_main_option("sqlalchemy.url", database_url)
|
|
return config
|
|
|
|
|
|
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")
|
|
config.set_main_option("sqlalchemy.url", database_url)
|
|
return config
|
|
|
|
|
|
@pytest.fixture
|
|
def test_database_urls(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
|
|
app_database_path = tmp_path / "app_test.db"
|
|
location_database_path = tmp_path / "location_test.db"
|
|
poo_database_path = tmp_path / "poo_placeholder.db"
|
|
app_database_url = f"sqlite:///{app_database_path}"
|
|
location_database_url = f"sqlite:///{location_database_path}"
|
|
poo_database_url = f"sqlite:///{poo_database_path}"
|
|
|
|
monkeypatch.setenv("APP_DATABASE_URL", app_database_url)
|
|
monkeypatch.setenv("LOCATION_DATABASE_URL", location_database_url)
|
|
monkeypatch.setenv("POO_DATABASE_URL", poo_database_url)
|
|
monkeypatch.setenv("AUTH_BOOTSTRAP_USERNAME", "admin")
|
|
monkeypatch.setenv("AUTH_BOOTSTRAP_PASSWORD", "test-password")
|
|
monkeypatch.setenv("AUTH_COOKIE_SECURE_OVERRIDE", "false")
|
|
get_settings.cache_clear()
|
|
reset_db_caches()
|
|
|
|
try:
|
|
yield {
|
|
"app_path": app_database_path,
|
|
"app_url": app_database_url,
|
|
"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()
|
|
reset_db_caches()
|
|
|
|
|
|
@pytest.fixture
|
|
def ready_location_database(test_database_urls):
|
|
command.upgrade(_make_alembic_config(test_database_urls["location_url"]), "head")
|
|
return test_database_urls
|
|
|
|
|
|
@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
|
|
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_db_caches()
|
|
|
|
yield test_database_urls
|
|
reset_db_caches()
|
|
|
|
|
|
@pytest.fixture
|
|
def app(ready_location_database, ready_poo_database, auth_database):
|
|
yield create_app()
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
|
|
@pytest.fixture
|
|
def location_client(ready_location_database, ready_poo_database, auth_database):
|
|
app_url = auth_database["app_url"]
|
|
engine = create_engine(app_url, connect_args={"check_same_thread": False})
|
|
fastapi_app = create_app()
|
|
with TestClient(fastapi_app) as client:
|
|
yield client, engine
|
|
engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
def poo_client(ready_location_database, ready_poo_database, auth_database):
|
|
app_url = auth_database["app_url"]
|
|
engine = create_engine(app_url, connect_args={"check_same_thread": False})
|
|
fastapi_app = create_app()
|
|
with TestClient(fastapi_app) as client:
|
|
yield client, engine
|
|
engine.dispose()
|