Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In LimitOffsetPagination limit=0 should revert to default limit. #4194

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rest_framework/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ def get_limit(self, request):
try:
return _positive_int(
request.query_params[self.limit_query_param],
strict=True,
cutoff=self.max_limit
)
except (KeyError, ValueError):
Expand Down
38 changes: 13 additions & 25 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ def test_invalid_limit(self):
assert queryset == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert content.get('next') == next_url

def test_zero_limit(self):
"""
An zero limit query param should be ignored in favor of the default.
"""
request = Request(factory.get('/', {'limit': 0, 'offset': 0}))
queryset = self.paginate_queryset(request)
content = self.get_paginated_content(queryset)
next_limit = self.pagination.default_limit
next_offset = self.pagination.default_limit
next_url = 'http://testserver/?limit={0}&offset={1}'.format(next_limit, next_offset)
assert queryset == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
assert content.get('next') == next_url

def test_max_limit(self):
"""
The limit defaults to the max_limit when there is a max_limit and the
Expand All @@ -505,31 +518,6 @@ def test_max_limit(self):
assert content.get('next') == next_url
assert content.get('previous') == prev_url

def test_limit_zero(self):
"""
A limit of 0 should return empty results.
"""
request = Request(factory.get('/', {'limit': 0, 'offset': 10}))
queryset = self.paginate_queryset(request)
context = self.get_html_context()
content = self.get_paginated_content(queryset)

assert context == {
'previous_url': 'http://testserver/?limit=0&offset=10',
'page_links': [
PageLink(
url='http://testserver/?limit=0',
number=1,
is_active=True,
is_break=False
)
],
'next_url': 'http://testserver/?limit=0&offset=10'
}

assert queryset == []
assert content.get('results') == []


class TestCursorPagination:
"""
Expand Down