mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 21:24:33 +03:00
Add couple of tests for filters (#4849)
This commit is contained in:
parent
48b5aa71f2
commit
21166a3ab6
|
@ -5,6 +5,7 @@ import unittest
|
|||
import warnings
|
||||
from decimal import Decimal
|
||||
|
||||
import pytest
|
||||
from django.conf.urls import url
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import models
|
||||
|
@ -119,6 +120,27 @@ if django_filters:
|
|||
]
|
||||
|
||||
|
||||
class BaseFilterTests(TestCase):
|
||||
def setUp(self):
|
||||
self.original_coreapi = filters.coreapi
|
||||
filters.coreapi = True # mock it, because not None value needed
|
||||
self.filter_backend = filters.BaseFilterBackend()
|
||||
|
||||
def tearDown(self):
|
||||
filters.coreapi = self.original_coreapi
|
||||
|
||||
def test_filter_queryset_raises_error(self):
|
||||
with pytest.raises(NotImplementedError):
|
||||
self.filter_backend.filter_queryset(None, None, None)
|
||||
|
||||
def test_get_schema_fields_checks_for_coreapi(self):
|
||||
filters.coreapi = None
|
||||
with pytest.raises(AssertionError):
|
||||
self.filter_backend.get_schema_fields({})
|
||||
filters.coreapi = True
|
||||
assert self.filter_backend.get_schema_fields({}) == []
|
||||
|
||||
|
||||
class CommonFilteringTestCase(TestCase):
|
||||
def _serialize_object(self, obj):
|
||||
return {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date.isoformat()}
|
||||
|
@ -429,6 +451,19 @@ class SearchFilterTests(TestCase):
|
|||
{'id': 2, 'title': 'zz', 'text': 'bcd'}
|
||||
]
|
||||
|
||||
def test_search_returns_same_queryset_if_no_search_fields_or_terms_provided(self):
|
||||
class SearchListView(generics.ListAPIView):
|
||||
queryset = SearchFilterModel.objects.all()
|
||||
serializer_class = SearchFilterSerializer
|
||||
filter_backends = (filters.SearchFilter,)
|
||||
|
||||
view = SearchListView.as_view()
|
||||
request = factory.get('/')
|
||||
response = view(request)
|
||||
expected = SearchFilterSerializer(SearchFilterModel.objects.all(),
|
||||
many=True).data
|
||||
assert response.data == expected
|
||||
|
||||
def test_exact_search(self):
|
||||
class SearchListView(generics.ListAPIView):
|
||||
queryset = SearchFilterModel.objects.all()
|
||||
|
|
Loading…
Reference in New Issue
Block a user