Give the tests their own database and drop the template specs
pytest was deleting every user on teardown against the development database. The engine is built at import time, so tests/__init__.py pins POSTGRES_DB before app.core.config loads; the fixture creates the schema and drops the database again, guarded against a name that is not _test. Playwright now defaults at the integrated stack, which is the origin the API allows, so a bare `bunx playwright test` works without a dev server. The four template specs that asserted copy we no longer ship are gone, along with the helpers they were the last callers of. The admin edit assertion was page-wide and only ever passed on a fresh database. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KkmeRiyeYmVZqJVwuyHq9o
This commit is contained in:
co-authored by
Claude Opus 5
parent
aa0e760615
commit
82356810ce
@@ -0,0 +1,11 @@
|
||||
"""Redirect the suite at its own database.
|
||||
|
||||
`app.core.db` builds the engine at import time and several modules bind that
|
||||
object, so the name has to be in the environment before anything imports the
|
||||
settings. Overriding it here — the first module pytest imports for the
|
||||
package — keeps a test run from touching the development data.
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
os.environ["POSTGRES_DB"] = "app_test"
|
||||
|
||||
@@ -2,12 +2,13 @@ from collections.abc import Generator
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlmodel import Session, delete
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.engine import make_url
|
||||
from sqlmodel import Session, SQLModel
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.db import engine, init_db
|
||||
from app.main import app
|
||||
from app.models import User
|
||||
from tests.utils.user import authentication_token_from_email
|
||||
from tests.utils.utils import get_superuser_token_headers
|
||||
|
||||
@@ -23,12 +24,29 @@ def flow_data(tmp_path_factory: pytest.TempPathFactory) -> Generator[None, None,
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
def db() -> Generator[Session, None, None]:
|
||||
"""Create the throwaway database `tests/__init__.py` points at, drop it after."""
|
||||
url = make_url(str(settings.SQLALCHEMY_DATABASE_URI))
|
||||
# The teardown drops this database, so refuse to run against anything but
|
||||
# the dedicated test one.
|
||||
assert url.database and url.database.endswith("_test"), url.database
|
||||
|
||||
maintenance = create_engine(
|
||||
url.set(database="postgres"), isolation_level="AUTOCOMMIT"
|
||||
)
|
||||
drop = text(f'DROP DATABASE IF EXISTS "{url.database}" WITH (FORCE)')
|
||||
with maintenance.connect() as connection:
|
||||
connection.execute(drop)
|
||||
connection.execute(text(f'CREATE DATABASE "{url.database}"'))
|
||||
|
||||
SQLModel.metadata.create_all(engine)
|
||||
with Session(engine) as session:
|
||||
init_db(session)
|
||||
yield session
|
||||
statement = delete(User)
|
||||
session.execute(statement)
|
||||
session.commit()
|
||||
|
||||
engine.dispose()
|
||||
with maintenance.connect() as connection:
|
||||
connection.execute(drop)
|
||||
maintenance.dispose()
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
Reference in New Issue
Block a user