|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | +# pylint: disable=unused-variable |
| 4 | +# pylint: disable=too-many-arguments |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from collections.abc import Callable |
| 8 | + |
| 9 | +import pytest |
| 10 | +from common_library.iter_tools import iter_pages |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def all_items() -> list[int]: |
| 15 | + return list(range(11)) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +async def get_page(all_items: list[int]) -> Callable: |
| 20 | + async def _get_page(offset, limit) -> tuple[list[int], int]: |
| 21 | + await asyncio.sleep(0) |
| 22 | + return all_items[offset : offset + limit], len(all_items) |
| 23 | + |
| 24 | + return _get_page |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize("limit", [2, 3, 5]) |
| 28 | +@pytest.mark.parametrize("offset", [0, 1, 5]) |
| 29 | +async def test_iter_pages( |
| 30 | + limit: int, offset: int, get_page: Callable, all_items: list[int] |
| 31 | +): |
| 32 | + |
| 33 | + last_page = [None] * 2 |
| 34 | + async for page_items in iter_pages(get_page, offset=offset, limit=limit): |
| 35 | + assert len(page_items) <= limit |
| 36 | + |
| 37 | + assert set(last_page) != set(page_items) |
| 38 | + last_page = list(page_items) |
| 39 | + |
| 40 | + # contains sub-sequence |
| 41 | + assert str(page_items)[1:-1] in str(all_items)[1:-1] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("limit", [-1, 0]) |
| 45 | +@pytest.mark.parametrize("offset", [-1]) |
| 46 | +async def test_iter_pages_invalid(limit: int, offset: int, get_page: Callable): |
| 47 | + |
| 48 | + with pytest.raises(ValueError, match="must be"): # noqa: PT012 |
| 49 | + async for _ in iter_pages(get_page, offset=offset, limit=limit): |
| 50 | + pass |
0 commit comments