Files
animaltrack/tests/conftest.py
Petru Paler cc32370d49 perf: speed up test suite with parallel execution
- Enable pytest-xdist with -n auto for parallel test execution
- Consolidate migrated_db fixture in conftest.py (was duplicated in 4 files)
- Remove local fixture definitions and unused imports from test files

Test execution: ~24s -> ~5s (~5x speedup)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-28 07:25:18 +00:00

62 lines
1.7 KiB
Python

# ABOUTME: Pytest configuration and fixtures for AnimalTrack tests.
# ABOUTME: Provides database fixtures, test clients, and common utilities.
import os
import tempfile
import pytest
from animaltrack.db import get_db
from animaltrack.migrations import run_migrations
@pytest.fixture
def migrated_db(tmp_path):
"""Create a database with migrations applied."""
db_path = str(tmp_path / "test.db")
run_migrations(db_path, "migrations", verbose=False)
return get_db(db_path)
@pytest.fixture
def temp_migrations_dir(tmp_path):
"""Create a temporary migrations directory for testing."""
migrations_path = tmp_path / "migrations"
migrations_path.mkdir()
return migrations_path
@pytest.fixture
def fresh_db_path(tmp_path):
"""Provide a path for a non-existent database file.
Unlike temp_db_path, this does not create the file - it just provides
a path. This is needed for fastmigrate which expects to create the db.
"""
db_path = tmp_path / "test.db"
yield db_path
# Cleanup
if db_path.exists():
db_path.unlink()
# Also clean up WAL files if they exist
for suffix in ["-shm", "-wal", "-journal"]:
wal_path = db_path.with_suffix(db_path.suffix + suffix)
if wal_path.exists():
wal_path.unlink()
@pytest.fixture
def temp_db_path():
"""Create a temporary database file path for testing."""
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
db_path = f.name
yield db_path
# Cleanup
if os.path.exists(db_path):
os.unlink(db_path)
# Also clean up WAL files if they exist
for suffix in ["-shm", "-wal", "-journal"]:
wal_path = db_path + suffix
if os.path.exists(wal_path):
os.unlink(wal_path)