mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-09 09:26:55 +08:00
Merge pull request #6571 from cwd0204/fix/session-download-history-async-count
This commit is contained in:
@@ -287,6 +287,12 @@ class SessionDownloadHistoryRepository:
|
|||||||
)
|
)
|
||||||
return [_project_history(record) for record in records]
|
return [_project_history(record) for record in records]
|
||||||
|
|
||||||
|
async def async_count(self) -> int:
|
||||||
|
"""在请求异步 Session 内统计下载历史总数。"""
|
||||||
|
if not isinstance(self._session, AsyncSession):
|
||||||
|
raise RuntimeError("下载历史异步统计需要 AsyncSession")
|
||||||
|
return await DownloadHistoryOper(self._session).async_count()
|
||||||
|
|
||||||
def stage_delete_history(self, history_id: int) -> None:
|
def stage_delete_history(self, history_id: int) -> None:
|
||||||
"""在请求同步 Session 内暂存下载历史删除。"""
|
"""在请求同步 Session 内暂存下载历史删除。"""
|
||||||
if not isinstance(self._session, Session):
|
if not isinstance(self._session, Session):
|
||||||
|
|||||||
@@ -205,3 +205,27 @@ def test_session_repository_obeys_caller_transaction(db) -> None:
|
|||||||
repository.stage_delete_history(history.id)
|
repository.stage_delete_history(history.id)
|
||||||
session.commit()
|
session.commit()
|
||||||
assert _repository().get_by_hash("request-history-hash") is None
|
assert _repository().get_by_hash("request-history-hash") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_session_repository_counts_history_in_caller_async_session(db) -> None:
|
||||||
|
"""请求级 adapter 在调用方 AsyncSession 内统计总数,与分页读取口径一致。"""
|
||||||
|
repository = _repository()
|
||||||
|
baseline_count = asyncio.run(repository.async_count())
|
||||||
|
history_id = repository.add(_history_write(download_hash="request-async-count-hash"))
|
||||||
|
|
||||||
|
async def exercise() -> tuple[int, list[DownloadHistorySnapshot]]:
|
||||||
|
"""在同一请求 AsyncSession 内先统计总数再分页读取。"""
|
||||||
|
async with async_session_scope() as session:
|
||||||
|
session_repository = SessionDownloadHistoryRepository(session)
|
||||||
|
total = await session_repository.async_count()
|
||||||
|
records = await session_repository.async_list_by_page(count=10)
|
||||||
|
return total, records
|
||||||
|
|
||||||
|
total, records = asyncio.run(exercise())
|
||||||
|
|
||||||
|
assert total == baseline_count + 1
|
||||||
|
assert any(record.id == history_id for record in records)
|
||||||
|
|
||||||
|
with SessionFactory() as session:
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
asyncio.run(SessionDownloadHistoryRepository(session).async_count())
|
||||||
|
|||||||
Reference in New Issue
Block a user