summaryrefslogtreecommitdiff
path: root/services/news-collector/tests/test_reddit.py
blob: 31b1dc1d034889890b17cc2589083f46c365c107 (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
"""Tests for Reddit collector."""

from unittest.mock import AsyncMock, patch

import pytest
from news_collector.collectors.reddit import RedditCollector


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


def test_collector_name(collector):
    assert collector.name == "reddit"
    assert collector.poll_interval == 900


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


async def test_collect_parses_posts(collector):
    mock_posts = [
        {
            "data": {
                "title": "NVDA to the moon! AI demand is insane",
                "selftext": "Just loaded up on NVDA calls",
                "url": "https://reddit.com/r/wallstreetbets/123",
                "created_utc": 1711929600,
                "score": 500,
                "num_comments": 200,
                "subreddit": "wallstreetbets",
            }
        },
    ]
    with patch.object(
        collector, "_fetch_subreddit", new_callable=AsyncMock, return_value=mock_posts
    ):
        items = await collector.collect()
    assert len(items) >= 1
    assert items[0].source == "reddit"
    assert items[0].category.value == "social"


async def test_collect_filters_low_score(collector):
    mock_posts = [
        {
            "data": {
                "title": "Random question",
                "selftext": "",
                "url": "https://reddit.com/456",
                "created_utc": 1711929600,
                "score": 3,
                "num_comments": 1,
                "subreddit": "stocks",
            }
        },
    ]
    with patch.object(
        collector, "_fetch_subreddit", new_callable=AsyncMock, return_value=mock_posts
    ):
        items = await collector.collect()
    assert items == []