763e06c127
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
"""Tests para fetch_reddit_search.
|
|
|
|
El parser (_parse_children) se testea con un fixture offline. La funcion
|
|
completa fetch_reddit_search hace red real; aqui solo validamos el shape del
|
|
parser para no depender de conectividad en CI.
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
|
from fetch_reddit_search import _parse_children
|
|
|
|
|
|
_FIXTURE_CHILDREN = [
|
|
{
|
|
"data": {
|
|
"id": "abc123",
|
|
"title": "I wish there was a CSV dedupe tool",
|
|
"selftext": "Anyone know a tool for this?",
|
|
"permalink": "/r/SaaS/comments/abc123/foo/",
|
|
"author": "user1",
|
|
"subreddit": "SaaS",
|
|
"created_utc": 1700000000.0,
|
|
"ups": 42,
|
|
}
|
|
},
|
|
{
|
|
"data": {
|
|
"id": "def456",
|
|
"title": "Link post no body",
|
|
"selftext": "",
|
|
"permalink": "/r/Entrepreneur/comments/def456/bar/",
|
|
"author": "user2",
|
|
"subreddit": "Entrepreneur",
|
|
"created_utc": 1700001234.0,
|
|
"ups": 7,
|
|
}
|
|
},
|
|
]
|
|
|
|
_EXPECTED_KEYS = {
|
|
"source", "platform_id", "title", "body", "url", "author",
|
|
"channel", "created_utc", "platform_score", "query",
|
|
}
|
|
|
|
|
|
def test_parser_normaliza_children_al_shape_exacto():
|
|
rows = _parse_children(_FIXTURE_CHILDREN, "csv dedupe")
|
|
assert len(rows) == 2
|
|
r = rows[0]
|
|
assert set(r.keys()) == _EXPECTED_KEYS
|
|
assert r["source"] == "reddit"
|
|
assert r["platform_id"] == "abc123"
|
|
assert r["title"] == "I wish there was a CSV dedupe tool"
|
|
assert r["body"] == "Anyone know a tool for this?"
|
|
assert r["url"] == "https://www.reddit.com/r/SaaS/comments/abc123/foo/"
|
|
assert r["author"] == "user1"
|
|
assert r["channel"] == "SaaS"
|
|
assert r["created_utc"] == 1700000000.0
|
|
assert isinstance(r["created_utc"], float)
|
|
assert r["platform_score"] == 42
|
|
assert isinstance(r["platform_score"], int)
|
|
assert r["query"] == "csv dedupe"
|
|
|
|
|
|
def test_selftext_vacio_se_mapea_a_body_vacio():
|
|
rows = _parse_children(_FIXTURE_CHILDREN, "q")
|
|
assert rows[1]["body"] == ""
|
|
|
|
|
|
def test_children_vacio_devuelve_lista_vacia():
|
|
assert _parse_children([], "q") == []
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_parser_normaliza_children_al_shape_exacto()
|
|
test_selftext_vacio_se_mapea_a_body_vacio()
|
|
test_children_vacio_devuelve_lista_vacia()
|
|
print("All tests passed.")
|