refactor: match upstream unittest convention
Convert all new tests from bare pytest style to unittest.TestCase with sys.path.insert, matching the convention used by all existing tests. Remove pyproject.toml and conftest.py. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+49
-30
@@ -1,58 +1,73 @@
|
||||
"""Tests for reddit_enrich.py — comment enrichment and parsing."""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
# Add lib to path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
|
||||
|
||||
from lib import reddit_enrich
|
||||
|
||||
FIXTURES_DIR = Path(__file__).parent.parent / "fixtures"
|
||||
|
||||
class TestExtractRedditPath:
|
||||
|
||||
def _load_fixture(name):
|
||||
with open(FIXTURES_DIR / name) as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
class TestExtractRedditPath(unittest.TestCase):
|
||||
"""Tests for extract_reddit_path()."""
|
||||
|
||||
def test_valid_url(self):
|
||||
url = "https://www.reddit.com/r/ClaudeAI/comments/abc123/post_title/"
|
||||
path = reddit_enrich.extract_reddit_path(url)
|
||||
assert path == "/r/ClaudeAI/comments/abc123/post_title/"
|
||||
self.assertEqual(path, "/r/ClaudeAI/comments/abc123/post_title/")
|
||||
|
||||
def test_non_reddit_url(self):
|
||||
assert reddit_enrich.extract_reddit_path("https://example.com/foo") is None
|
||||
self.assertIsNone(reddit_enrich.extract_reddit_path("https://example.com/foo"))
|
||||
|
||||
def test_empty_string(self):
|
||||
assert reddit_enrich.extract_reddit_path("") is None
|
||||
self.assertIsNone(reddit_enrich.extract_reddit_path(""))
|
||||
|
||||
def test_old_reddit(self):
|
||||
url = "https://old.reddit.com/r/test/comments/xyz/"
|
||||
assert reddit_enrich.extract_reddit_path(url) is not None
|
||||
self.assertIsNotNone(reddit_enrich.extract_reddit_path(url))
|
||||
|
||||
|
||||
class TestParseThreadData:
|
||||
class TestParseThreadData(unittest.TestCase):
|
||||
"""Tests for parse_thread_data() using fixture."""
|
||||
|
||||
def test_parses_submission(self, load_fixture):
|
||||
data = load_fixture("reddit_thread_sample.json")
|
||||
def test_parses_submission(self):
|
||||
data = _load_fixture("reddit_thread_sample.json")
|
||||
result = reddit_enrich.parse_thread_data(data)
|
||||
assert result["submission"] is not None
|
||||
assert result["submission"]["score"] == 847
|
||||
assert result["submission"]["num_comments"] == 156
|
||||
self.assertIsNotNone(result["submission"])
|
||||
self.assertEqual(result["submission"]["score"], 847)
|
||||
self.assertEqual(result["submission"]["num_comments"], 156)
|
||||
|
||||
def test_parses_comments(self, load_fixture):
|
||||
data = load_fixture("reddit_thread_sample.json")
|
||||
def test_parses_comments(self):
|
||||
data = _load_fixture("reddit_thread_sample.json")
|
||||
result = reddit_enrich.parse_thread_data(data)
|
||||
assert len(result["comments"]) == 8
|
||||
assert result["comments"][0]["author"] == "skill_expert"
|
||||
self.assertEqual(len(result["comments"]), 8)
|
||||
self.assertEqual(result["comments"][0]["author"], "skill_expert")
|
||||
|
||||
def test_empty_input(self):
|
||||
result = reddit_enrich.parse_thread_data([])
|
||||
assert result["submission"] is None
|
||||
assert result["comments"] == []
|
||||
self.assertIsNone(result["submission"])
|
||||
self.assertEqual(result["comments"], [])
|
||||
|
||||
def test_malformed_input(self):
|
||||
result = reddit_enrich.parse_thread_data("not a list")
|
||||
assert result["submission"] is None
|
||||
self.assertIsNone(result["submission"])
|
||||
|
||||
def test_none_input(self):
|
||||
result = reddit_enrich.parse_thread_data(None)
|
||||
assert result["submission"] is None
|
||||
self.assertIsNone(result["submission"])
|
||||
|
||||
|
||||
class TestGetTopComments:
|
||||
class TestGetTopComments(unittest.TestCase):
|
||||
"""Tests for get_top_comments()."""
|
||||
|
||||
def test_sorted_by_score(self):
|
||||
@@ -62,8 +77,8 @@ class TestGetTopComments:
|
||||
{"score": 50, "author": "c"},
|
||||
]
|
||||
top = reddit_enrich.get_top_comments(comments, limit=3)
|
||||
assert top[0]["score"] == 100
|
||||
assert top[1]["score"] == 50
|
||||
self.assertEqual(top[0]["score"], 100)
|
||||
self.assertEqual(top[1]["score"], 50)
|
||||
|
||||
def test_filters_deleted(self):
|
||||
comments = [
|
||||
@@ -72,19 +87,19 @@ class TestGetTopComments:
|
||||
{"score": 10, "author": "real_user"},
|
||||
]
|
||||
top = reddit_enrich.get_top_comments(comments)
|
||||
assert len(top) == 1
|
||||
assert top[0]["author"] == "real_user"
|
||||
self.assertEqual(len(top), 1)
|
||||
self.assertEqual(top[0]["author"], "real_user")
|
||||
|
||||
def test_respects_limit(self):
|
||||
comments = [{"score": i, "author": f"u{i}"} for i in range(20)]
|
||||
top = reddit_enrich.get_top_comments(comments, limit=5)
|
||||
assert len(top) == 5
|
||||
self.assertEqual(len(top), 5)
|
||||
|
||||
def test_empty_list(self):
|
||||
assert reddit_enrich.get_top_comments([]) == []
|
||||
self.assertEqual(reddit_enrich.get_top_comments([]), [])
|
||||
|
||||
|
||||
class TestExtractCommentInsights:
|
||||
class TestExtractCommentInsights(unittest.TestCase):
|
||||
"""Tests for extract_comment_insights()."""
|
||||
|
||||
def test_filters_short_comments(self):
|
||||
@@ -93,7 +108,7 @@ class TestExtractCommentInsights:
|
||||
{"body": "A" * 50 + " this is a substantive comment about the topic."},
|
||||
]
|
||||
insights = reddit_enrich.extract_comment_insights(comments)
|
||||
assert len(insights) == 1
|
||||
self.assertEqual(len(insights), 1)
|
||||
|
||||
def test_filters_low_value_patterns(self):
|
||||
comments = [
|
||||
@@ -102,9 +117,13 @@ class TestExtractCommentInsights:
|
||||
{"body": "A" * 50 + " Here's a real insight about how to approach this problem."},
|
||||
]
|
||||
insights = reddit_enrich.extract_comment_insights(comments)
|
||||
assert len(insights) == 1
|
||||
self.assertEqual(len(insights), 1)
|
||||
|
||||
def test_respects_limit(self):
|
||||
comments = [{"body": f"Comment number {i} " + "x" * 50} for i in range(20)]
|
||||
insights = reddit_enrich.extract_comment_insights(comments, limit=3)
|
||||
assert len(insights) <= 3
|
||||
self.assertLessEqual(len(insights), 3)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user