Skip to content

Commit 98d6900

Browse files
committed
fix(tests): resolve ruff lint errors and add len() performance tests
Ruff fixes: - Remove unused 'import pytest' (F401) - Remove extraneous f-prefix from plain string 'tok1' (F541) Performance tests (requested by reviewer): - test_len_single_page_makes_no_extra_fetches: verifies that len() on a single-page PaginationList does not invoke the fetch callback at all - test_len_multi_page_fetches_all_pages_once: verifies that len() fetches remaining pages exactly once and caches the result for subsequent calls
1 parent 5a01899 commit 98d6900

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

tests/utils/test_pagination.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
from collections.abc import Callable
2121

22-
import pytest
23-
2422
from pyiceberg.utils.pagination import PaginationList
2523

2624

@@ -63,7 +61,7 @@ def fetch(token: str) -> tuple[list[int], str | None]:
6361
call_count += 1
6462
return pages[page_idx], next_tokens[page_idx]
6563

66-
first_token = f"tok1" if len(pages) > 1 else None
64+
first_token = "tok1" if len(pages) > 1 else None
6765
pl = PaginationList(pages[0], first_token, fetch)
6866
return pl, all_items
6967

@@ -192,3 +190,40 @@ def test_empty_first_page(self) -> None:
192190
def test_equality_with_plain_list(self) -> None:
193191
pl, expected = _simple_pagination_list([[1, 2], [3]])
194192
assert pl == expected
193+
194+
195+
class TestPaginationListPerformance:
196+
"""Verify that PaginationList does not eagerly fetch all pages for len()
197+
on a single-page list, and that multi-page len() fetches all pages exactly once."""
198+
199+
def test_len_single_page_makes_no_extra_fetches(self) -> None:
200+
"""len() on a single-page PaginationList should not call the fetch callback."""
201+
fetched: list[str] = []
202+
203+
def fetch(token: str) -> tuple[list[int], str | None]:
204+
fetched.append(token)
205+
return [], None
206+
207+
pl: PaginationList[int] = PaginationList([1, 2, 3], None, fetch)
208+
assert len(pl) == 3
209+
assert fetched == [], "No fetch should occur for a single-page list"
210+
211+
def test_len_multi_page_fetches_all_pages_once(self) -> None:
212+
"""len() on a multi-page PaginationList fetches remaining pages exactly once."""
213+
fetch_count = 0
214+
215+
def fetch(token: str) -> tuple[list[int], str | None]:
216+
nonlocal fetch_count
217+
fetch_count += 1
218+
if token == "p2":
219+
return [3, 4], "p3"
220+
return [5], None
221+
222+
pl: PaginationList[int] = PaginationList([1, 2], "p2", fetch)
223+
assert len(pl) == 5
224+
assert fetch_count == 2, "Should fetch pages 2 and 3 exactly once each"
225+
226+
# Second len() call must not trigger further fetches
227+
fetch_count = 0
228+
assert len(pl) == 5
229+
assert fetch_count == 0, "Second len() should use cached data"

0 commit comments

Comments
 (0)