Files
nuzlocke-tracker/.beans/nuzlocke-tracker-rrcf--set-up-backend-test-infrastructure.md
Julian Tabel b0ac3714a9 Set up backend test infrastructure
Add pytest fixtures (engine, db_session, client) with session-scoped
event loop to avoid asyncpg loop mismatch errors. Smoke tests verify
all three main API endpoints return empty results on a clean DB.
Test DB provided by docker-compose.test.yml on port 5433.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 12:35:22 +01:00

1.8 KiB

title, status, type, priority, created_at, updated_at, parent, blocking
title status type priority created_at updated_at parent blocking
Set up backend test infrastructure completed task normal 2026-02-10T09:32:57Z 2026-02-21T11:33:32Z nuzlocke-tracker-yzpb
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

  • Add asyncio_default_fixture_loop_scope = "session" and asyncio_default_test_loop_scope = "session" to pyproject.toml
  • Create backend/tests/conftest.py with engine, db_session, and client fixtures
  • Write a smoke test in backend/tests/test_smoke.py to verify the setup
  • Verify all tests pass (pytest from backend dir)