This commit is contained in:
Alexander Karpinsky 2017-05-16 10:36:40 +00:00 committed by GitHub
commit d4df1b8094
2 changed files with 13 additions and 3 deletions

View File

@ -572,7 +572,7 @@ class CursorPagination(BasePagination):
if not self.has_next:
return None
if self.cursor and self.cursor.reverse and self.cursor.offset != 0:
if self.cursor and self.cursor.reverse and self.cursor.offset != 0 and self.page:
# If we're reversing direction and we have an offset cursor
# then we cannot use the first position we find as a marker.
compare = self._get_position_from_instance(self.page[-1], self.ordering)
@ -620,7 +620,7 @@ class CursorPagination(BasePagination):
if not self.has_previous:
return None
if self.cursor and not self.cursor.reverse and self.cursor.offset != 0:
if self.cursor and not self.cursor.reverse and self.cursor.offset != 0 and self.page:
# If we're reversing direction and we have an offset cursor
# then we cannot use the first position we find as a marker.
compare = self._get_position_from_instance(self.page[0], self.ordering)

View File

@ -9,7 +9,7 @@ from django.test import TestCase
from rest_framework import (
exceptions, filters, generics, pagination, serializers, status
)
from rest_framework.pagination import PAGE_BREAK, PageLink
from rest_framework.pagination import PAGE_BREAK, PageLink, Cursor
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory
@ -633,6 +633,16 @@ class CursorPaginationTestsMixin:
assert isinstance(self.pagination.to_html(), type(''))
def test_offset_cutoff(self):
(previous, current, next, previous_url, next_url) = self.get_pages('/')
next_url = self.pagination.encode_cursor(Cursor(1050, False, None))
(previous, current, next, previous_url, next_url) = self.get_pages(next_url)
assert previous == [7, 7, 7, 8, 9]
assert current == []
assert next is None
class TestCursorPagination(CursorPaginationTestsMixin):
"""