programme-pulse-chat/tests/conftest.py
DJP b70d148b94 Productionise Programme Pulse
Backend
- Routes moved under /api/, JWT bearer auth via @before_request
- DEV_AUTH_BYPASS escape hatch for local dev
- In-memory chat history and report state replaced with Postgres tables
  (preferences, chat_messages, reports, feedback_events) keyed on user
- SQLAlchemy 2.x + Alembic migrations run on container start
- Graceful Airtable failure handling — bad creds no longer 500 the API
- Per-user data isolation via g.user_email from validated token

Frontend
- React + Vite + TypeScript SPA at /programme-pulse/
- MSAL.js (PKCE, sessionStorage, ID token to backend)
- VITE_DEV_AUTH_BYPASS mirrors backend bypass for local dev
- Streaming chat via fetch ReadableStream + SSE parsing
- Charts via chart.js, markdown via react-markdown + remark-gfm
- Full UI parity with the original templates/index.html

Deploy (optical-dev split-build pattern)
- Dockerfile + docker-compose.yml (name: programme-pulse pinned;
  app + Postgres; 127.0.0.1 binding only)
- deploy/apache-programme-pulse.conf.tmpl with flushpackets=on for SSE
- deploy/deploy.sh mirrors OSOP — port auto-pick (5051..5099),
  apache conf render, frontend build in throwaway node container,
  rsync to /var/www/html/programme-pulse, /api/health poll

Tests
- 49 passing; new tests for DB-backed preferences and JWT auth helpers
- SQLite-backed test fixture in tests/conftest.py

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 11:08:28 -04:00

33 lines
998 B
Python

"""Shared pytest fixtures.
Provides a SQLite-backed test database. We point DATABASE_URL at a temp file
before any `src.db` import so the engine binds to the right URL.
"""
from __future__ import annotations
import os
import pytest
@pytest.fixture(scope="session", autouse=True)
def _set_database_url(tmp_path_factory):
"""Set DATABASE_URL → SQLite temp file for the whole test session."""
db_path = tmp_path_factory.mktemp("db") / "test.sqlite"
os.environ["DATABASE_URL"] = f"sqlite+pysqlite:///{db_path}"
yield
@pytest.fixture(autouse=True)
def _reset_db():
"""Create a fresh schema for each test."""
# Import here so DATABASE_URL has been set first.
from src import db as db_module
# Force a fresh engine in case a previous test mutated module state.
db_module._engine = None
db_module._SessionLocal = None
engine = db_module.get_engine()
db_module.Base.metadata.drop_all(engine)
db_module.Base.metadata.create_all(engine)
yield