Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception message for invalid filter result #943

Merged
merged 2 commits into from
Jul 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
@@ -179,6 +179,9 @@ def filter_queryset(self, queryset):
"""
for name, value in self.form.cleaned_data.items():
queryset = self.filters[name].filter(queryset, value)
assert isinstance(queryset, models.QuerySet), \
"Expected '%s.%s' to return a QuerySet, but got a %s instead." \
% (type(self).__name__, name, type(queryset).__name__)
return queryset

@property
7 changes: 3 additions & 4 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
SpacewalkRecord,
User
)
from .utils import MockQuerySet


class CharFilterTests(TestCase):
@@ -1869,10 +1870,8 @@ class Meta:
model = User
fields = ['account']

qs = mock.NonCallableMagicMock()
f = F({'account': 'jdoe'}, queryset=qs)
result = f.qs
self.assertNotEqual(qs, result)
qs = MockQuerySet()
F({'account': 'jdoe'}, queryset=qs).qs
qs.all.return_value.filter.assert_called_with(username__exact='jdoe')

def test_filtering_without_meta(self):
20 changes: 16 additions & 4 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
UUIDTestModel,
Worker
)
from .utils import MockQuerySet


def checkItemsEqual(L1, L2):
@@ -634,12 +635,14 @@ def test_creating_with_request(self):
class FilterSetQuerysetTests(TestCase):

class F(FilterSet):
invalid = CharFilter(method=lambda *args: None)

class Meta:
model = User
fields = ['username']
fields = ['username', 'invalid']

def test_filter_queryset_called_once(self):
m = mock.Mock()
m = MockQuerySet()
f = self.F({'username': 'bob'}, queryset=m)

with mock.patch.object(f, 'filter_queryset',
@@ -672,7 +675,7 @@ def test_form_caching(self):
self.assertIs(f.form, f.form)

def test_qs_triggers_form_validation(self):
m = mock.Mock()
m = MockQuerySet()
f = self.F({'username': 'bob'}, queryset=m)

with mock.patch.object(f.form, 'full_clean',
@@ -681,6 +684,14 @@ def test_qs_triggers_form_validation(self):
f.qs
fn.assert_called()

def test_filters_must_return_queryset(self):
m = MockQuerySet()
f = self.F({'invalid': 'result'}, queryset=m)

msg = "Expected 'F.invalid' to return a QuerySet, but got a NoneType instead."
with self.assertRaisesMessage(AssertionError, msg):
f.qs


# test filter.method here, as it depends on its parent FilterSet
class FilterMethodTests(TestCase):
@@ -759,7 +770,7 @@ def test_parent_unresolvable(self):
def test_method_self_is_parent(self):
# Ensure the method isn't 're-parented' on the `FilterMethod` helper class.
# Filter methods should have access to the filterset's properties.
request = mock.Mock()
request = MockQuerySet()

class F(FilterSet):
f = CharFilter(method='filter_f')
@@ -771,6 +782,7 @@ class Meta:
def filter_f(inner_self, qs, name, value):
self.assertIsInstance(inner_self, F)
self.assertIs(inner_self.request, request)
return qs

F({'f': 'foo'}, request=request, queryset=User.objects.all()).qs

15 changes: 15 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from unittest import mock

from django.db import models


class MockQuerySet:
"""
Generate a mock that is suitably similar to a QuerySet
"""

def __new__(self):
m = mock.Mock(spec_set=models.QuerySet())
m.filter.return_value = m
m.all.return_value = m
return m