Files
hot_comment_radar/tests/unit/test_douyin_mapping.py
T

157 lines
4.6 KiB
Python

from app.platforms.douyin import DouyinPlatform
def test_douyin_maps_hotspots():
platform = DouyinPlatform(client=None)
payload = {
"data": {
"word_list": [
{"query_id": "q1", "title": "热点", "rank": 2, "hot_score": "888"}
]
}
}
hotspots = platform.map_hotspots(payload, limit=5)
assert hotspots[0].source_hot_id == "q1"
assert hotspots[0].title == "热点"
assert hotspots[0].rank == 2
assert hotspots[0].heat_value == "888"
def test_douyin_maps_hotspots_from_real_item_list_shape():
platform = DouyinPlatform(client=None)
payload = {
"data": {
"item_list": [
{"query_id": "q-real", "sentence": "真实热点", "rank": 1, "hot_score": 999}
]
}
}
hotspots = platform.map_hotspots(payload, limit=5)
assert hotspots[0].source_hot_id == "q-real"
assert hotspots[0].title == "真实热点"
assert hotspots[0].rank == 1
assert hotspots[0].heat_value == "999"
def test_douyin_maps_videos():
platform = DouyinPlatform(client=None)
payload = {
"data": [
{
"aweme_info": {
"aweme_id": "aweme-1",
"desc": "视频描述",
"author": {"nickname": "作者"},
"statistics": {"comment_count": 10},
}
}
]
}
items = platform.map_items(payload, limit=5)
assert items[0].source_item_id == "aweme-1"
assert items[0].title == "视频描述"
assert items[0].item_type == "video"
def test_douyin_maps_videos_from_real_business_data_shape():
platform = DouyinPlatform(client=None)
payload = {
"data": {
"business_data": [
{
"data": {
"aweme_info": {
"aweme_id": "aweme-real",
"desc": "真实视频描述",
"share_url": "https://example.com/video",
}
}
}
]
}
}
items = platform.map_items(payload, limit=5)
assert items[0].source_item_id == "aweme-real"
assert items[0].title == "真实视频描述"
assert items[0].url == "https://example.com/video"
def test_douyin_maps_comment_id_and_missing_optional_fields():
platform = DouyinPlatform(client=None)
payload = {
"comments": [
{"comment_id": "preferred", "cid": "fallback", "text": "评论"},
{"cid": "fallback-only", "text": "评论2", "digg_count": 9},
]
}
comments = platform.map_comments(payload, limit=10)
assert comments[0].source_comment_id == "preferred"
assert comments[0].content == "评论"
assert comments[0].like_count is None
assert comments[0].comment_time is None
assert comments[1].source_comment_id == "fallback-only"
assert comments[1].like_count == 9
def test_douyin_maps_comments_when_user_is_none():
platform = DouyinPlatform(client=None)
payload = {"data": {"comments": [{"cid": "c-none-user", "text": "评论", "user": None}]}}
comments = platform.map_comments(payload, limit=10)
assert comments[0].source_comment_id == "c-none-user"
assert comments[0].author is None
def test_douyin_maps_null_comments_as_empty_list():
platform = DouyinPlatform(client=None)
payload = {"data": {"comments": None}}
assert platform.map_comments(payload, limit=10) == []
class RecordingDouyinClient:
def __init__(self):
self.requests = []
def get(self, path, *, params=None):
self.requests.append((path, params))
cursor = params["cursor"]
if cursor == 0:
return {
"data": {
"comments": [{"cid": f"c-{index}", "text": f"评论 {index}"} for index in range(20)],
"cursor": 20,
"has_more": 1,
}
}
return {
"data": {
"comments": [{"cid": f"c-{index}", "text": f"评论 {index}"} for index in range(20, 55)],
"cursor": 55,
"has_more": 0,
}
}
def test_douyin_fetch_comments_paginates_until_limit():
client = RecordingDouyinClient()
platform = DouyinPlatform(client=client)
comments = platform.fetch_comments("aweme-1", limit=50)
assert len(comments) == 50
assert comments[0].source_comment_id == "c-0"
assert comments[-1].source_comment_id == "c-49"
assert [request[1]["cursor"] for request in client.requests] == [0, 20]