"""Tests for database news/sentiment methods. Uses in-memory SQLite.""" from datetime import UTC, date, datetime import pytest from shared.db import Database from shared.models import NewsCategory, NewsItem from shared.sentiment_models import MarketSentiment, SymbolScore @pytest.fixture async def db(): database = Database("sqlite+aiosqlite://") await database.connect() yield database await database.close() async def test_insert_and_get_news_items(db): item = NewsItem( source="finnhub", headline="AAPL earnings beat", published_at=datetime(2026, 4, 2, 12, 0, tzinfo=UTC), sentiment=0.8, category=NewsCategory.EARNINGS, symbols=["AAPL"], ) await db.insert_news_item(item) items = await db.get_recent_news(hours=24) assert len(items) == 1 assert items[0]["headline"] == "AAPL earnings beat" async def test_upsert_symbol_score(db): score = SymbolScore( symbol="AAPL", news_score=0.5, news_count=10, social_score=0.3, policy_score=0.0, filing_score=0.2, composite=0.3, updated_at=datetime(2026, 4, 2, tzinfo=UTC), ) await db.upsert_symbol_score(score) scores = await db.get_top_symbol_scores(limit=5) assert len(scores) == 1 assert scores[0]["symbol"] == "AAPL" async def test_upsert_market_sentiment(db): ms = MarketSentiment( fear_greed=55, fear_greed_label="Neutral", vix=18.2, fed_stance="neutral", market_regime="neutral", updated_at=datetime(2026, 4, 2, tzinfo=UTC), ) await db.upsert_market_sentiment(ms) result = await db.get_latest_market_sentiment() assert result is not None assert result["fear_greed"] == 55 async def test_insert_stock_selection(db): await db.insert_stock_selection( trade_date=date(2026, 4, 2), symbol="NVDA", side="BUY", conviction=0.85, reason="CHIPS Act", key_news=["Trump signs CHIPS expansion"], sentiment_snapshot={"composite": 0.8}, ) selections = await db.get_stock_selections(date(2026, 4, 2)) assert len(selections) == 1 assert selections[0]["symbol"] == "NVDA"