fix: 完善失败重试与跳过边界
This commit is contained in:
@@ -34,6 +34,24 @@ class OneFailedOneSuccessfulItemPlatform:
|
||||
return [CommentData(source_comment_id="c1", content="继续成功")]
|
||||
|
||||
|
||||
class AllItemsFailPlatform:
|
||||
def fetch_hotspots(self, *, limit):
|
||||
from app.platforms.base import HotspotData
|
||||
|
||||
return [HotspotData(source_hot_id="h1", title="热点一", rank=1)]
|
||||
|
||||
def search_items_by_hotspot(self, keyword, *, limit):
|
||||
from app.platforms.base import ContentItemData
|
||||
|
||||
return [
|
||||
ContentItemData(source_item_id="bad-one", item_type="video", title="失败内容一"),
|
||||
ContentItemData(source_item_id="bad-two", item_type="video", title="失败内容二"),
|
||||
]
|
||||
|
||||
def fetch_comments(self, source_item_id, *, limit):
|
||||
raise PlatformAPIError("comments exhausted", error_type="rate_limited", status_code=429)
|
||||
|
||||
|
||||
def test_hotspot_failure_marks_task_failed(monkeypatch):
|
||||
with make_test_client() as (_client, engine):
|
||||
monkeypatch.setattr("app.services.task_service.build_platform", lambda _platform: FailingHotspotPlatform())
|
||||
@@ -102,3 +120,30 @@ def test_failed_content_item_is_recorded_and_following_item_continues(monkeypatc
|
||||
assert failed_item.error_stage == "crawl_comments"
|
||||
assert failed_item.error_type == "rate_limited"
|
||||
assert successful_item.status == "success"
|
||||
|
||||
|
||||
def test_task_fails_with_visible_reason_when_all_content_items_fail(monkeypatch):
|
||||
with make_test_client() as (_client, engine):
|
||||
monkeypatch.setattr("app.services.task_service.build_platform", lambda _platform: AllItemsFailPlatform())
|
||||
|
||||
with Session(engine) as session:
|
||||
task = create_task(
|
||||
session,
|
||||
CreateTaskRequest(platform="douyin", hotspot_limit=1, item_limit_per_hotspot=2, comment_limit_per_item=10),
|
||||
submit_background=False,
|
||||
)
|
||||
task_id = task.id
|
||||
run_task(task_id, session_factory=lambda: session)
|
||||
|
||||
persisted = session.get(Task, task_id)
|
||||
failed_items = session.query(ContentItem).filter_by(task_id=task_id, status="failed").all()
|
||||
|
||||
assert persisted.status == "failed"
|
||||
assert persisted.total_items_count == 2
|
||||
assert persisted.processed_items_count == 2
|
||||
assert persisted.successful_items_count == 0
|
||||
assert persisted.failed_items_count == 2
|
||||
assert persisted.error_stage == "crawl_comments"
|
||||
assert persisted.error_type == "rate_limited"
|
||||
assert persisted.error_message == "comments exhausted"
|
||||
assert len(failed_items) == 2
|
||||
|
||||
@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.db import Base
|
||||
from app.models import Comment, ContentItem, Hotspot, Task
|
||||
from app.services.report_service import build_comment_metrics, generate_item_report
|
||||
from app.services.report_service import DEFAULT_SUMMARY, build_comment_metrics, generate_hotspot_report, generate_item_report
|
||||
|
||||
|
||||
def test_build_comment_metrics_counts_sentiments_and_top_labels():
|
||||
@@ -74,3 +74,55 @@ def test_generate_item_report_persists_markdown_and_default_summary():
|
||||
assert "样本评论数量" in report.markdown_content
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_generate_hotspot_report_persists_default_summary_when_ai_summary_fails():
|
||||
engine = create_engine("sqlite:///:memory:", connect_args={"check_same_thread": False})
|
||||
Base.metadata.create_all(engine)
|
||||
try:
|
||||
with Session(engine) as session:
|
||||
task = Task(platform="xiaohongshu", status="success")
|
||||
session.add(task)
|
||||
session.flush()
|
||||
hotspot = Hotspot(task_id=task.id, platform="xiaohongshu", title="热点", raw_data="{}")
|
||||
session.add(hotspot)
|
||||
session.flush()
|
||||
item = ContentItem(
|
||||
task_id=task.id,
|
||||
hotspot_id=hotspot.id,
|
||||
platform="xiaohongshu",
|
||||
source_item_id="n1",
|
||||
item_type="note",
|
||||
title="笔记",
|
||||
status="success",
|
||||
raw_data="{}",
|
||||
)
|
||||
session.add(item)
|
||||
session.flush()
|
||||
session.add(
|
||||
Comment(
|
||||
task_id=task.id,
|
||||
hotspot_id=hotspot.id,
|
||||
content_item_id=item.id,
|
||||
platform="xiaohongshu",
|
||||
source_comment_id="c1",
|
||||
content="好评",
|
||||
sentiment="positive",
|
||||
labels='["认可"]',
|
||||
raw_data="{}",
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
report = generate_hotspot_report(
|
||||
session,
|
||||
hotspot.id,
|
||||
summary_provider=lambda *_args, **_kwargs: (_ for _ in ()).throw(RuntimeError("summary failed")),
|
||||
)
|
||||
|
||||
assert report.report_type == "hotspot"
|
||||
assert report.hotspot_id == hotspot.id
|
||||
assert report.summary == DEFAULT_SUMMARY
|
||||
assert DEFAULT_SUMMARY in report.markdown_content
|
||||
finally:
|
||||
engine.dispose()
|
||||
|
||||
Reference in New Issue
Block a user