summaryrefslogtreecommitdiff
path: root/services/news-collector/tests/test_finnhub.py
blob: a4cf169d9b3a17b6e595ce894c4af46b8738e20f (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
"""Tests for Finnhub news collector."""

import pytest
from unittest.mock import AsyncMock, patch

from news_collector.collectors.finnhub import FinnhubCollector


@pytest.fixture
def collector():
    return FinnhubCollector(api_key="test_key")


def test_collector_name(collector):
    assert collector.name == "finnhub"
    assert collector.poll_interval == 300


async def test_is_available_with_key(collector):
    assert await collector.is_available() is True


async def test_is_available_without_key():
    c = FinnhubCollector(api_key="")
    assert await c.is_available() is False


async def test_collect_parses_response(collector):
    mock_response = [
        {
            "category": "top news",
            "datetime": 1711929600,
            "headline": "AAPL beats earnings",
            "id": 12345,
            "related": "AAPL",
            "source": "MarketWatch",
            "summary": "Apple reported better than expected...",
            "url": "https://example.com/article",
        },
        {
            "category": "top news",
            "datetime": 1711929000,
            "headline": "Fed holds rates steady",
            "id": 12346,
            "related": "",
            "source": "Reuters",
            "summary": "The Federal Reserve...",
            "url": "https://example.com/fed",
        },
    ]

    with patch.object(collector, "_fetch_news", new_callable=AsyncMock, return_value=mock_response):
        items = await collector.collect()

    assert len(items) == 2
    assert items[0].source == "finnhub"
    assert items[0].headline == "AAPL beats earnings"
    assert items[0].symbols == ["AAPL"]
    assert items[0].url == "https://example.com/article"
    assert isinstance(items[0].sentiment, float)
    assert items[1].symbols == []


async def test_collect_handles_empty_response(collector):
    with patch.object(collector, "_fetch_news", new_callable=AsyncMock, return_value=[]):
        items = await collector.collect()
    assert items == []