Files
romm/backend/tests/test_database_fixtures.py
Zach Clendenen 7d3e3ec1ab perf(tests): faster database setup and cleanup in the backend suite (#5073)
* perf(tests): skip empty tables in clear_database and run migration tests first

clear_database issued 20 DELETEs after every test. One EXISTS query now finds which tables hold rows and only those are cleared. The slow migration tests are scheduled first so they overlap the rest of the suite under xdist.

* perf(tests): clone a migrated template into each MariaDB xdist worker

Replaying every migration in each worker at once was most of a cold run. The template database is migrated once under a server lock and copied into each empty worker database. PostgreSQL keeps running migrations per worker.

* fix(tests): address pr-ready code review

Key the MariaDB template on a digest of the migrations and the helpers
they import, so an edited revision or a template left at another
branch's head or half-migrated is rebuilt instead of reused or failing.
Copy alembic_version last and reset a worker database that has tables
but no version row, so a clone cut short is redone on the next run.

* refactor(tests): pr-ready simplify pass

* style(tests): pr-ready polish

* style(tests): pr-ready humanizer pass

* test(tests): cover the template copy and empty-table cleanup

Addresses the Greptile finding that the new database setup paths had no
assertions:
- test_copy_schema_reproduces_the_migrated_database copies the migrated
  worker database with _copy_schema and checks tables, views, triggers,
  row checksums and the alembic version match the source (MariaDB only).
- test_clear_tables_deletes_only_from_tables_with_rows checks that cleanup
  issues a DELETE only for tables holding rows. The fixture body moves into
  _clear_tables so the test can call it.

* fix(tests): address pr-ready code review (xhigh)

Hash all of models/ and utils/ into the template digest, warn when the
clone falls back to migrating, defer the digest past the early returns,
and dedupe the DEFINER regex, statement capture and schema-name queries.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(tests): pr-ready simplify pass

Read tables and views in one query, strip DEFINER once, use the
inspector for the version check, always recreate an unversioned worker
DB, and pull the server lock and template build into helpers.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* style(tests): pr-ready polish

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* test(db): clear every model table between tests

Derive the cleared tables from BaseModel.metadata.sorted_tables, children
first, instead of a hand-kept list of 20 models. Views and the
permission tables the migrations seed are skipped. A new model is now
cleared without editing the fixture.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* perf(tests): build the MariaDB template once in the xdist controller

pytest_configure_node checks each worker's database on the controller
and, for one with no version row, builds the template (once per session)
and hands its name over through workerinput. Workers copy it without
taking the lock, so copies run in parallel, and a broken migration
fails once instead of once per waiting worker. The template is shared
by every checkout and ROMM_TEST_DB_TAG run, since its digest pins the
schema. DEVELOPER_SETUP.md documents it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 12:52:09 -04:00

161 lines
5.3 KiB
Python

import re
from collections.abc import Iterator
from types import SimpleNamespace
import pytest
from sqlalchemy import Connection, create_engine, text
from tests.conftest import (
_DEFINER_RE,
_TEMPLATE_INPUT,
_alembic_version,
_capture_statements,
_clear_tables,
_cleared_tables,
_copy_schema,
_recreate_database,
_schema_names,
engine,
pytest_configure_node,
session,
)
from config import ROMM_DB_DRIVER
from models.collection import Collection, VirtualCollection
from models.permission import PermissionGroup, PermissionGroupGrant
from models.platform import Platform
from models.rom import Rom, RomMetadata, SiblingRom
_AUTO_INCREMENT_RE = re.compile(r" AUTO_INCREMENT=\d+")
def _snapshot(conn: Connection, schema: str) -> dict[str, object]:
"""Every table, view and trigger definition of `schema`, plus each table's rows."""
objects = conn.execute(
text(
"SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES"
" WHERE TABLE_SCHEMA = :s ORDER BY TABLE_NAME"
),
{"s": schema},
).all()
triggers = _schema_names(
conn,
"SELECT TRIGGER_NAME FROM information_schema.TRIGGERS"
" WHERE TRIGGER_SCHEMA = :s ORDER BY TRIGGER_NAME",
schema,
)
conn.exec_driver_sql(f"USE `{schema}`")
snapshot: dict[str, object] = {}
for name, kind in objects:
if kind == "VIEW":
ddl = conn.exec_driver_sql(f"SHOW CREATE VIEW `{name}`").one()[1]
snapshot[name] = _DEFINER_RE.sub("", ddl)
continue
ddl = conn.exec_driver_sql(f"SHOW CREATE TABLE `{name}`").one()[1]
rows = conn.exec_driver_sql(f"SELECT COUNT(*) FROM `{name}`").scalar()
checksum = conn.exec_driver_sql(f"CHECKSUM TABLE `{name}`").one()[1]
snapshot[name] = (_AUTO_INCREMENT_RE.sub("", ddl), rows, checksum)
for name in triggers:
ddl = conn.exec_driver_sql(f"SHOW CREATE TRIGGER `{name}`").one()[2]
snapshot[f"trigger:{name}"] = _DEFINER_RE.sub("", ddl)
return snapshot
@pytest.fixture
def scratch_connection() -> Iterator[Connection]:
# A private engine, since `_copy_schema` switches the connection's schema.
scratch_engine = create_engine(engine.url)
try:
with scratch_engine.connect() as conn:
yield conn
finally:
scratch_engine.dispose()
@pytest.mark.skipif(
ROMM_DB_DRIVER not in ("mariadb", "mysql"),
reason="the template clone only runs on MariaDB",
)
def test_copy_schema_reproduces_the_migrated_database(
scratch_connection: Connection, platform: Platform
) -> None:
source = engine.url.database
assert source
target = f"{source}_copy"
_recreate_database(scratch_connection, target)
try:
_copy_schema(scratch_connection, source, target)
assert _alembic_version(scratch_connection, target) == _alembic_version(
scratch_connection, source
)
assert _snapshot(scratch_connection, target) == _snapshot(
scratch_connection, source
)
finally:
scratch_connection.exec_driver_sql(f"DROP DATABASE IF EXISTS `{target}`")
@pytest.mark.skipif(
ROMM_DB_DRIVER not in ("mariadb", "mysql"),
reason="the template clone only runs on MariaDB",
)
def test_configure_node_hands_the_template_only_to_unmigrated_workers(
scratch_connection: Connection,
) -> None:
source = engine.url.database
assert source
worker_db = f"{source}_probe"
_recreate_database(scratch_connection, worker_db)
try:
fresh = SimpleNamespace(workerinput={"workerid": "probe"})
pytest_configure_node(fresh)
template = fresh.workerinput[_TEMPLATE_INPUT]
assert _alembic_version(scratch_connection, template) == _alembic_version(
scratch_connection, source
)
_copy_schema(scratch_connection, template, worker_db)
migrated = SimpleNamespace(workerinput={"workerid": "probe"})
pytest_configure_node(migrated)
assert _TEMPLATE_INPUT not in migrated.workerinput
finally:
scratch_connection.exec_driver_sql(f"DROP DATABASE IF EXISTS `{worker_db}`")
def test_cleared_tables_skip_views_and_seeded_tables() -> None:
tables, _ = _cleared_tables()
names = [table.name for table in tables]
assert Collection.__tablename__ in names
assert names.index(Rom.__tablename__) < names.index(Platform.__tablename__)
assert not set(names) & {
RomMetadata.__tablename__,
SiblingRom.__tablename__,
VirtualCollection.__tablename__,
PermissionGroup.__tablename__,
PermissionGroupGrant.__tablename__,
}
def test_clear_tables_keeps_seeded_permission_groups(platform: Platform) -> None:
with session() as s:
groups = s.query(PermissionGroup).count()
assert groups
_clear_tables()
with session() as s:
assert s.query(PermissionGroup).count() == groups
def test_clear_tables_deletes_only_from_tables_with_rows(platform: Platform) -> None:
with _capture_statements(engine) as statements:
_clear_tables()
_clear_tables()
deletes = [s for s in statements if s.lstrip().upper().startswith("DELETE")]
assert len(deletes) == 1
assert Platform.__tablename__ in deletes[0]
with session() as s:
assert s.query(Platform).count() == 0