Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

perf(tests): build the test schema once and empty it between tests

Petr Bauch5aae5cac1f1b
diff --git a/core/tests/conftest.py b/core/tests/conftest.py
index fe471b7aa..94330c41a 100644
--- a/core/tests/conftest.py
+++ b/core/tests/conftest.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
import contextlib
import uuid
from collections.abc import AsyncIterator, Iterator
@@ -41,14 +42,47 @@ async def _seed_tenant_zero(conn: AsyncConnection) -> None:
)
+# Every table, emptied in one statement. `CASCADE` is what lets a single
+# TRUNCATE cover tables that reference each other, and `RESTART IDENTITY`
+# keeps sequence-backed columns (`messages.seq`) starting where a test that
+# hard-codes an expected value needs them to.
+_TRUNCATE_ALL = (
+ "TRUNCATE "
+ + ", ".join(f'"{table.name}"' for table in Base.metadata.sorted_tables)
+ + " RESTART IDENTITY CASCADE"
+)
+
+
+async def empty_the_database(engine: AsyncEngine) -> None:
+ """Leave the schema in place and take every row out of it, tenant zero apart.
+
+ The state a test wants from `create_all` is an empty database, which
+ building the schema from nothing is only one way to reach. Emptying it
+ instead is the same starting point for a fraction of the DDL, and it is
+ what every per-test fixture here uses.
+ """
+ async with engine.begin() as conn:
+ await conn.execute(text(_TRUNCATE_ALL))
+ await _seed_tenant_zero(conn)
+
+
@pytest.fixture(scope="session")
def postgres_url() -> Iterator[str]:
"""A throwaway PostgreSQL instance for the whole test session.
Store tests run against real Postgres (not SQLite/mocks) so behaviours like
`ON DELETE SET NULL` and check constraints are exercised for real.
+
+ Durability is off. The container is deleted when the session ends, so a
+ crash has nothing to recover to and every fsync it would perform is work
+ thrown away — which matters here because the per-test reset is almost
+ entirely write traffic. Nothing about the behaviour under test changes:
+ these settings govern what survives a crash, not what a transaction sees.
"""
- with PostgresContainer("postgres:16-alpine") as pg:
+ container = PostgresContainer("postgres:16-alpine").with_command(
+ "postgres -c fsync=off -c synchronous_commit=off -c full_page_writes=off"
+ )
+ with container as pg:
host = pg.get_container_host_ip()
port = pg.get_exposed_port(5432)
yield (
@@ -57,11 +91,43 @@ def postgres_url() -> Iterator[str]:
)
[email protected](scope="session")
+def postgres_schema(postgres_url: str) -> str:
+ """The schema, built once for the whole session.
+
+ Returns `postgres_url`: it is the same database, and asking for this
+ fixture rather than that one is how a test says it wants the tables there.
+ The tests that want a schema of their own — migration parity, the backfill
+ migrations, the frozen-DDL comparison — create a separate database off
+ `postgres_url` and are untouched by this.
+
+ Synchronous, with an event loop of its own, because it has to outlive the
+ per-test loop pytest-asyncio opens: the schema belongs to the container,
+ not to any one test's loop, and the engine that builds it is disposed
+ before the first test runs.
+ """
+
+ async def build() -> None:
+ engine = create_async_engine(postgres_url)
+ try:
+ async with engine.begin() as conn:
+ await conn.run_sync(Base.metadata.create_all)
+ finally:
+ await engine.dispose()
+
+ asyncio.run(build())
+ return postgres_url
+
+
@pytest_asyncio.fixture
async def session_factory(
- postgres_url: str, request: pytest.FixtureRequest
+ postgres_schema: str, request: pytest.FixtureRequest
) -> AsyncIterator[async_sessionmaker[AsyncSession]]:
- """Fresh schema per test: create all tables, yield a session factory, drop.
+ """An empty database per test, on a schema built once for the session.
+
+ Emptied on the way in rather than the way out, so a test that fails leaves
+ its rows in the container for a `psql` to look at, and so the guarantee
+ holds for the first test as much as for the rest.
Also binds tenant zero for the duration of the test, unless the test is
marked `no_ambient_tenant`. In production a tenant is always bound before
@@ -82,10 +148,8 @@ async def session_factory(
overrides this default for that block — only the "nothing bound at all,
including before I do anything" assertions need the marker.
"""
- engine = create_async_engine(postgres_url)
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.create_all)
- await _seed_tenant_zero(conn)
+ engine = create_async_engine(postgres_schema)
+ await empty_the_database(engine)
ambient_tenant = (
contextlib.nullcontext()
if request.node.get_closest_marker("no_ambient_tenant")
@@ -98,8 +162,6 @@ async def session_factory(
# suite — differs from production in as little as possible.
yield create_session_factory(engine)
finally:
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.drop_all)
await engine.dispose()
@@ -136,7 +198,7 @@ class RLSHarness:
@pytest_asyncio.fixture
-async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
+async def rls_harness(postgres_schema: str) -> AsyncIterator[RLSHarness]:
"""A session factory connected as a role row-level security applies to.
`session_factory` connects as the container's superuser — the same shape
@@ -146,9 +208,9 @@ async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
`docs/old/multi-tenancy-phase1-db.md`'s "Done when" is unreachable without
something else.
- This is that something else. Per test, it builds the schema (so
- `create_all` attaches the same policies a migration would, exactly as
- `session_factory` does), seeds tenant zero, and creates a throwaway login
+ This is that something else. Per test, it empties the session's schema
+ (built by `create_all`, so it carries the same policies a migration would
+ attach, exactly as `session_factory` sees), and creates a throwaway login
role that is not the owner (so it doesn't inherit the owner's blanket
exemption from its own tables' policies), not a superuser, and carries no
`BYPASSRLS`.
@@ -170,13 +232,12 @@ async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
`db/runtime_role.verify_restricted_role` rather than here, because CI has
no production connection to ask it of.
"""
- owner_engine = create_async_engine(postgres_url)
+ owner_engine = create_async_engine(postgres_schema)
role = f"switch_rls_test_{uuid.uuid4().hex[:12]}"
password = uuid.uuid4().hex
+ await empty_the_database(owner_engine)
async with owner_engine.begin() as conn:
- await conn.run_sync(Base.metadata.create_all)
- await _seed_tenant_zero(conn)
# CREATE ROLE does not accept a bind parameter for PASSWORD (it is not
# a normal DML statement), so the password is interpolated directly.
# It is a fresh, random, throwaway value scoped to this one test.
@@ -188,7 +249,7 @@ async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
)
await grant_runtime_role(conn, role)
- restricted_url = make_url(postgres_url).set(username=role, password=password)
+ restricted_url = make_url(postgres_schema).set(username=role, password=password)
restricted_engine = create_async_engine(restricted_url)
try:
yield RLSHarness(
@@ -200,20 +261,19 @@ async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
finally:
await restricted_engine.dispose()
async with owner_engine.begin() as conn:
- await conn.run_sync(Base.metadata.drop_all)
- async with owner_engine.begin() as conn:
- # Dropping the tables removes their own grants with them, but the
- # schema-level `GRANT USAGE` survives — `DROP OWNED BY` revokes
- # every privilege still granted to the role (it owns no objects,
- # so there is nothing for it to drop), which is what lets `DROP
- # ROLE` succeed right after.
+ # The tables outlive the test, so their grants to this role would
+ # too. `DROP OWNED BY` revokes every privilege still held by the
+ # role — the table grants and the schema-level `GRANT USAGE` alike
+ # (it owns no objects, so there is nothing for it to drop) — which
+ # is both what leaves the shared schema as it was found and what
+ # lets `DROP ROLE` succeed right after.
await conn.execute(text(f'DROP OWNED BY "{role}"'))
await conn.execute(text(f'DROP ROLE "{role}"'))
# `grant_runtime_role` also revokes PUBLIC's default EXECUTE on
# functions, and that one is recorded against the *owner* rather
# than against the throwaway role — so `DROP OWNED BY` above does
- # not touch it and it outlives this test, governing every schema
- # every later test in the session builds. Left in place it makes
+ # not touch it and it outlives this test, governing every function
+ # every later test in the session creates. Left in place it makes
# the checks about it pass for the wrong reason: the second test
# to ask whether a new function is PUBLIC-executable would be
# answered by the first test's leftover rather than by the grants
@@ -222,4 +282,33 @@ async def rls_harness(postgres_url: str) -> AsyncIterator[RLSHarness]:
await conn.execute(
text("ALTER DEFAULT PRIVILEGES GRANT EXECUTE ON FUNCTIONS TO PUBLIC")
)
+ # The same argument, for the schema itself rather than the default
+ # privileges on it. The tests this fixture exists for break the
+ # schema on purpose — forcing row level security, granting EXECUTE
+ # back to PUBLIC — to watch the boot check refuse it, and the
+ # schema now outlives them. Put it back the way `create_all`
+ # leaves it: nothing forced, and PUBLIC holding the EXECUTE
+ # Postgres grants on a new function. `grant_runtime_role` above
+ # revokes that again for every test, so what the assertions run
+ # against is unchanged.
+ forced = (
+ (
+ await conn.execute(
+ text(
+ "SELECT relname FROM pg_class c "
+ "JOIN pg_namespace n ON n.oid = c.relnamespace "
+ "WHERE c.relforcerowsecurity AND n.nspname = 'public'"
+ )
+ )
+ )
+ .scalars()
+ .all()
+ )
+ for table in forced:
+ await conn.execute(
+ text(f'ALTER TABLE "{table}" NO FORCE ROW LEVEL SECURITY')
+ )
+ await conn.execute(
+ text("GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO PUBLIC")
+ )
await owner_engine.dispose()
diff --git a/core/tests/switch_core/db/test_tenant_session_hook.py b/core/tests/switch_core/db/test_tenant_session_hook.py
index ae0c7ffab..1df1246cb 100644
--- a/core/tests/switch_core/db/test_tenant_session_hook.py
+++ b/core/tests/switch_core/db/test_tenant_session_hook.py
@@ -23,10 +23,10 @@ from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_asyn
from sqlalchemy.orm import Session
from switch_core.db import tenant_session
-from switch_core.db.base import Base
from switch_core.db.engine import create_session_factory
from switch_core.db.models import Client, Tenant, User
from switch_core.tenant_context import tenant_scope
+from tests.conftest import empty_the_database
TENANT_A = "tenant-hook-a"
TENANT_B = "tenant-hook-b"
@@ -34,19 +34,16 @@ TENANT_B = "tenant-hook-b"
@pytest_asyncio.fixture
async def single_connection_session_factory(
- postgres_url: str,
+ postgres_schema: str,
) -> AsyncIterator[async_sessionmaker[AsyncSession]]:
- engine = create_async_engine(postgres_url, pool_size=1, max_overflow=0)
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.create_all)
+ engine = create_async_engine(postgres_schema, pool_size=1, max_overflow=0)
+ await empty_the_database(engine)
try:
# Goes through the production factory constructor, same as the shared
# fixture. The hook itself is registered on import, not by this call —
# `TestTheHookIsRegisteredByImportAlone` is what pins that.
yield create_session_factory(engine)
finally:
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.drop_all)
await engine.dispose()
diff --git a/core/tests/switch_core/gateway/test_tenant_resolution.py b/core/tests/switch_core/gateway/test_tenant_resolution.py
index 6b3bfc05b..11dd9728d 100644
--- a/core/tests/switch_core/gateway/test_tenant_resolution.py
+++ b/core/tests/switch_core/gateway/test_tenant_resolution.py
@@ -45,10 +45,9 @@ import httpx
import pytest
import pytest_asyncio
from fastapi import Depends, FastAPI, Response
-from sqlalchemy import insert, text
+from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
-from switch_core.db.base import Base
from switch_core.db.engine import create_session_factory
from switch_core.db.models import TENANT_ZERO_ID, Tenant, TenantMember, User
from switch_core.db.stores.user_store import UserStore
@@ -66,6 +65,7 @@ from switch_core.gateway.auth import (
from switch_core.gateway.auth_routes import login
from switch_core.gateway.schemas import LoginRequest
from switch_core.gateway.tenants import router as tenants_router
+from tests.conftest import empty_the_database
_SECRET = "unit-test-jwt-key-unit-test-jwt-key-unit-test" # gitleaks:allow
TENANT_B = "tenant-resolution-b"
@@ -540,7 +540,7 @@ class TestSwitchTenantEndpoint:
@pytest_asyncio.fixture
async def one_connection_session_factory(
- postgres_url: str,
+ postgres_schema: str,
) -> AsyncIterator[async_sessionmaker[AsyncSession]]:
"""A pool of exactly one connection, and a short timeout for waiting on it.
@@ -550,20 +550,12 @@ async def one_connection_session_factory(
production.
"""
engine = create_async_engine(
- postgres_url, pool_size=1, max_overflow=0, pool_timeout=5
+ postgres_schema, pool_size=1, max_overflow=0, pool_timeout=5
)
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.create_all)
- await conn.execute(
- insert(Tenant.__table__).values(
- id=TENANT_ZERO_ID, slug="default", name="Default"
- )
- )
+ await empty_the_database(engine)
try:
yield create_session_factory(engine)
finally:
- async with engine.begin() as conn:
- await conn.run_sync(Base.metadata.drop_all)
await engine.dispose()
diff --git a/core/tests/test_conftest_tenant_binding.py b/core/tests/test_conftest_tenant_binding.py
index 664796522..a938a79e9 100644
--- a/core/tests/test_conftest_tenant_binding.py
+++ b/core/tests/test_conftest_tenant_binding.py
@@ -43,9 +43,11 @@ class _NoMarkerRequest:
async def test_survives_being_finalised_from_another_context(
- postgres_url: str,
+ postgres_schema: str,
) -> None:
- agen: AsyncGenerator[Any, None] = _session_factory(postgres_url, _NoMarkerRequest())
+ agen: AsyncGenerator[Any, None] = _session_factory(
+ postgres_schema, _NoMarkerRequest()
+ )
loop = asyncio.get_running_loop()
# Advance to the fixture's `yield` inside its own, isolated context -- the
# same isolation a pytest-asyncio task gives it going in.