|
1 | | -from typing import Any, List, Optional |
| 1 | +from typing import Any, List, Type, Union, Optional |
2 | 2 |
|
3 | 3 | import httpx |
| 4 | +import pytest |
4 | 5 |
|
5 | | -from kernel.pagination import SyncOffsetPagination |
| 6 | +from kernel.pagination import AsyncOffsetPagination, SyncOffsetPagination |
| 7 | + |
| 8 | +PageClass = Union[Type[SyncOffsetPagination[Any]], Type[AsyncOffsetPagination[Any]]] |
| 9 | + |
| 10 | +# build() and next_page_info() are plain synchronous methods on both classes, |
| 11 | +# so both generated variants are pinned against drifting apart on regeneration. |
| 12 | +both_classes = pytest.mark.parametrize("cls", [SyncOffsetPagination, AsyncOffsetPagination]) |
6 | 13 |
|
7 | 14 |
|
8 | 15 | def _page( |
9 | | - *, items: List[Any], next_offset: Optional[int], has_more: Optional[bool] |
10 | | -) -> SyncOffsetPagination[Any]: |
| 16 | + cls: PageClass, *, items: List[Any], next_offset: Optional[int], has_more: Optional[bool] |
| 17 | +) -> Any: |
11 | 18 | headers: dict[str, str] = {} |
12 | 19 | if next_offset is not None: |
13 | 20 | headers["X-Next-Offset"] = str(next_offset) |
14 | 21 | if has_more is not None: |
15 | 22 | headers["X-Has-More"] = "true" if has_more else "false" |
16 | 23 | response = httpx.Response(200, headers=headers) |
17 | | - return SyncOffsetPagination.build(response=response, data=items) |
| 24 | + return cls.build(response=response, data=items) |
18 | 25 |
|
19 | 26 |
|
20 | | -def test_next_page_starts_at_exactly_x_next_offset() -> None: |
| 27 | +@both_classes |
| 28 | +def test_next_page_starts_at_exactly_x_next_offset(cls: PageClass) -> None: |
21 | 29 | # X-Next-Offset already holds the next page's start. Adding the current |
22 | 30 | # page length on top (the old behavior) skipped a full page per iteration. |
23 | | - page = _page(items=[{}] * 100, next_offset=100, has_more=True) |
| 31 | + page = _page(cls, items=[{}] * 100, next_offset=100, has_more=True) |
24 | 32 | info = page.next_page_info() |
25 | 33 | assert info is not None |
26 | 34 | assert info.params == {"offset": 100} |
27 | 35 |
|
28 | 36 |
|
29 | | -def test_stops_when_x_next_offset_absent() -> None: |
30 | | - page = _page(items=[{}] * 100, next_offset=None, has_more=True) |
| 37 | +@both_classes |
| 38 | +def test_stops_cleanly_when_last_page_omits_x_next_offset(cls: PageClass) -> None: |
| 39 | + page = _page(cls, items=[{}] * 50, next_offset=None, has_more=False) |
31 | 40 | assert page.next_page_info() is None |
32 | 41 | assert page.has_next_page() is False |
33 | 42 |
|
34 | 43 |
|
35 | | -def test_stops_when_x_has_more_false() -> None: |
36 | | - page = _page(items=[{}] * 50, next_offset=200, has_more=False) |
| 44 | +@both_classes |
| 45 | +def test_stops_when_x_has_more_false(cls: PageClass) -> None: |
| 46 | + # Covers the 0 sentinel the API emits on last pages: has_more is false |
| 47 | + # whenever next_offset is 0, and has_more gates first. |
| 48 | + page = _page(cls, items=[{}] * 50, next_offset=0, has_more=False) |
37 | 49 | assert page.has_next_page() is False |
38 | 50 |
|
39 | 51 |
|
40 | | -def test_stops_on_empty_page() -> None: |
41 | | - page = _page(items=[], next_offset=300, has_more=True) |
| 52 | +@both_classes |
| 53 | +def test_stops_on_empty_page(cls: PageClass) -> None: |
| 54 | + page = _page(cls, items=[], next_offset=300, has_more=True) |
42 | 55 | assert page.has_next_page() is False |
| 56 | + |
| 57 | + |
| 58 | +@both_classes |
| 59 | +def test_refuses_to_silently_truncate_on_contradictory_headers(cls: PageClass) -> None: |
| 60 | + page = _page(cls, items=[{}] * 100, next_offset=None, has_more=True) |
| 61 | + with pytest.raises(RuntimeError, match="refusing to silently truncate"): |
| 62 | + page.has_next_page() |
0 commit comments