This commit is contained in:
Rollo Konig Brock 2021-03-31 16:27:23 +01:00
parent 21710814d7
commit 91f9677f57
3 changed files with 24 additions and 23 deletions

View File

@ -2,7 +2,6 @@
Pagination serializers determine the structure of the output that should Pagination serializers determine the structure of the output that should
be used for paginated responses. be used for paginated responses.
""" """
import json
import operator import operator
from base64 import b64decode, b64encode 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.exceptions import NotFound
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.settings import api_settings 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 from rest_framework.utils.urls import remove_query_param, replace_query_param

View File

@ -1,11 +1,9 @@
import base64 import base64
import itertools import itertools
import re
from base64 import b64encode from base64 import b64encode
from urllib import parse from urllib import parse
import pytest import pytest
from django.db import models
from rest_framework import generics from rest_framework import generics
from rest_framework.pagination import Cursor, CursorPagination from rest_framework.pagination import Cursor, CursorPagination
from rest_framework.filters import OrderingFilter from rest_framework.filters import OrderingFilter
@ -41,7 +39,6 @@ def create_cursor(offset, reverse, position):
def decode_cursor(response): def decode_cursor(response):
links = { links = {
'next': response.data.get('next'), 'next': response.data.get('next'),
'prev': response.data.get('prev'), 'prev': response.data.get('prev'),
@ -84,9 +81,11 @@ def decode_cursor(response):
@pytest.mark.django_db @pytest.mark.django_db
@pytest.mark.parametrize("page_size,offset", [ @pytest.mark.parametrize(
"page_size,offset",
[
(6, 2), (2, 6), (5, 3), (3, 5), (5, 5) (6, 2), (2, 6), (5, 3), (3, 5), (5, 5)
], ],
ids=[ ids=[
'page_size_divisor_of_offset', 'page_size_divisor_of_offset',
'page_size_multiple_of_offset', 'page_size_multiple_of_offset',
@ -179,7 +178,7 @@ def test_filtered_items_are_paginated(page_size, offset):
while next_cursor: while next_cursor:
assert ( 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']) position += len(response.data['results'])
response = _request(*next_cursor) response = _request(*next_cursor)
@ -190,7 +189,7 @@ def test_filtered_items_are_paginated(page_size, offset):
while prev_cursor: while prev_cursor:
assert ( 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']) position -= len(response.data['results'])
response = _request(*prev_cursor) response = _request(*prev_cursor)

View File

@ -4,7 +4,6 @@ import re
import operator import operator
from unittest.mock import Mock from unittest.mock import Mock
from functools import reduce
from django.core.paginator import Paginator as DjangoPaginator from django.core.paginator import Paginator as DjangoPaginator
from django.db.models.query_utils import Q from django.db.models.query_utils import Q
@ -34,7 +33,6 @@ class MockQuerySet:
q_object = Q(**kwargs) q_object = Q(**kwargs)
query = self._q_object_to_expression(q_object) query = self._q_object_to_expression(q_object)
res = query(self.items[0])
return MockQuerySet([ return MockQuerySet([
item for item in self.items item for item in self.items
@ -48,15 +46,14 @@ class MockQuerySet:
for param in ordering: for param in ordering:
if param.startswith('-'): if param.startswith('-'):
ordering_params.append(0 - getattr(item,param[1:])) ordering_params.append(0 - getattr(item, param[1:]))
else: else:
ordering_params.append(getattr(item,param)) ordering_params.append(getattr(item, param))
return tuple(ordering_params) return tuple(ordering_params)
return MockQuerySet(list(sorted(self.items, key=_ordering_callable))) return MockQuerySet(list(sorted(self.items, key=_ordering_callable)))
def __getitem__(self, sliced): def __getitem__(self, sliced):
return self.items[sliced] return self.items[sliced]
@ -73,10 +70,13 @@ class MockQuerySet:
def _parse(_q_object): def _parse(_q_object):
_statements = [] _statements = []
for child in _q_object.children: for child in _q_object.children:
if isinstance(child, Q): 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() match = self._operator_match.match(child[0]).groupdict()
field, field_op = match['field'], match['operator'] field, field_op = match['field'], match['operator']
@ -92,7 +92,10 @@ class MockQuerySet:
return _statements 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: class TestPaginationIntegration:
""" """
@ -733,7 +736,7 @@ class CursorPaginationTestsMixin:
(previous, current, next, previous_url, next_url) = self.get_pages(next_url) (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 current == [7, 7, 7, 7, 7]
assert next == [7, 7, 7, 8, 9] 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) (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 current == [7, 7, 7, 7, 7]
assert next == [7, 7, 7, 8, 9] assert next == [7, 7, 7, 8, 9]
@ -919,7 +922,7 @@ class CursorPaginationTestsMixin:
assert previous == [4, 4, 5, 6, 7] assert previous == [4, 4, 5, 6, 7]
assert current == [7, 7, 7, 7, 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) (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 assert next_url is None
def test_get_displayed_page_numbers(): def test_get_displayed_page_numbers():
""" """
Test our contextual page display function. Test our contextual page display function.