feat(kevin): SA models for bridge audit + backtest persistence
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
3 tables (kevin_signal_bridge_state, kevin_backtest_runs, kevin_backtest_trades) all UUID-keyed for consistency with Trade/Position. KEVIN_STRATEGY_UUID constant pinned for FK joins from Trade.strategy_id.
This commit is contained in:
parent
6636054742
commit
4d40536da7
7 changed files with 367 additions and 0 deletions
49
tests/conftest.py
Normal file
49
tests/conftest.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"""Shared pytest fixtures for the trading-bot test suite.
|
||||
|
||||
Provides:
|
||||
- `db_session`: an `AsyncSession` against the dev Postgres (transaction-scoped,
|
||||
rolled back between tests).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from collections.abc import AsyncIterator
|
||||
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncSession,
|
||||
async_sessionmaker,
|
||||
create_async_engine,
|
||||
)
|
||||
|
||||
|
||||
def _dev_db_url() -> str:
|
||||
return os.environ.get(
|
||||
"TRADING_TEST_DATABASE_URL",
|
||||
"postgresql+asyncpg://trading:trading@localhost:5432/trading_dev",
|
||||
)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_session() -> AsyncIterator[AsyncSession]:
|
||||
"""Per-test session wrapped in a transaction that is rolled back at teardown."""
|
||||
engine = create_async_engine(_dev_db_url(), echo=False)
|
||||
try:
|
||||
connection = await engine.connect()
|
||||
transaction = await connection.begin()
|
||||
session_factory = async_sessionmaker(
|
||||
bind=connection,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
join_transaction_mode="create_savepoint",
|
||||
)
|
||||
session = session_factory()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
await transaction.rollback()
|
||||
await connection.close()
|
||||
finally:
|
||||
await engine.dispose()
|
||||
0
tests/shared/models/__init__.py
Normal file
0
tests/shared/models/__init__.py
Normal file
121
tests/shared/models/test_meet_kevin_trading.py
Normal file
121
tests/shared/models/test_meet_kevin_trading.py
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
"""Tests for the kevin trading SA models (audit + backtest)."""
|
||||
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from shared.models.meet_kevin import (
|
||||
KevinAnalysis,
|
||||
KevinChannel,
|
||||
KevinMarketOutlook,
|
||||
KevinStockMention,
|
||||
KevinTickerAction,
|
||||
KevinTimeHorizon,
|
||||
KevinVideo,
|
||||
KevinVideoStatus,
|
||||
)
|
||||
from shared.models.meet_kevin_trading import (
|
||||
BridgeStatus,
|
||||
KevinBacktestRun,
|
||||
KevinBacktestRunStatus,
|
||||
KevinBacktestTrade,
|
||||
KevinSignalBridgeState,
|
||||
TriggerSource,
|
||||
)
|
||||
|
||||
|
||||
async def _seed_mention(db_session: AsyncSession) -> KevinStockMention:
|
||||
channel = KevinChannel(youtube_channel_id="UCtest", title="t")
|
||||
db_session.add(channel)
|
||||
await db_session.flush()
|
||||
video = KevinVideo(
|
||||
channel_id=channel.id,
|
||||
youtube_video_id="v1",
|
||||
title="t",
|
||||
published_at=datetime.now(timezone.utc),
|
||||
status=KevinVideoStatus.ANALYZED,
|
||||
)
|
||||
db_session.add(video)
|
||||
await db_session.flush()
|
||||
analysis = KevinAnalysis(
|
||||
video_id=video.id,
|
||||
model="claude-sonnet",
|
||||
prompt_version="v1",
|
||||
market_outlook_direction=KevinMarketOutlook.NEUTRAL,
|
||||
market_outlook_reasoning="x",
|
||||
summary="x",
|
||||
prompt_tokens=10,
|
||||
completion_tokens=10,
|
||||
cost_usd=Decimal("0.01"),
|
||||
)
|
||||
db_session.add(analysis)
|
||||
await db_session.flush()
|
||||
mention = KevinStockMention(
|
||||
video_id=video.id,
|
||||
analysis_id=analysis.id,
|
||||
symbol="NVDA",
|
||||
action=KevinTickerAction.BUY,
|
||||
conviction=Decimal("0.7"),
|
||||
time_horizon=KevinTimeHorizon.WEEKS,
|
||||
rationale_quote="x",
|
||||
)
|
||||
db_session.add(mention)
|
||||
await db_session.flush()
|
||||
return mention
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bridge_state_inserts_and_loads(db_session: AsyncSession):
|
||||
mention = await _seed_mention(db_session)
|
||||
|
||||
state = KevinSignalBridgeState(
|
||||
mention_id=mention.id,
|
||||
bridge_status=BridgeStatus.EMITTED,
|
||||
effective_conviction=Decimal("0.75"),
|
||||
notes="boosted +0.05",
|
||||
)
|
||||
db_session.add(state)
|
||||
await db_session.flush()
|
||||
|
||||
row = (
|
||||
await db_session.execute(
|
||||
select(KevinSignalBridgeState).where(
|
||||
KevinSignalBridgeState.mention_id == mention.id
|
||||
)
|
||||
)
|
||||
).scalar_one()
|
||||
assert row.bridge_status == BridgeStatus.EMITTED
|
||||
assert row.effective_conviction == Decimal("0.750")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_backtest_run_and_trade_cascade(db_session: AsyncSession):
|
||||
run = KevinBacktestRun(
|
||||
run_uuid=uuid.uuid4(),
|
||||
status=KevinBacktestRunStatus.COMPLETED,
|
||||
trigger_source=TriggerSource.MANUAL,
|
||||
params_json={"holding_days": 10},
|
||||
metrics_json={"total_return_pct": 3.4},
|
||||
)
|
||||
db_session.add(run)
|
||||
await db_session.flush()
|
||||
|
||||
trade = KevinBacktestTrade(
|
||||
run_id=run.id,
|
||||
symbol="NVDA",
|
||||
entry_at=datetime.now(timezone.utc),
|
||||
entry_price=Decimal("180.00"),
|
||||
qty=Decimal("11"),
|
||||
)
|
||||
db_session.add(trade)
|
||||
await db_session.flush()
|
||||
|
||||
# Cascade: delete run -> trade gone
|
||||
await db_session.delete(run)
|
||||
await db_session.flush()
|
||||
trades = (await db_session.execute(select(KevinBacktestTrade))).scalars().all()
|
||||
assert trades == []
|
||||
Loading…
Add table
Add a link
Reference in a new issue