Skip to content
Snippets Groups Projects
Commit 18447ab4 authored by jan.bednarik's avatar jan.bednarik
Browse files

Paginator works for 'last' only if it's used with 'before'.

parent 50471b43
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,10 @@ from graphene.relay import PageInfo
PER_PAGE = 10
class MissingBeforeValueError(Exception):
pass
def encode_cursor(num):
return base64.b64encode(str(num).encode('utf-8')).decode('utf-8')
......@@ -27,7 +31,9 @@ class Paginator:
slice_to = slice_from + first
elif last is not None:
if before is not None:
if before is None:
raise MissingBeforeValueError('Pagination "last" works only in combination with "before" argument.')
slice_to = decode_cursor(before) - 1
slice_from = slice_to - last
if slice_from < 0:
......
import pytest
from ..api.paginator import PER_PAGE, encode_cursor, decode_cursor, Paginator
from ..api.paginator import (
PER_PAGE,
encode_cursor,
decode_cursor,
Paginator,
MissingBeforeValueError,
)
@pytest.mark.parametrize('num, cursor', [
......@@ -41,10 +47,8 @@ def test_paginator__custom_per_page():
@pytest.mark.parametrize('kw, slice_from, slice_to', [
({'first': 15}, 0, 15),
({'first': 5, 'after': encode_cursor(3)}, 3, 8),
({'last': 3}, 7, 10),
({'last': 8, 'before': encode_cursor(20)}, 11, 19),
# overflow of slice_from
({'last': 15}, 0, 10),
({'last': 100, 'before': encode_cursor(42)}, 0, 41),
# preffer first before last if both provided
({'first': 20, 'last': 4}, 0, 20),
......@@ -55,6 +59,11 @@ def test_paginator__input_combinations(kw, slice_from, slice_to):
assert paginator.slice_to == slice_to
def test_paginator__last_without_before():
with pytest.raises(MissingBeforeValueError):
Paginator(last=1)
@pytest.mark.parametrize('kw, total, previous, next, start, end', [
({}, 10, False, False, 1, 10),
({}, 15, False, True, 1, 10),
......@@ -66,10 +75,6 @@ def test_paginator__input_combinations(kw, slice_from, slice_to):
({'first': 3, 'after': encode_cursor(7)}, 20, True, True, 8, 10),
({'first': 3, 'after': encode_cursor(17)}, 20, True, False, 18, 20),
({'first': 5, 'after': encode_cursor(17)}, 20, True, False, 18, 20),
({'last': 1}, 10, True, False, 10, 10),
({'last': 5}, 10, True, False, 6, 10),
({'last': 15}, 10, False, False, 1, 10),
({'last': 10}, 10, False, False, 1, 10),
({'last': 4, 'before': encode_cursor(10)}, 20, True, True, 6, 9),
({'last': 4, 'before': encode_cursor(5)}, 20, False, True, 1, 4),
({'last': 4, 'before': encode_cursor(3)}, 20, False, True, 1, 2),
......@@ -89,7 +94,7 @@ def test_paginator__get_page_info(kw, total, previous, next, start, end):
@pytest.mark.parametrize('kw, num, cursor', [
({}, 3, encode_cursor(3)),
({'first': 6, 'after': encode_cursor(1)}, 3, encode_cursor(4)),
({'last': 6}, 3, encode_cursor(7)),
({'last': 6, 'before': encode_cursor(11)}, 3, encode_cursor(7)),
])
def test_paginator__get_edge_cursor(kw, num, cursor):
paginator = Paginator(**kw)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment