summaryrefslogtreecommitdiff
path: root/shared/tests/test_db_news.py
blob: c184bed8b2d7ff53be7951e465fbc4cabd68cb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""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"