refactor: split pagination logic from response models

- Extract pagination logic to separate util module
- Update response models to use new pagination util
- Fix pagination calculation edge cases

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-05-03 22:57:21 +02:00
parent b2747381ec
commit fc57c83f79
4 changed files with 47 additions and 29 deletions

View File

@@ -102,3 +102,38 @@ class TestComputeTotalPages:
with pytest.raises(ValueError, match="page_size must be >= 1"):
compute_total_pages(100, -1)
class TestPaginationMetadataMode:
"""Test pagination_mode field in create_pagination_metadata."""
def test_create_pagination_metadata_sets_offset_mode(self) -> None:
"""create_pagination_metadata sets pagination_mode to 'offset'."""
from app.utils.pagination import create_pagination_metadata
metadata = create_pagination_metadata(total=100, page=2, page_size=10)
assert metadata.pagination_mode == "offset"
def test_create_keyset_pagination_metadata_sets_cursor_mode(self) -> None:
"""create_keyset_pagination_metadata sets pagination_mode to 'cursor'."""
from app.utils.pagination import create_keyset_pagination_metadata
metadata = create_keyset_pagination_metadata([], None, 10)
assert metadata.pagination_mode == "cursor"
def test_cursor_metadata_has_cursor_none_when_no_next_page(self) -> None:
"""Cursor metadata with no next page has cursor=None and has_next_page=False."""
from app.utils.pagination import create_keyset_pagination_metadata
metadata = create_keyset_pagination_metadata([], None, 10)
assert metadata.has_next_page is False
assert metadata.cursor is None
def test_cursor_metadata_has_next_page_when_cursor_present(self) -> None:
"""Cursor metadata with next_cursor sets has_next_page=True."""
from app.utils.pagination import create_keyset_pagination_metadata, encode_cursor
next_cursor = encode_cursor(42)
metadata = create_keyset_pagination_metadata([{"id": 42}], next_cursor, 10)
assert metadata.has_next_page is True
assert metadata.cursor == next_cursor