Skip to content

Commit

Permalink
Add check for invalid behavior value, add and clean up various relate…
Browse files Browse the repository at this point in the history
…d tests
  • Loading branch information
loeeess committed Jul 11, 2024
1 parent f5b6fc6 commit 167c506
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions django_filters/filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ def handle_unrecognized_field(cls, field_name, message):
warnings.warn(f"Unrecognized field type for '{field_name}'. Field will be ignored.")
elif behavior == UnknownFieldBehavior.IGNORE:
pass
else:
raise ValueError(f"Invalid unknown_field_behavior: {behavior}")

@classmethod
def filter_for_field(cls, field, field_name, lookup_expr=None):
Expand Down
35 changes: 26 additions & 9 deletions tests/test_filterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
UUIDFilter,
)
from django_filters.filterset import (
UnknownFieldBehavior,
FILTER_FOR_DBFIELD_DEFAULTS,
FilterSet,
UnknownFieldBehavior,
filterset_factory,
)
from django_filters.widgets import BooleanWidget
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_unknown_field_type_error(self):
def test_return_none(self):
f = NetworkSetting._meta.get_field("mask")
# Set unknown_field_behavior to 'ignore' to avoid raising exceptions
FilterSet._meta.unknown_field_behavior = "ignore"
FilterSet._meta.unknown_field_behavior = UnknownFieldBehavior.IGNORE
result = FilterSet.filter_for_field(f, "mask")

self.assertIsNone(result)
Expand Down Expand Up @@ -212,6 +212,7 @@ def test_modified_default_lookup(self):
def test_filter_overrides(self):
pass


class HandleUnknownFieldTests(TestCase):
def setUp(self):
class NetworkSettingFilterSet(FilterSet):
Expand All @@ -226,28 +227,33 @@ class Meta:
def test_raise_unknown_field_behavior(self):
self.FilterSet._meta.unknown_field_behavior = UnknownFieldBehavior.RAISE

with self.assertRaises(AssertionError):
with self.assertRaises(AssertionError) as excinfo:
self.FilterSet.handle_unrecognized_field("mask", "test_message")

self.assertIn(
"test_message",
excinfo.exception.args[0],
)

def test_unknown_field_warn_behavior(self):
self.FilterSet._meta.unknown_field_behavior = UnknownFieldBehavior.WARN

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
self.FilterSet.handle_unrecognized_field("mask", "test_message")

self.assertIn(
"Unrecognized field type for 'mask'. "
"Field will be ignored.",
str(w[-1].message),
)
self.assertIn(
"Unrecognized field type for 'mask'. "
"Field will be ignored.",
str(w[-1].message),
)

def test_unknown_field_ignore_behavior(self):
# No exception or warning should be raised
self.FilterSet._meta.unknown_field_behavior = UnknownFieldBehavior.IGNORE
self.FilterSet.handle_unrecognized_field("mask", "test_message")

def test_unknown_field_invalid_behavior(self):
def test_unknown_field_invalid_initial_behavior(self):
# Creation of new custom FilterSet to set initial field behavior
with self.assertRaises(ValueError) as excinfo:

Expand All @@ -262,6 +268,17 @@ class Meta:
str(excinfo.exception),
)

def test_unknown_field_invalid_changed_option_behavior(self):
self.FilterSet._meta.unknown_field_behavior = "invalid"

with self.assertRaises(ValueError) as excinfo:
self.FilterSet.handle_unrecognized_field("mask", "test_message")

self.assertIn(
"Invalid unknown_field_behavior: invalid",
str(excinfo.exception),
)


class FilterSetFilterForLookupTests(TestCase):
def test_filter_for_ISNULL_lookup(self):
Expand Down

0 comments on commit 167c506

Please sign in to comment.