Skip to content

Commit 21166a3

Browse files
archy-rock3t-cloudtomchristie
authored andcommitted
Add couple of tests for filters (#4849)
1 parent 48b5aa7 commit 21166a3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_filters.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import warnings
66
from decimal import Decimal
77

8+
import pytest
89
from django.conf.urls import url
910
from django.core.exceptions import ImproperlyConfigured
1011
from django.db import models
@@ -119,6 +120,27 @@ def get_queryset(self):
119120
]
120121

121122

123+
class BaseFilterTests(TestCase):
124+
def setUp(self):
125+
self.original_coreapi = filters.coreapi
126+
filters.coreapi = True # mock it, because not None value needed
127+
self.filter_backend = filters.BaseFilterBackend()
128+
129+
def tearDown(self):
130+
filters.coreapi = self.original_coreapi
131+
132+
def test_filter_queryset_raises_error(self):
133+
with pytest.raises(NotImplementedError):
134+
self.filter_backend.filter_queryset(None, None, None)
135+
136+
def test_get_schema_fields_checks_for_coreapi(self):
137+
filters.coreapi = None
138+
with pytest.raises(AssertionError):
139+
self.filter_backend.get_schema_fields({})
140+
filters.coreapi = True
141+
assert self.filter_backend.get_schema_fields({}) == []
142+
143+
122144
class CommonFilteringTestCase(TestCase):
123145
def _serialize_object(self, obj):
124146
return {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date.isoformat()}
@@ -429,6 +451,19 @@ class SearchListView(generics.ListAPIView):
429451
{'id': 2, 'title': 'zz', 'text': 'bcd'}
430452
]
431453

454+
def test_search_returns_same_queryset_if_no_search_fields_or_terms_provided(self):
455+
class SearchListView(generics.ListAPIView):
456+
queryset = SearchFilterModel.objects.all()
457+
serializer_class = SearchFilterSerializer
458+
filter_backends = (filters.SearchFilter,)
459+
460+
view = SearchListView.as_view()
461+
request = factory.get('/')
462+
response = view(request)
463+
expected = SearchFilterSerializer(SearchFilterModel.objects.all(),
464+
many=True).data
465+
assert response.data == expected
466+
432467
def test_exact_search(self):
433468
class SearchListView(generics.ListAPIView):
434469
queryset = SearchFilterModel.objects.all()

0 commit comments

Comments
 (0)