feat: 提交热榜评论分析工具 MVP 基线
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.platforms.base import PlatformAPIError
|
||||
from app.schemas import CreateTaskRequest
|
||||
from app.services.task_service import create_task, run_task
|
||||
from tests.helpers import make_test_client
|
||||
|
||||
|
||||
class FailingHotspotPlatform:
|
||||
def fetch_hotspots(self, *, limit):
|
||||
raise PlatformAPIError("hotspot failed", error_type="api_error", status_code=500)
|
||||
|
||||
|
||||
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())
|
||||
with Session(engine) as session:
|
||||
task = create_task(
|
||||
session,
|
||||
CreateTaskRequest(platform="douyin", hotspot_limit=1, item_limit_per_hotspot=1, comment_limit_per_item=10),
|
||||
submit_background=False,
|
||||
)
|
||||
task_id = task.id
|
||||
run_task(task_id, session_factory=lambda: session)
|
||||
task = session.get(type(task), task_id)
|
||||
|
||||
assert task.status == "failed"
|
||||
assert task.error_stage == "crawl_hotspots"
|
||||
assert task.error_type == "api_error"
|
||||
|
||||
|
||||
def test_unexpected_task_exception_marks_task_failed(monkeypatch):
|
||||
def raise_unexpected_error(_platform):
|
||||
raise RuntimeError("boom")
|
||||
|
||||
with make_test_client() as (_client, engine):
|
||||
monkeypatch.setattr("app.services.task_service.build_platform", raise_unexpected_error)
|
||||
with Session(engine) as session:
|
||||
task = create_task(
|
||||
session,
|
||||
CreateTaskRequest(platform="douyin", hotspot_limit=1, item_limit_per_hotspot=1, comment_limit_per_item=10),
|
||||
submit_background=False,
|
||||
)
|
||||
task_id = task.id
|
||||
run_task(task_id, session_factory=lambda: session)
|
||||
task = session.get(type(task), task_id)
|
||||
|
||||
assert task.status == "failed"
|
||||
assert task.error_stage == "system"
|
||||
assert task.error_type == "unexpected_error"
|
||||
assert task.error_message == "boom"
|
||||
Reference in New Issue
Block a user