refactor: extract admin business logic to services, introduce job queue, add derived index helpers
- Move DB operations from routes/admin.py to services/admin.py (get_logs_context, query_summary_statuses, retry_failed, delete/reset operations) - Add services/jobs.py with Job/JobEvent-based async job queue (create_job, run_job, enqueue_job) - Add services/derived.py with FTS5 reindex and paper index deletion helpers - Refactor scheduler to use job queue instead of direct pipeline calls - Add heartbeat_at/expires_at to TaskLock for lock health tracking - Remove DESIGN_REVIEW.md - Update tests: remove redundant integration tests, add unit tests for new services
This commit is contained in:
@@ -6,9 +6,6 @@ from datetime import date
|
||||
from unittest.mock import patch as upatch
|
||||
|
||||
|
||||
from app.config import settings
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
# Detail 页 & 相似论文
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
@@ -37,29 +34,6 @@ class TestDetailPage:
|
||||
class TestTrendsDashboard:
|
||||
"""趋势看板测试。"""
|
||||
|
||||
def test_trends_page_renders(self, client, sample_papers_with_summary):
|
||||
"""趋势看板页面正常渲染。"""
|
||||
resp = client.get("/trends")
|
||||
assert resp.status_code == 200
|
||||
assert "趋势看板" in resp.text
|
||||
assert "chart" in resp.text.lower() or "Chart" in resp.text
|
||||
|
||||
def test_trends_api_returns_data(self, client, sample_papers_with_summary):
|
||||
"""趋势 API 返回正确数据结构。"""
|
||||
resp = client.get("/api/stats/trends")
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
|
||||
assert "daily_counts" in data
|
||||
assert "top_tags" in data
|
||||
assert "upvotes_dist" in data
|
||||
assert "summary_completion" in data
|
||||
|
||||
assert isinstance(data["daily_counts"], list)
|
||||
assert isinstance(data["top_tags"], list)
|
||||
assert isinstance(data["upvotes_dist"], list)
|
||||
assert isinstance(data["summary_completion"], list)
|
||||
|
||||
def test_trends_api_daily_counts(self, client, sample_papers_with_summary):
|
||||
"""每日论文数量数据正确。"""
|
||||
# 使用测试数据的日期范围
|
||||
@@ -108,12 +82,6 @@ class TestTrendsDashboard:
|
||||
class TestComparePage:
|
||||
"""论文对比页测试。"""
|
||||
|
||||
def test_compare_page_no_ids(self, client):
|
||||
"""无 ID 时显示输入表单。"""
|
||||
resp = client.get("/compare")
|
||||
assert resp.status_code == 200
|
||||
assert "对比" in resp.text
|
||||
|
||||
def test_compare_page_with_ids(self, client, sample_papers_with_summary):
|
||||
"""对比多篇论文正常渲染。"""
|
||||
resp = client.get("/compare?ids=2401.20001,2401.20002")
|
||||
@@ -124,23 +92,6 @@ class TestComparePage:
|
||||
assert "一句话摘要" in resp.text
|
||||
assert "研究问题" in resp.text
|
||||
|
||||
def test_compare_page_max_5(self, client, sample_papers_with_summary):
|
||||
"""最多 5 篇。"""
|
||||
ids = "2401.20001,2401.20002,2401.20003,2401.20004,2401.20005"
|
||||
resp = client.get(f"/compare?ids={ids}")
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_compare_page_over_5_truncates(self, client, sample_papers_with_summary):
|
||||
"""超过 5 篇截断。"""
|
||||
ids = "2401.20001,2401.20002,2401.20003,2401.20004,2401.20005,2401.20006"
|
||||
resp = client.get(f"/compare?ids={ids}")
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_compare_page_invalid_ids(self, client):
|
||||
"""无效 ID 时显示空结果。"""
|
||||
resp = client.get("/compare?ids=nonexistent.99999")
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_compare_page_shows_no_summary_placeholder(
|
||||
self, client, sample_papers_with_summary
|
||||
):
|
||||
@@ -149,65 +100,3 @@ class TestComparePage:
|
||||
resp = client.get("/compare?ids=2401.20005")
|
||||
assert resp.status_code == 200
|
||||
assert "暂无总结" in resp.text
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
# Nav Bar
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestNavBar:
|
||||
"""导航栏测试。"""
|
||||
|
||||
def test_nav_includes_trends_link(self, client):
|
||||
"""导航栏应包含趋势链接。"""
|
||||
resp = client.get("/search")
|
||||
assert resp.status_code == 200
|
||||
assert "/trends" in resp.text
|
||||
|
||||
def test_nav_includes_compare_implicitly(self, client):
|
||||
"""compare 页面可访问。"""
|
||||
resp = client.get("/compare")
|
||||
assert resp.status_code == 200
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
# Graceful Degradation(CHROMA_ENABLED=false)
|
||||
# ═══════════════════════════════════════════════════════════════════════
|
||||
|
||||
|
||||
class TestGracefulDegradation:
|
||||
"""CHROMA_ENABLED=false 时优雅降级测试。"""
|
||||
|
||||
def test_search_works_without_chroma(
|
||||
self, client, monkeypatch, sample_papers_with_summary
|
||||
):
|
||||
"""CHROMA 关闭时 FTS5 搜索正常工作。"""
|
||||
monkeypatch.setattr(settings, "CHROMA_ENABLED", False)
|
||||
resp = client.get("/search?q=Test")
|
||||
assert resp.status_code == 200
|
||||
assert "Test Paper" in resp.text or "测试论文" in resp.text
|
||||
|
||||
def test_detail_works_without_chroma(
|
||||
self, client, monkeypatch, sample_papers_with_summary
|
||||
):
|
||||
"""CHROMA 关闭时详情页正常工作。"""
|
||||
monkeypatch.setattr(settings, "CHROMA_ENABLED", False)
|
||||
resp = client.get("/paper/2401.20001")
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_trends_works_without_chroma(
|
||||
self, client, monkeypatch, sample_papers_with_summary
|
||||
):
|
||||
"""CHROMA 关闭时趋势看板正常工作。"""
|
||||
monkeypatch.setattr(settings, "CHROMA_ENABLED", False)
|
||||
resp = client.get("/trends")
|
||||
assert resp.status_code == 200
|
||||
|
||||
def test_compare_works_without_chroma(
|
||||
self, client, monkeypatch, sample_papers_with_summary
|
||||
):
|
||||
"""CHROMA 关闭时对比页正常工作。"""
|
||||
monkeypatch.setattr(settings, "CHROMA_ENABLED", False)
|
||||
resp = client.get("/compare?ids=2401.20001,2401.20002")
|
||||
assert resp.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user