|
| 1 | +import pytest |
| 2 | +from cloudflare.pagination import AsyncV4PagePaginationArray, V4PagePaginationArrayResultInfo |
| 3 | + |
| 4 | +class MockOptions: |
| 5 | + """Mock object to simulate the options/params passed to the paginator.""" |
| 6 | + def __init__(self, page_number: int): |
| 7 | + self.params = {"page": page_number} |
| 8 | + |
| 9 | +@pytest.mark.asyncio |
| 10 | +async def test_async_pagination_stops_iteration_when_total_pages_reached() -> None: |
| 11 | + """ |
| 12 | + Ensures the AsyncV4PagePaginationArray iterator correctly terminates when |
| 13 | + the current page number matches the 'total_pages' field in the response metadata. |
| 14 | + |
| 15 | + This prevents infinite loops when the API returns the last page's data |
| 16 | + repeatedly for out-of-bound page requests. |
| 17 | + """ |
| 18 | + result_info = V4PagePaginationArrayResultInfo( |
| 19 | + page=5, |
| 20 | + per_page=20, |
| 21 | + total_pages=5, |
| 22 | + total_count=100, |
| 23 | + count=20 |
| 24 | + ) |
| 25 | + |
| 26 | + paginator = AsyncV4PagePaginationArray( |
| 27 | + result=[], |
| 28 | + result_info=result_info |
| 29 | + ) |
| 30 | + |
| 31 | + # Manually inject private _options to simulate the current page state |
| 32 | + object.__setattr__(paginator, "_options", MockOptions(page_number=5)) |
| 33 | + |
| 34 | + next_info = paginator.next_page_info() |
| 35 | + |
| 36 | + assert next_info is None |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_async_pagination_continues_when_more_pages_exist() -> None: |
| 40 | + """ |
| 41 | + Ensures the iterator calculates the next page parameters correctly |
| 42 | + when the current page is less than 'total_pages'. |
| 43 | + """ |
| 44 | + result_info = V4PagePaginationArrayResultInfo( |
| 45 | + page=1, |
| 46 | + per_page=20, |
| 47 | + total_pages=5, |
| 48 | + total_count=100, |
| 49 | + count=20 |
| 50 | + ) |
| 51 | + |
| 52 | + paginator = AsyncV4PagePaginationArray( |
| 53 | + result=[], |
| 54 | + result_info=result_info, |
| 55 | + ) |
| 56 | + |
| 57 | + object.__setattr__(paginator, "_options", MockOptions(page_number=1)) |
| 58 | + |
| 59 | + next_info = paginator.next_page_info() |
| 60 | + |
| 61 | + assert next_info is not None |
| 62 | + assert next_info.params["page"] == 2 |
0 commit comments