Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
Openlobby Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
openlobby
Openlobby Server
Commits
18447ab4
Commit
18447ab4
authored
Jan 28, 2018
by
jan.bednarik
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
openlobby/core/api/paginator.py
+8
-2
8 additions, 2 deletions
openlobby/core/api/paginator.py
openlobby/core/tests/test_paginator.py
+13
-8
13 additions, 8 deletions
openlobby/core/tests/test_paginator.py
with
21 additions
and
10 deletions
openlobby/core/api/paginator.py
+
8
−
2
View file @
18447ab4
...
@@ -5,6 +5,10 @@ from graphene.relay import PageInfo
...
@@ -5,6 +5,10 @@ from graphene.relay import PageInfo
PER_PAGE
=
10
PER_PAGE
=
10
class
MissingBeforeValueError
(
Exception
):
pass
def
encode_cursor
(
num
):
def
encode_cursor
(
num
):
return
base64
.
b64encode
(
str
(
num
).
encode
(
'
utf-8
'
)).
decode
(
'
utf-8
'
)
return
base64
.
b64encode
(
str
(
num
).
encode
(
'
utf-8
'
)).
decode
(
'
utf-8
'
)
...
@@ -27,7 +31,9 @@ class Paginator:
...
@@ -27,7 +31,9 @@ class Paginator:
slice_to
=
slice_from
+
first
slice_to
=
slice_from
+
first
elif
last
is
not
None
:
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_to
=
decode_cursor
(
before
)
-
1
slice_from
=
slice_to
-
last
slice_from
=
slice_to
-
last
if
slice_from
<
0
:
if
slice_from
<
0
:
...
...
This diff is collapsed.
Click to expand it.
openlobby/core/tests/test_paginator.py
+
13
−
8
View file @
18447ab4
import
pytest
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
'
,
[
@pytest.mark.parametrize
(
'
num, cursor
'
,
[
...
@@ -41,10 +47,8 @@ def test_paginator__custom_per_page():
...
@@ -41,10 +47,8 @@ def test_paginator__custom_per_page():
@pytest.mark.parametrize
(
'
kw, slice_from, slice_to
'
,
[
@pytest.mark.parametrize
(
'
kw, slice_from, slice_to
'
,
[
({
'
first
'
:
15
},
0
,
15
),
({
'
first
'
:
15
},
0
,
15
),
({
'
first
'
:
5
,
'
after
'
:
encode_cursor
(
3
)},
3
,
8
),
({
'
first
'
:
5
,
'
after
'
:
encode_cursor
(
3
)},
3
,
8
),
({
'
last
'
:
3
},
7
,
10
),
({
'
last
'
:
8
,
'
before
'
:
encode_cursor
(
20
)},
11
,
19
),
({
'
last
'
:
8
,
'
before
'
:
encode_cursor
(
20
)},
11
,
19
),
# overflow of slice_from
# overflow of slice_from
({
'
last
'
:
15
},
0
,
10
),
({
'
last
'
:
100
,
'
before
'
:
encode_cursor
(
42
)},
0
,
41
),
({
'
last
'
:
100
,
'
before
'
:
encode_cursor
(
42
)},
0
,
41
),
# preffer first before last if both provided
# preffer first before last if both provided
({
'
first
'
:
20
,
'
last
'
:
4
},
0
,
20
),
({
'
first
'
:
20
,
'
last
'
:
4
},
0
,
20
),
...
@@ -55,6 +59,11 @@ def test_paginator__input_combinations(kw, slice_from, slice_to):
...
@@ -55,6 +59,11 @@ def test_paginator__input_combinations(kw, slice_from, slice_to):
assert
paginator
.
slice_to
==
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
'
,
[
@pytest.mark.parametrize
(
'
kw, total, previous, next, start, end
'
,
[
({},
10
,
False
,
False
,
1
,
10
),
({},
10
,
False
,
False
,
1
,
10
),
({},
15
,
False
,
True
,
1
,
10
),
({},
15
,
False
,
True
,
1
,
10
),
...
@@ -66,10 +75,6 @@ def test_paginator__input_combinations(kw, slice_from, slice_to):
...
@@ -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
(
7
)},
20
,
True
,
True
,
8
,
10
),
({
'
first
'
:
3
,
'
after
'
:
encode_cursor
(
17
)},
20
,
True
,
False
,
18
,
20
),
({
'
first
'
:
3
,
'
after
'
:
encode_cursor
(
17
)},
20
,
True
,
False
,
18
,
20
),
({
'
first
'
:
5
,
'
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
(
10
)},
20
,
True
,
True
,
6
,
9
),
({
'
last
'
:
4
,
'
before
'
:
encode_cursor
(
5
)},
20
,
False
,
True
,
1
,
4
),
({
'
last
'
:
4
,
'
before
'
:
encode_cursor
(
5
)},
20
,
False
,
True
,
1
,
4
),
({
'
last
'
:
4
,
'
before
'
:
encode_cursor
(
3
)},
20
,
False
,
True
,
1
,
2
),
({
'
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):
...
@@ -89,7 +94,7 @@ def test_paginator__get_page_info(kw, total, previous, next, start, end):
@pytest.mark.parametrize
(
'
kw, num, cursor
'
,
[
@pytest.mark.parametrize
(
'
kw, num, cursor
'
,
[
({},
3
,
encode_cursor
(
3
)),
({},
3
,
encode_cursor
(
3
)),
({
'
first
'
:
6
,
'
after
'
:
encode_cursor
(
1
)},
3
,
encode_cursor
(
4
)),
({
'
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
):
def
test_paginator__get_edge_cursor
(
kw
,
num
,
cursor
):
paginator
=
Paginator
(
**
kw
)
paginator
=
Paginator
(
**
kw
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment