paper-dynasty-gameplay-webapp/tests/conftest.py
Cal Corum 1c24161e76 CLAUDE: Achieve 100% test pass rate with comprehensive AI service testing
- Fix TypeError in check_steal_opportunity by properly mocking catcher defense
- Correct tag_from_third test calculation to account for all adjustment conditions
- Fix pitcher replacement test by setting appropriate allowed runners threshold
- Add comprehensive test coverage for AI service business logic
- Implement VS Code testing panel configuration with pytest integration
- Create pytest.ini for consistent test execution and warning management
- Add test isolation guidelines and factory pattern implementation
- Establish 102 passing tests with zero failures

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-28 17:55:34 -05:00

78 lines
2.2 KiB
Python

"""
Shared pytest fixtures for Paper Dynasty web app testing.
Provides database sessions, test data factories, and common testing utilities
following the transaction rollback pattern for test isolation.
"""
import pytest
from sqlmodel import Session, SQLModel, create_engine
from uuid import uuid4
# Test Database Configuration
TEST_DATABASE_URL = "postgresql://paper_dynasty_user:paper_dynasty_test_password@localhost:5434/paper_dynasty_test"
@pytest.fixture(scope="session")
def test_engine():
"""Create test database engine for the entire test session."""
engine = create_engine(TEST_DATABASE_URL, echo=False)
# Create all tables
SQLModel.metadata.create_all(engine)
yield engine
# Optional: Drop all tables after test session
# SQLModel.metadata.drop_all(engine)
@pytest.fixture
def db_session(test_engine):
"""
Create database session with transaction rollback for test isolation.
This is the primary fixture for database tests. Each test runs in a
transaction that is rolled back after the test completes, ensuring
complete isolation between tests.
"""
# Create a connection and start a transaction
connection = test_engine.connect()
transaction = connection.begin()
# Create session bound to the connection
session = Session(bind=connection)
try:
yield session
finally:
# Close session and rollback transaction
session.close()
transaction.rollback()
connection.close()
@pytest.fixture
def fresh_db_session(test_engine):
"""
Create database session for integration tests that need real commits.
Use this fixture for tests that specifically need to test commit behavior
or cross-transaction functionality. Use sparingly as these tests are slower
and require manual cleanup.
"""
with Session(test_engine) as session:
yield session
# Manual cleanup would go here if needed
def generate_unique_id():
"""Generate unique integer ID for test data."""
# Use last 8 digits of uuid4 as integer to avoid conflicts
return int(str(uuid4()).replace('-', '')[-8:], 16)
def generate_unique_name(prefix="Test"):
"""Generate unique name for test data."""
return f"{prefix} {uuid4().hex[:8]}"