fix: 完善失败重试与跳过边界

This commit is contained in:
meijiali
2026-07-03 15:19:50 +08:00
parent accfaeffaf
commit 89c93b5051
3 changed files with 119 additions and 1 deletions
+53 -1
View File
@@ -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()