Files
nuzlocke-tracker/.beans/nuzlocke-tracker-rrcf--set-up-backend-test-infrastructure.md

36 lines
1.8 KiB
Markdown
Raw Normal View History

---
# nuzlocke-tracker-rrcf
title: Set up backend test infrastructure
status: completed
type: task
priority: normal
created_at: 2026-02-10T09:32:57Z
updated_at: 2026-02-21T11:33:32Z
parent: nuzlocke-tracker-yzpb
blocking:
- nuzlocke-tracker-hjkk
- nuzlocke-tracker-iam7
- nuzlocke-tracker-ch77
- nuzlocke-tracker-ugb7
- nuzlocke-tracker-0arz
- nuzlocke-tracker-9c66
---
Set up the foundational test infrastructure for the FastAPI backend so that all subsequent test tasks can build on it.
## Approach
- Session-scoped async engine: creates all tables once via `Base.metadata.create_all()`, drops them after all tests finish
- Function-scoped `db_session` fixture: provides a fresh `AsyncSession`, overrides the `get_session` FastAPI dependency, and truncates all tables after each test for isolation
- Function-scoped `client` fixture: `httpx.AsyncClient` with `ASGITransport` — hits the real app stack including middleware and routing
- `asyncio_default_fixture_loop_scope = "session"` and `asyncio_default_test_loop_scope = "session"` added to pyproject.toml so all fixtures and tests share the same session event loop (required to avoid asyncpg "Future attached to different loop" errors)
- Test database URL read from `TEST_DATABASE_URL` env var (default: `postgresql+asyncpg://postgres:postgres@localhost:5433/nuzlocke_test`)
- The test DB is provided by `docker-compose.test.yml` (postgres on port 5433, `nuzlocke_test` DB created automatically)
## Checklist
- [x] Add `asyncio_default_fixture_loop_scope = "session"` and `asyncio_default_test_loop_scope = "session"` to `pyproject.toml`
- [x] Create `backend/tests/conftest.py` with `engine`, `db_session`, and `client` fixtures
- [x] Write a smoke test in `backend/tests/test_smoke.py` to verify the setup
- [x] Verify all tests pass (`pytest` from backend dir)