refactor: 统一代码格式,调整多行代码换行风格
对多个文件进行代码格式化调整,将长行参数拆分为多行书写,提升代码可读性,包括: - 调整函数定义、调用的多行换行格式 - 优化列表、元组、字典的多行排版 - 新增README.md项目说明文档
This commit is contained in:
@@ -7,15 +7,28 @@ from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from app.config import Settings
|
||||
from app.core.ingest_tasks import DEDUP_KEY_PREFIX, REDIS_KEY_PREFIX, IngestTaskManager, IngestTaskStatus
|
||||
from app.core.ingest_tasks import (
|
||||
DEDUP_KEY_PREFIX,
|
||||
REDIS_KEY_PREFIX,
|
||||
IngestTaskManager,
|
||||
IngestTaskStatus,
|
||||
)
|
||||
from app.core.ingestion import IngestionError
|
||||
from app.models.document import DocumentInput, DocumentSummary, IngestionResult, SummaryLevel
|
||||
from app.models.document import (
|
||||
DocumentInput,
|
||||
DocumentSummary,
|
||||
IngestionResult,
|
||||
SummaryLevel,
|
||||
)
|
||||
|
||||
|
||||
def make_summary() -> DocumentSummary:
|
||||
"""构造固定的三级总结"""
|
||||
return DocumentSummary(
|
||||
l1_summary="一句话总结", l2_outline=None, l3_content_outline="内容大纲", level=SummaryLevel.L3
|
||||
l1_summary="一句话总结",
|
||||
l2_outline=None,
|
||||
l3_content_outline="内容大纲",
|
||||
level=SummaryLevel.L3,
|
||||
)
|
||||
|
||||
|
||||
@@ -42,7 +55,9 @@ class FakeIngester:
|
||||
self.gate: asyncio.Event | None = None
|
||||
self.started = asyncio.Event()
|
||||
|
||||
async def ingest(self, doc: DocumentInput, progress_cb: Callable[[str], None] | None = None) -> IngestionResult:
|
||||
async def ingest(
|
||||
self, doc: DocumentInput, progress_cb: Callable[[str], None] | None = None
|
||||
) -> IngestionResult:
|
||||
self.calls.append(doc)
|
||||
self.started.set()
|
||||
if progress_cb is not None:
|
||||
@@ -64,7 +79,9 @@ class FakeRedis:
|
||||
self.writes: list[tuple[str, dict[str, Any], int | None]] = []
|
||||
self.store: dict[str, dict[str, Any]] = {}
|
||||
|
||||
async def set_json(self, key: str, value: dict[str, Any], ttl: int | None = None) -> bool:
|
||||
async def set_json(
|
||||
self, key: str, value: dict[str, Any], ttl: int | None = None
|
||||
) -> bool:
|
||||
self.writes.append((key, value, ttl))
|
||||
if self.fail_writes:
|
||||
raise RuntimeError("redis down")
|
||||
@@ -105,8 +122,12 @@ async def test_submit_returns_immediately_and_completes() -> None:
|
||||
|
||||
async def test_ingestion_error_marks_failed_with_stage_and_partial_summary() -> None:
|
||||
"""IngestionError:任务 failed,error.stage 透传,partial_summary 保留"""
|
||||
summary = DocumentSummary(l1_summary="L1", l2_outline=None, l3_content_outline="L3", level=SummaryLevel.L3)
|
||||
ingester = FakeIngester(error=IngestionError("classify", "分类判定失败: boom", summary=summary))
|
||||
summary = DocumentSummary(
|
||||
l1_summary="L1", l2_outline=None, l3_content_outline="L3", level=SummaryLevel.L3
|
||||
)
|
||||
ingester = FakeIngester(
|
||||
error=IngestionError("classify", "分类判定失败: boom", summary=summary)
|
||||
)
|
||||
manager = IngestTaskManager(ingester, FakeRedis(), Settings())
|
||||
|
||||
task_id = await manager.submit(DocumentInput(text="正文"))
|
||||
@@ -179,10 +200,14 @@ async def test_redis_mirror_ttl_done_and_failed() -> None:
|
||||
assert done_writes[-1][0]["status"] == IngestTaskStatus.DONE
|
||||
|
||||
redis2 = FakeRedis()
|
||||
failing_manager = IngestTaskManager(FakeIngester(error=RuntimeError("boom")), redis2, settings)
|
||||
failing_manager = IngestTaskManager(
|
||||
FakeIngester(error=RuntimeError("boom")), redis2, settings
|
||||
)
|
||||
failed_id = await failing_manager.submit(DocumentInput(text="y"))
|
||||
await failing_manager.wait_done(failed_id, timeout=5)
|
||||
failed_writes = [(v, ttl) for key, v, ttl in redis2.writes if key.endswith(failed_id)]
|
||||
failed_writes = [
|
||||
(v, ttl) for key, v, ttl in redis2.writes if key.endswith(failed_id)
|
||||
]
|
||||
assert failed_writes
|
||||
assert failed_writes[-1][0]["status"] == IngestTaskStatus.FAILED
|
||||
assert failed_writes[-1][1] == 604800
|
||||
@@ -275,6 +300,7 @@ async def test_dedup_skipped_when_redis_unavailable() -> None:
|
||||
|
||||
async def test_dedup_lookup_failure_falls_back_to_normal_pipeline() -> None:
|
||||
"""Redis get_json 抛错:去重查询降级为未命中,走原流水线"""
|
||||
|
||||
class _ExplodingRedis(FakeRedis):
|
||||
async def get_json(self, key: str) -> dict[str, Any] | None:
|
||||
raise RuntimeError("redis down")
|
||||
|
||||
Reference in New Issue
Block a user