47 lines
No EOL
1.3 KiB
Python
47 lines
No EOL
1.3 KiB
Python
"""Pytest fixtures: database setup/teardown for all tests.
|
|
|
|
Uses a real PostgreSQL on port 5433 (jobhunt-test-pg container).
|
|
Each test module gets a clean database via truncate fixtures.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
# Set DATABASE_URL before importing app modules
|
|
os.environ.setdefault(
|
|
"DATABASE_URL",
|
|
"postgresql://jobhunt:jobhunt@localhost:5433/jobhunt",
|
|
)
|
|
|
|
from app.db import close_pool, get_pool # noqa: E402
|
|
from app.db import migrate as migrate_mod # noqa: E402
|
|
from app.db import repo_app, repo_profile # noqa: E402
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def _setup_database():
|
|
"""Run migrations once at session start."""
|
|
migrate_mod.reset_database()
|
|
yield
|
|
close_pool()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _truncate_tables():
|
|
"""Truncate all tables before each test (except schema_migrations)."""
|
|
import psycopg
|
|
from app.config import DATABASE_URL
|
|
|
|
with psycopg.connect(DATABASE_URL) as conn:
|
|
conn.execute(
|
|
"""
|
|
TRUNCATE TABLE notification_log, email_suggestion, task_run, outbox, approval, artifact,
|
|
application, job_posting, cv_section, profile
|
|
RESTART IDENTITY CASCADE
|
|
"""
|
|
)
|
|
conn.commit()
|
|
yield |