diff --git a/cursor_pagination.py b/cursor_pagination.py index 3caf6ea..dbcc1d7 100644 --- a/cursor_pagination.py +++ b/cursor_pagination.py @@ -36,9 +36,6 @@ def __getitem__(self, key): def __repr__(self): return '' % (', '.join(repr(i) for i in self.items[:21]), ' (remaining truncated)' if len(self.items) > 21 else '') - async def acount(self): - return await self.items.acount() - class CursorPaginator(object): delimiter = '|' @@ -75,10 +72,6 @@ def _apply_paginator_arguments(self, qs, first=None, last=None, after=None, befo """ Apply first/after, last/before filtering to the queryset """ - page_size = first or last - if page_size is None: - return CursorPage(qs, self) - from_last = last is not None if from_last and first is not None: raise ValueError('Cannot process first and last') @@ -112,7 +105,7 @@ def page(self, first=None, last=None, after=None, before=None): qs = self._apply_paginator_arguments(qs, first, last, after, before) qs = list(qs) - page_size = first or last + page_size = first if first is not None else last items = qs[:page_size] if last is not None: items.reverse() @@ -124,7 +117,7 @@ async def apage(self, first=None, last=None, after=None, before=None): qs = self.queryset qs = self._apply_paginator_arguments(qs, first, last, after, before) - page_size = first or last + page_size = first if first is not None else last items = [] async for item in qs[:page_size].aiterator(): items.append(item) diff --git a/tests/tests.py b/tests/tests.py index a769dd1..0603b44 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -55,6 +55,18 @@ def setUpTestData(cls): cls.items.append(post) cls.paginator = CursorPaginator(Post.objects.all(), ('-created',)) + def test_first_page_zero(self): + page = self.paginator.page(first=0) + self.assertSequenceEqual(page, []) + self.assertTrue(page.has_next) + self.assertFalse(page.has_previous) + + async def test_async_first_page_zero(self): + page = await self.paginator.apage(first=0) + self.assertSequenceEqual(page, []) + self.assertTrue(page.has_next) + self.assertFalse(page.has_previous) + def test_first_page(self): page = self.paginator.page(first=2) self.assertSequenceEqual(page, [self.items[0], self.items[1]]) @@ -127,6 +139,18 @@ def setUpTestData(cls): cls.items.append(post) cls.paginator = CursorPaginator(Post.objects.all(), ('-created',)) + def test_first_page_zero(self): + page = self.paginator.page(last=0) + self.assertSequenceEqual(page, []) + self.assertTrue(page.has_previous) + self.assertFalse(page.has_next) + + async def test_async_first_page_zero(self): + page = await self.paginator.apage(last=0) + self.assertSequenceEqual(page, []) + self.assertTrue(page.has_previous) + self.assertFalse(page.has_next) + def test_first_page(self): page = self.paginator.page(last=2) self.assertSequenceEqual(page, [self.items[18], self.items[19]])