121 lines
3.5 KiB
Python
121 lines
3.5 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) == []
|