fix(history): stabilize download history pagination

This commit is contained in:
jxxghp
2026-08-03 08:45:35 +08:00
parent d8adb4fbfe
commit 52ca375f3d
4 changed files with 104 additions and 4 deletions

View File

@@ -98,3 +98,92 @@ def test_download_history_title_search_is_case_insensitive(tmp_path: Path):
await engine.dispose()
asyncio.run(run_case())
def test_download_history_page_is_newest_first(tmp_path: Path):
"""下载历史分页应按时间和 ID 倒序稳定返回。"""
engine = create_engine(f"sqlite:///{tmp_path / 'download_history_page.db'}")
SessionFactory = sessionmaker(bind=engine)
Base.metadata.create_all(bind=engine)
try:
with SessionFactory() as db:
db.add_all(
[
DownloadHistory(
path="/downloads/oldest",
type="电影",
title="Oldest",
date="2026-06-01 00:00:00",
),
DownloadHistory(
path="/downloads/newer-first",
type="电影",
title="Newer First",
date="2026-06-02 00:00:00",
),
DownloadHistory(
path="/downloads/newer-second",
type="电影",
title="Newer Second",
date="2026-06-02 00:00:00",
),
]
)
db.commit()
first_page = DownloadHistory.list_by_page(db, page=1, count=2)
second_page = DownloadHistory.list_by_page(db, page=2, count=2)
assert [item.title for item in first_page] == ["Newer Second", "Newer First"]
assert [item.title for item in second_page] == ["Oldest"]
finally:
engine.dispose()
def test_async_download_history_page_is_newest_first(tmp_path: Path):
"""异步下载历史分页应按时间和 ID 倒序稳定返回。"""
async def run_case():
"""执行异步分页顺序断言。"""
engine = create_async_engine(f"sqlite+aiosqlite:///{tmp_path / 'async_download_history_page.db'}")
SessionFactory = async_sessionmaker(bind=engine)
try:
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with SessionFactory() as db:
db.add_all(
[
DownloadHistory(
path="/downloads/oldest",
type="电影",
title="Oldest",
date="2026-06-01 00:00:00",
),
DownloadHistory(
path="/downloads/newer-first",
type="电影",
title="Newer First",
date="2026-06-02 00:00:00",
),
DownloadHistory(
path="/downloads/newer-second",
type="电影",
title="Newer Second",
date="2026-06-02 00:00:00",
),
]
)
await db.commit()
first_page = await DownloadHistory.async_list_by_page(db, page=1, count=2)
second_page = await DownloadHistory.async_list_by_page(db, page=2, count=2)
assert [item.title for item in first_page] == ["Newer Second", "Newer First"]
assert [item.title for item in second_page] == ["Oldest"]
finally:
await engine.dispose()
asyncio.run(run_case())