Tests for reverse pagination

This commit is contained in:
Tom Christie 2015-01-22 10:51:04 +00:00
parent cae9528c54
commit f1af603fb0
2 changed files with 73 additions and 27 deletions

View File

@ -408,6 +408,8 @@ def encode_cursor(cursor):
class CursorPagination(BasePagination): class CursorPagination(BasePagination):
# TODO: handle queries with '' as a legitimate position # TODO: handle queries with '' as a legitimate position
# Support case where ordering is already negative
# Support tuple orderings
cursor_query_param = 'cursor' cursor_query_param = 'cursor'
page_size = 5 page_size = 5

View File

@ -436,13 +436,22 @@ class TestCursorPagination:
def __init__(self, items): def __init__(self, items):
self.items = items self.items = items
def filter(self, created__gt): def filter(self, created__gt=None, created__lt=None):
return [ if created__gt is not None:
return MockQuerySet([
item for item in self.items
if item.created > int(created__gt)
])
assert created__lt is not None
return MockQuerySet([
item for item in self.items item for item in self.items
if item.created > int(created__gt) if item.created < int(created__lt)
] ])
def order_by(self, ordering): def order_by(self, ordering):
if ordering.startswith('-'):
return MockQuerySet(reversed(self.items))
return self return self
def __getitem__(self, sliced): def __getitem__(self, sliced):
@ -485,6 +494,25 @@ class TestCursorPagination:
next_url = self.pagination.get_next_link() next_url = self.pagination.get_next_link()
assert next_url is None assert next_url is None
# Now page back again
previous_url = self.pagination.get_previous_link()
assert previous_url
request = Request(factory.get(previous_url))
queryset = self.paginate_queryset(request)
assert [item.created for item in queryset] == [6, 7, 8, 9, 10]
previous_url = self.pagination.get_previous_link()
assert previous_url
request = Request(factory.get(previous_url))
queryset = self.paginate_queryset(request)
assert [item.created for item in queryset] == [1, 2, 3, 4, 5]
previous_url = self.pagination.get_previous_link()
assert previous_url is None
class TestCrazyCursorPagination: class TestCrazyCursorPagination:
""" """
@ -500,13 +528,22 @@ class TestCrazyCursorPagination:
def __init__(self, items): def __init__(self, items):
self.items = items self.items = items
def filter(self, created__gt): def filter(self, created__gt=None, created__lt=None):
return [ if created__gt is not None:
return MockQuerySet([
item for item in self.items
if item.created > int(created__gt)
])
assert created__lt is not None
return MockQuerySet([
item for item in self.items item for item in self.items
if item.created > int(created__gt) if item.created < int(created__lt)
] ])
def order_by(self, ordering): def order_by(self, ordering):
if ordering.startswith('-'):
return MockQuerySet(reversed(self.items))
return self return self
def __getitem__(self, sliced): def __getitem__(self, sliced):
@ -553,25 +590,32 @@ class TestCrazyCursorPagination:
next_url = self.pagination.get_next_link() next_url = self.pagination.get_next_link()
assert next_url is None assert next_url is None
# assert content == {
# 'results': [1, 2, 3, 4, 5], # Now page back again
# 'previous': None,
# 'next': 'http://testserver/?limit=5&offset=5', previous_url = self.pagination.get_previous_link()
# 'count': 100 assert previous_url
# }
# assert context == { request = Request(factory.get(previous_url))
# 'previous_url': None, queryset = self.paginate_queryset(request)
# 'next_url': 'http://testserver/?limit=5&offset=5', assert [item.created for item in queryset] == [1, 1, 2, 3, 4]
# 'page_links': [
# PageLink('http://testserver/?limit=5', 1, True, False), previous_url = self.pagination.get_previous_link()
# PageLink('http://testserver/?limit=5&offset=5', 2, False, False), assert previous_url
# PageLink('http://testserver/?limit=5&offset=10', 3, False, False),
# PAGE_BREAK, request = Request(factory.get(previous_url))
# PageLink('http://testserver/?limit=5&offset=95', 20, False, False), queryset = self.paginate_queryset(request)
# ] assert [item.created for item in queryset] == [1, 1, 1, 1, 1]
# }
# assert self.pagination.display_page_controls previous_url = self.pagination.get_previous_link()
# assert isinstance(self.pagination.to_html(), type('')) assert previous_url
request = Request(factory.get(previous_url))
queryset = self.paginate_queryset(request)
assert [item.created for item in queryset] == [1, 1, 1, 1, 1]
previous_url = self.pagination.get_previous_link()
assert previous_url is None
def test_get_displayed_page_numbers(): def test_get_displayed_page_numbers():