Skip to content

Commit

Permalink
Add complex filtering + pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan P Kilby committed Dec 23, 2017
1 parent abbc7fa commit 59f0839
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
38 changes: 37 additions & 1 deletion tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def setUpTestData(cls):
models.User.objects.create(username="user1", email="[email protected]")
models.User.objects.create(username="user2", email="[email protected]")
models.User.objects.create(username="user3", email="[email protected]")
models.User.objects.create(username="user4", email="[email protected]")

def test_valid(self):
readable = '(username%3Duser1)|(email__contains%3Dexample.org)'
Expand All @@ -119,7 +120,7 @@ def test_valid(self):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertListEqual(
[r['username'] for r in response.data],
['user1', 'user3']
['user1', 'user3', 'user4']
)

def test_invalid(self):
Expand All @@ -130,3 +131,38 @@ def test_invalid(self):
self.assertDictEqual(response.data, {
'filters': ["Invalid querystring operator. Matched: 'asdf'."],
})

def test_pagination_compatibility(self):
"""
Ensure that complex-filtering does not interfere with additional query param processing.
"""
readable = '(email__contains%3Dexample.org)'

# sanity check w/o pagination
response = self.client.get('/ffcomplex-users/?filters=' + quote(readable), content_type='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertListEqual(
[r['username'] for r in response.data],
['user3', 'user4']
)

# sanity check w/o complex-filtering
response = self.client.get('/ffcomplex-users/?page_size=1', content_type='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('results', response.data)
self.assertListEqual(
[r['username'] for r in response.data['results']],
['user1']
)

# pagination + complex-filtering
response = self.client.get('/ffcomplex-users/?page_size=1&filters=' + quote(readable), content_type='json')

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn('results', response.data)
self.assertListEqual(
[r['username'] for r in response.data['results']],
['user3']
)
5 changes: 4 additions & 1 deletion tests/testapp/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from rest_framework import viewsets
from rest_framework import pagination, viewsets
from rest_framework_filters import backends

from .models import User, Note
Expand Down Expand Up @@ -33,6 +33,9 @@ class ComplexFilterFieldsUserViewSet(FilterFieldsUserViewSet):
'email': '__all__',
}

class pagination_class(pagination.PageNumberPagination):
page_size_query_param = 'page_size'


class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
Expand Down

0 comments on commit 59f0839

Please sign in to comment.