- PR1: Project scaffold, DB models, price loader - PR2: Congressional trade ingestion (House Stock Watcher) - PR3: Security enrichment + deployment infrastructure - 37 passing tests, 87%+ coverage - Docker + Proxmox deployment ready - Complete documentation - Works 100% offline with fixtures
106 lines
2.7 KiB
Python
106 lines
2.7 KiB
Python
"""
|
|
Pytest fixtures and test configuration.
|
|
"""
|
|
|
|
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from pote.db import Base
|
|
from pote.db.models import Official, Price, Security, Trade
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def test_db_session() -> Session:
|
|
"""
|
|
Create an in-memory SQLite database for testing.
|
|
Each test gets a fresh database.
|
|
"""
|
|
engine = create_engine("sqlite:///:memory:", echo=False)
|
|
Base.metadata.create_all(engine)
|
|
|
|
TestSessionLocal = sessionmaker(bind=engine)
|
|
session = TestSessionLocal()
|
|
|
|
yield session
|
|
|
|
session.close()
|
|
engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_official(test_db_session: Session) -> Official:
|
|
"""Create a sample official for testing."""
|
|
official = Official(
|
|
name="Jane Doe",
|
|
chamber="Senate",
|
|
party="Independent",
|
|
state="CA",
|
|
bioguide_id="D000123",
|
|
)
|
|
test_db_session.add(official)
|
|
test_db_session.commit()
|
|
test_db_session.refresh(official)
|
|
return official
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_security(test_db_session: Session) -> Security:
|
|
"""Create a sample security for testing."""
|
|
security = Security(
|
|
ticker="AAPL",
|
|
name="Apple Inc.",
|
|
exchange="NASDAQ",
|
|
sector="Technology",
|
|
asset_type="stock",
|
|
)
|
|
test_db_session.add(security)
|
|
test_db_session.commit()
|
|
test_db_session.refresh(security)
|
|
return security
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_trade(
|
|
test_db_session: Session, sample_official: Official, sample_security: Security
|
|
) -> Trade:
|
|
"""Create a sample trade for testing."""
|
|
trade = Trade(
|
|
official_id=sample_official.id,
|
|
security_id=sample_security.id,
|
|
source="test",
|
|
external_id="test-001",
|
|
transaction_date=date(2024, 1, 15),
|
|
filing_date=date(2024, 2, 1),
|
|
side="buy",
|
|
value_min=Decimal("15000.00"),
|
|
value_max=Decimal("50000.00"),
|
|
currency="USD",
|
|
)
|
|
test_db_session.add(trade)
|
|
test_db_session.commit()
|
|
test_db_session.refresh(trade)
|
|
return trade
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_price(test_db_session: Session, sample_security: Security) -> Price:
|
|
"""Create a sample price record for testing."""
|
|
price = Price(
|
|
security_id=sample_security.id,
|
|
date=date(2024, 1, 15),
|
|
open=Decimal("180.50"),
|
|
high=Decimal("182.75"),
|
|
low=Decimal("179.00"),
|
|
close=Decimal("181.25"),
|
|
volume=50000000,
|
|
source="yfinance",
|
|
)
|
|
test_db_session.add(price)
|
|
test_db_session.commit()
|
|
test_db_session.refresh(price)
|
|
return price
|