summaryrefslogtreecommitdiff
path: root/services/news-collector/tests/test_fear_greed.py
blob: e8bd8f046c90bb06d6b892beb66508c273819da2 (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
"""Tests for CNN Fear & Greed Index collector."""

from unittest.mock import AsyncMock, patch

import pytest
from news_collector.collectors.fear_greed import FearGreedCollector


@pytest.fixture
def collector():
    return FearGreedCollector()


def test_collector_name(collector):
    assert collector.name == "fear_greed"
    assert collector.poll_interval == 3600


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


async def test_collect_parses_api_response(collector):
    mock_data = {
        "fear_and_greed": {
            "score": 45.0,
            "rating": "Fear",
            "timestamp": "2026-04-02T12:00:00+00:00",
        }
    }
    with patch.object(collector, "_fetch_index", new_callable=AsyncMock, return_value=mock_data):
        result = await collector.collect()
    assert result.fear_greed == 45
    assert result.fear_greed_label == "Fear"


async def test_collect_returns_none_on_failure(collector):
    with patch.object(collector, "_fetch_index", new_callable=AsyncMock, return_value=None):
        result = await collector.collect()
    assert result is None


def test_classify_label():
    c = FearGreedCollector()
    assert c._classify(10) == "Extreme Fear"
    assert c._classify(30) == "Fear"
    assert c._classify(50) == "Neutral"
    assert c._classify(70) == "Greed"
    assert c._classify(85) == "Extreme Greed"