thousandeyes-sdk-python/thousandeyes-sdk-core/test/test_pagination_iterator.py
2026-01-20 10:47:35 +00:00

67 lines
1.8 KiB
Python

from types import SimpleNamespace
from thousandeyes_sdk.core.iterable import PaginatorIterator
def test_iterator_uses_cursor_from_next_href():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("cursor") is None:
links = SimpleNamespace(next="https://example.com/items?cursor=abc")
else:
links = SimpleNamespace(next=None)
data = SimpleNamespace(links=links)
return SimpleNamespace(data=data)
responses = list(PaginatorIterator(method))
assert len(responses) == 2
assert calls == [{}, {"cursor": "abc"}]
def test_iterator_reads_cursor_from_links_mapping():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("pageCursor") is None:
data = {"_links": {"next": {"href": "https://example.com?foo=1&pageCursor=xyz"}}}
else:
data = {"_links": {"next": None}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method, cursor_param="pageCursor"))
assert calls == [{}, {"pageCursor": "xyz"}]
def test_iterator_stops_when_no_cursor_param_present():
calls = []
def method(**params):
calls.append(params.copy())
if params.get("cursor") is None:
data = {"links": {"next": "/next/page"}}
else:
data = {"links": {"next": None}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method))
assert calls == [{}]
def test_iterator_stops_on_repeated_cursor():
calls = []
def method(**params):
calls.append(params.copy())
data = {"links": {"next": "https://example.com?cursor=same"}}
return SimpleNamespace(data=data)
list(PaginatorIterator(method, cursor="same"))
assert calls == [{"cursor": "same"}]