Test infrastructure for selective table loading
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 17h 7m
- Merged PRs (30d)
- 358
Description
## Overview
Create reusable test utilities that allow loading only required database tables per test file, instead of creating all tables at once.
## Deliverables
### 1. `with_tables` Context Manager
**File:** `tests/testutils/db.py`
```python
@asynccontextmanager
async def with_tables(engine: AsyncEngine, tables: list[Table]):
"""
Create tables on enter, TRUNCATE CASCADE on exit.
"""
async with engine.begin() as conn:
for table in tables:
await conn.run_sync(table.create, checkfirst=True)
yield
async with engine.begin() as conn:
table_names = ", ".join(t.name for t in tables)
await conn.execute(text(f"TRUNCATE {table_names} CASCADE"))
```
### 2. New Fixtures in `tests/conftest.py`
- `postgres_container` (session scope): PostgreSQL via testcontainer
- `database_connection` (session scope): AsyncEngine connection only, no table creation
### 3. Usage Pattern
```python
# tests/unit/manager/repositories/domain/conftest.py
REQUIRED_TABLES = [DomainRow.__table__, UserRow.__table__, ...]
@pytest.fixture(scope="module")
async def required_tables(database_connection):
async with with_tables(database_connection, REQUIRED_TABLES):
yield
```
## Acceptance Criteria
- [ ] `with_tables` creates only specified tables
- [ ] `with_tables` cleans up data via TRUNCATE CASCADE after tests
- [ ] `postgres_container` starts PostgreSQL 16 via testcontainer
- [ ] `database_connection` provides AsyncEngine without creating tables
- [ ] Existing `database_engine` fixture remains for backward compatibility
## Parent Epic
BA-3611
JIRA Issue: BA-3612
Contributor guide
Assessment
This issue has not been assessed yet.