diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 539bf8b11..044407e72 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -2,7 +2,6 @@ Pagination serializers determine the structure of the output that should be used for paginated responses. """ -import json import operator from base64 import b64decode, b64encode @@ -21,6 +20,7 @@ from rest_framework.compat import coreapi, coreschema from rest_framework.exceptions import NotFound from rest_framework.response import Response from rest_framework.settings import api_settings +from rest_framework.utils import json from rest_framework.utils.urls import remove_query_param, replace_query_param diff --git a/tests/test_cursor_pagination.py b/tests/test_cursor_pagination.py index d267dca07..f655cc97f 100644 --- a/tests/test_cursor_pagination.py +++ b/tests/test_cursor_pagination.py @@ -1,11 +1,9 @@ import base64 import itertools -import re from base64 import b64encode from urllib import parse import pytest -from django.db import models from rest_framework import generics from rest_framework.pagination import Cursor, CursorPagination from rest_framework.filters import OrderingFilter @@ -41,12 +39,11 @@ def create_cursor(offset, reverse, position): def decode_cursor(response): - links = { 'next': response.data.get('next'), 'prev': response.data.get('prev'), } - + cursors = {} for rel, link in links.items(): @@ -84,9 +81,11 @@ def decode_cursor(response): @pytest.mark.django_db -@pytest.mark.parametrize("page_size,offset", [ - (6, 2), (2, 6), (5, 3), (3, 5), (5, 5) -], +@pytest.mark.parametrize( + "page_size,offset", + [ + (6, 2), (2, 6), (5, 3), (3, 5), (5, 5) + ], ids=[ 'page_size_divisor_of_offset', 'page_size_multiple_of_offset', @@ -179,7 +178,7 @@ def test_filtered_items_are_paginated(page_size, offset): while next_cursor: assert ( - expected_result[position : position + len(response.data['results'])] == response.data['results'] + expected_result[position: position + len(response.data['results'])] == response.data['results'] ) position += len(response.data['results']) response = _request(*next_cursor) @@ -190,7 +189,7 @@ def test_filtered_items_are_paginated(page_size, offset): while prev_cursor: assert ( - expected_result[position - len(response.data['results']) : position] == response.data['results'] + expected_result[position - len(response.data['results']): position] == response.data['results'] ) position -= len(response.data['results']) response = _request(*prev_cursor) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index b1163345f..55c4e8833 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -4,7 +4,6 @@ import re import operator from unittest.mock import Mock -from functools import reduce from django.core.paginator import Paginator as DjangoPaginator from django.db.models.query_utils import Q @@ -34,7 +33,6 @@ class MockQuerySet: q_object = Q(**kwargs) query = self._q_object_to_expression(q_object) - res = query(self.items[0]) return MockQuerySet([ item for item in self.items @@ -48,15 +46,14 @@ class MockQuerySet: for param in ordering: if param.startswith('-'): - ordering_params.append(0 - getattr(item,param[1:])) + ordering_params.append(0 - getattr(item, param[1:])) else: - ordering_params.append(getattr(item,param)) + ordering_params.append(getattr(item, param)) return tuple(ordering_params) return MockQuerySet(list(sorted(self.items, key=_ordering_callable))) - def __getitem__(self, sliced): return self.items[sliced] @@ -73,10 +70,13 @@ class MockQuerySet: def _parse(_q_object): _statements = [] - for child in _q_object.children: if isinstance(child, Q): - return [lambda item: operator_map[child.connector](l(item) for l in _parse(child))] + return [ + lambda item: operator_map[child.connector]( + lmbda(item) for lmbda in _parse(child) + ) + ] match = self._operator_match.match(child[0]).groupdict() field, field_op = match['field'], match['operator'] @@ -87,12 +87,15 @@ class MockQuerySet: field_op = operator_map[field_op] value = child[1] - + _statements.append(lambda item: field_op(getattr(item, field), int(value))) return _statements - return lambda item: operator_map[q_object.connector](l(item) for l in _parse(q_object)) + return lambda item: operator_map[q_object.connector]( + lmbda(item) for lmbda in _parse(q_object) + ) + class TestPaginationIntegration: """ @@ -733,7 +736,7 @@ class CursorPaginationTestsMixin: (previous, current, next, previous_url, next_url) = self.get_pages(next_url) - #assert previous == [4, 4, 4, 5, 6] # Paging artifact + # assert previous == [4, 4, 4, 5, 6] # Paging artifact assert current == [7, 7, 7, 7, 7] assert next == [7, 7, 7, 8, 9] @@ -893,7 +896,7 @@ class CursorPaginationTestsMixin: (previous, current, next, previous_url, next_url) = self.get_pages(next_url) - #assert previous == [4, 4, 4, 5, 6] # Paging artifact + # assert previous == [4, 4, 4, 5, 6] # Paging artifact assert current == [7, 7, 7, 7, 7] assert next == [7, 7, 7, 8, 9] @@ -919,7 +922,7 @@ class CursorPaginationTestsMixin: assert previous == [4, 4, 5, 6, 7] assert current == [7, 7, 7, 7, 7] - #assert next == [8, 9, 9, 9, 9] # Paging artifact + # assert next == [8, 9, 9, 9, 9] # Paging artifact (previous, current, next, previous_url, next_url) = self.get_pages(previous_url) @@ -1158,7 +1161,6 @@ class TestCursorPaginationWithValueQueryset(CursorPaginationTestsMixin, TestCase assert next_url is None - def test_get_displayed_page_numbers(): """ Test our contextual page display function.