From 2c0f2b897d89f6c620b39ca6c8ccac7851f7f6eb Mon Sep 17 00:00:00 2001 From: Erwan Rouchet <rouchet@teklia.com> Date: Mon, 27 May 2019 10:30:11 +0000 Subject: [PATCH] Handle zero results in pagination --- arkindex/pagination.py | 5 +++++ arkindex/tests/test_pagination.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 arkindex/tests/test_pagination.py diff --git a/arkindex/pagination.py b/arkindex/pagination.py index 0b5c2d1..31ecfd8 100644 --- a/arkindex/pagination.py +++ b/arkindex/pagination.py @@ -48,6 +48,11 @@ class ResponsePaginator(Sized, Iterator): if self.data and self.data.get('next') is None: raise StopIteration self._fetch_page(self.current_page + 1) + + # Even after fetching a new page, if the new page is empty, just fail + if len(self.results) < 1: + raise StopIteration + return self.results.pop(0) def __len__(self): diff --git a/arkindex/tests/test_pagination.py b/arkindex/tests/test_pagination.py new file mode 100644 index 0000000..621695b --- /dev/null +++ b/arkindex/tests/test_pagination.py @@ -0,0 +1,16 @@ +import pytest +import responses +from arkindex import ArkindexClient + + +@responses.activate +def test_pagination_empty(): + responses.add( + responses.GET, + 'https://dummy.test/api/v1/elements/', + json={'count': 0, 'previous': None, 'next': None, 'results': []}, + ) + + cli = ArkindexClient('t0k3n', base_url='https://dummy.test') + with pytest.raises(StopIteration): + next(cli.paginate('ListElements')) -- GitLab