返回 last30days-skill
test_reddit_listing.py
根目录 / tests / test_reddit_listing.py
1 """Tests for scripts/lib/reddit_listing.py — keyless scored listing scrape."""
2
3 from pathlib import Path
4 from unittest import mock
5
6 from lib import reddit_listing as rl
7
8 FIXTURE = Path(__file__).resolve().parent.parent / "fixtures" / "reddit_listing_cards_sample.html"
9
10
11 def _html():
12 return FIXTURE.read_text(encoding="utf-8")
13
14
15 class TestParseCards:
16 """parse_cards reads <shreddit-post> cards into scored post dicts."""
17
18 def test_parses_five_cards(self):
19 posts = rl.parse_cards(_html(), query="netherlands")
20 assert len(posts) == 5
21
22 def test_real_score_and_count(self):
23 posts = rl.parse_cards(_html())
24 top = posts[0]
25 assert top["score"] == 52692 # the real upvote count
26 assert top["engagement"]["score"] == 52692
27 assert top["num_comments"] == 1743
28 assert top["engagement"]["num_comments"] == 1743
29
30 def test_normalized_shape(self):
31 post = rl.parse_cards(_html())[0]
32 required = {"id", "title", "url", "score", "num_comments", "subreddit",
33 "created_utc", "author", "selftext", "date",
34 "engagement", "relevance", "why_relevant", "metadata"}
35 assert required.issubset(set(post.keys()))
36 assert post["why_relevant"] == "Reddit listing"
37 assert post["metadata"]["post_id"] # post id captured for backfill
38
39 def test_fields_populated(self):
40 post = rl.parse_cards(_html())[0]
41 assert post["title"]
42 assert post["author"] == "AdSpecialist6598"
43 assert post["subreddit"] == "technology"
44 assert "/comments/" in post["url"]
45 assert post["date"] and len(post["date"]) == 10
46
47 def test_empty_html_returns_empty(self):
48 assert rl.parse_cards("") == []
49 assert rl.parse_cards("<div>no cards</div>") == []
50
51
52 class TestListingUrl:
53 def test_top_includes_timeframe(self):
54 u = rl._listing_url("technology", "top")
55 assert "community-more-posts/top/" in u and "name=technology" in u and "t=month" in u
56
57 def test_hot_no_timeframe(self):
58 u = rl._listing_url("r/technology", "hot")
59 assert "community-more-posts/hot/" in u and "name=technology" in u and "t=" not in u
60 assert ".json" not in u
61
62
63 class TestFetchListings:
64 def test_dedupes_across_sorts(self):
65 with mock.patch.object(rl.http, "get_text", return_value=_html()):
66 posts = rl.fetch_listings(["technology"], depth="default")
67 urls = [p["url"] for p in posts]
68 assert len(urls) == len(set(urls)) # top + hot return same cards -> deduped
69
70 def test_no_subreddits_returns_empty(self):
71 assert rl.fetch_listings([], depth="default") == []
72
73 def test_all_fetches_fail_returns_empty(self):
74 with mock.patch.object(rl.http, "get_text", return_value=None):
75 assert rl.fetch_listings(["technology"]) == []
76
77
78 class TestScoreIndex:
79 def test_builds_post_id_to_score_map(self):
80 with mock.patch.object(rl.http, "get_text", return_value=_html()):
81 idx = rl.score_index(["technology"], depth="quick")
82 assert idx # non-empty
83 first = next(iter(idx.values()))
84 assert set(first.keys()) == {"score", "num_comments"}
85 assert any(v["score"] == 52692 for v in idx.values())
86
86 lines PYTHON