def test_filtering_skipped_with_list_value_with_blank_lookup(self): return # Now field is required to provide valid lookup_type if it provides any qs = mock.Mock(spec=['filter']) f = Filter(name='somefield', lookup_type=None) result = f.filter(qs, Lookup('value', '')) qs.filter.assert_called_once_with(somefield__exact='value') self.assertNotEqual(qs, result)
def test_filter_using_action(self): qs = mock.NonCallableMock(spec=[]) action = mock.Mock(spec=['filter']) f = Filter(action=action) result = f.filter(qs, 'value') action.assert_called_once_with(qs, 'value') self.assertNotEqual(qs, result)
def test_filter_using_method(self): qs = mock.NonCallableMock(spec=[]) method = mock.Mock() f = Filter(method=method) result = f.filter(qs, 'value') method.assert_called_once_with(qs, None, 'value') self.assertNotEqual(qs, result)
def test_parent_unresolvable(self): f = Filter(method='filter_f') with self.assertRaises(AssertionError) as w: f.filter(User.objects.all(), 0) msg = "Filter 'None' must have a parent FilterSet to find '.filter_f()'." self.assertEqual(str(w.exception), msg)
def test_parent_unresolvable(self): f = Filter(method='filter_f') with self.assertRaises(AssertionError) as w: f.filter(User.objects.all(), 0) self.assertIn("'None'", str(w.exception)) self.assertIn('parent', str(w.exception)) self.assertIn('filter_f', str(w.exception))
def test_filter_using_action(self): qs = mock.NonCallableMock(spec=[]) action = mock.Mock(spec=['filter']) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") f = Filter(action=action) result = f.filter(qs, 'value') action.assert_called_once_with(qs, 'value') self.assertNotEqual(qs, result) self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
def test_field_with_required_filter(self): with mock.patch.object(Filter, 'field_class', spec=['__call__']) as mocked: f = Filter(required=True) f.field mocked.assert_called_once_with(required=True, label=mock.ANY, widget=mock.ANY, help_text=mock.ANY)
def test_field_extra_params(self): with mock.patch.object(Filter, 'field_class', spec=['__call__']) as mocked: f = Filter(someattr='someattr') f.field mocked.assert_called_once_with(required=mock.ANY, label=mock.ANY, widget=mock.ANY, someattr='someattr')
def test_field_params(self): with mock.patch.object(Filter, 'field_class', spec=['__call__']) as mocked: f = Filter(name='somefield', label='somelabel', widget='somewidget') f.field mocked.assert_called_once_with(required=False, label='somelabel', widget='somewidget', help_text=mock.ANY)
def test_field_required_default(self): # filter form fields should not be required by default with mock.patch.object(Filter, 'field_class', spec=['__call__']) as mocked: f = Filter() f.field mocked.assert_called_once_with(required=False, label=mock.ANY)
def test_lookup_type_deprecation(self): """ Make sure user is alerted when using deprecated ``lookup_type``. """ with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") Filter(lookup_type='exact') self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
def test_custom_lookup_exprs(self): filters.LOOKUP_TYPES = [ ('', '---------'), ('exact', 'Is equal to'), ('not_exact', 'Is not equal to'), ('lt', 'Lesser than'), ('gt', 'Greater than'), ('gte', 'Greater than or equal to'), ('lte', 'Lesser than or equal to'), ('startswith', 'Starts with'), ('endswith', 'Ends with'), ('contains', 'Contains'), ('not_contains', 'Does not contain'), ] f = Filter(lookup_expr=None) field = f.field choice_field = field.fields[1] all_choices = choice_field.choices self.assertIsInstance(field, LookupTypeField) self.assertEqual(all_choices, filters.LOOKUP_TYPES) self.assertEqual(all_choices[1][0], 'exact') self.assertEqual(all_choices[1][1], 'Is equal to') custom_f = Filter(lookup_expr=('endswith', 'not_contains')) custom_field = custom_f.field custom_choice_field = custom_field.fields[1] my_custom_choices = custom_choice_field.choices available_lookup_exprs = [ ('endswith', 'Ends with'), ('not_contains', 'Does not contain'), ] self.assertIsInstance(custom_field, LookupTypeField) self.assertEqual(my_custom_choices, available_lookup_exprs) self.assertEqual(my_custom_choices[0][0], 'endswith') self.assertEqual(my_custom_choices[0][1], 'Ends with') self.assertEqual(my_custom_choices[1][0], 'not_contains') self.assertEqual(my_custom_choices[1][1], 'Does not contain')
def replace_csv_filters(filterset_class): """ Replace the "in" and "range" filters (that are not explicitly declared) to not be BaseCSVFilter (BaseInFilter, BaseRangeFilter) objects anymore but regular Filter objects that simply use the input value as filter argument on the queryset. This is because those BaseCSVFilter are expecting a string as input with comma separated value but with GraphQl we can actually have a list as input and have a proper type verification of each value in the list. See issue https://github.com/graphql-python/graphene-django/issues/1068. """ for name, filter_field in list(filterset_class.base_filters.items()): filter_type = filter_field.lookup_expr if filter_type in ["in", "range"]: assert isinstance(filter_field, BaseCSVFilter) filterset_class.base_filters[name] = Filter( field_name=filter_field.field_name, lookup_expr=filter_field.lookup_expr, label=filter_field.label, method=filter_field.method, exclude=filter_field.exclude, **filter_field.extra)
def get_filters(cls): """ Retrieve a list of filters from this object's meta property :param django_filters.filterset.FilterSetMetaclass cls: A class instance :return: A filter dictionary :rtype: dict """ if not cls._meta.fields: return {} filters = OrderedDict() fields = cls.get_fields() for field_name, lookups in fields.items(): for lookup_expr in lookups: filter_name = cls.get_filter_name(field_name, lookup_expr) filters[filter_name] = Filter() return filters
def test_custom_lookup_types(self): filters.LOOKUP_TYPES = [ ('', '---------'), ('exact', 'Is equal to'), ('not_exact', 'Is not equal to'), ('lt', 'Lesser than'), ('gt', 'Greater than'), ('gte', 'Greater than or equal to'), ('lte', 'Lesser than or equal to'), ('startswith', 'Starts with'), ('endswith', 'Ends with'), ('contains', 'Contains'), ('not_contains', 'Does not contain'), ] f = Filter(lookup_type=None) field = f.field choice_field = field.fields[1] choices = choice_field.choices self.assertIsInstance(field, LookupTypeField) self.assertEqual(choices, filters.LOOKUP_TYPES) self.assertEqual(choices[1][0], 'exact') self.assertEqual(choices[1][1], 'Is equal to')
def test_filtering_with_list_value(self): qs = mock.Mock(spec=['filter']) f = Filter(name='somefield', lookup_type=['some_lookup_type']) result = f.filter(qs, Lookup('value', 'some_lookup_type')) qs.filter.assert_called_once_with(somefield__some_lookup_type='value') self.assertNotEqual(qs, result)
def test_filtering_skipped_with_list_value_with_blank(self): qs = mock.Mock() f = Filter(name='somefield', lookup_type=['some_lookup_type']) result = f.filter(qs, Lookup('', 'some_lookup_type')) self.assertListEqual(qs.method_calls, []) self.assertEqual(qs, result)
def test_field_with_none_lookup_type(self): f = Filter(lookup_type=None) field = f.field self.assertIsInstance(field, LookupTypeField) choice_field = field.fields[1] self.assertEqual(len(choice_field.choices), len(LOOKUP_TYPES))
def test_filtering_skipped_with_none_value(self): qs = mock.Mock() f = Filter() result = f.filter(qs, None) self.assertListEqual(qs.method_calls, []) self.assertEqual(qs, result)
class F(FilterSet): f = Filter(method=filter_f)
def test_filtering_skipped_with_list_value_with_blank_lookup(self): qs = mock.Mock(spec=['filter']) f = Filter(name='somefield') result = f.filter(qs, ['value', '']) qs.filter.assert_called_once_with(somefield__exact='value') self.assertNotEqual(qs, result)
def test_creation(self): f = Filter() self.assertEqual(f.lookup_type, 'exact') self.assertEqual(f.exclude, False)
def test_filtering_uses_distinct(self): qs = mock.Mock(spec=['filter', 'distinct']) f = Filter(name='somefield', distinct=True) f.filter(qs, 'value') result = qs.distinct.assert_called_once() self.assertNotEqual(qs, result)
def test_default_field(self): f = Filter() field = f.field self.assertIsInstance(field, forms.Field) self.assertEqual(field.help_text, 'Filter')
def test_creation_order(self): f = Filter() f2 = Filter() self.assertTrue(f2.creation_counter > f.creation_counter)
def test_field_with_list_lookup_type(self): f = Filter(lookup_type=('istartswith', 'iendswith')) field = f.field self.assertIsInstance(field, LookupTypeField) choice_field = field.fields[1] self.assertEqual(len(choice_field.choices), 2)
def test_field_with_lookup_type_and_exlusion(self): f = Filter(lookup_type=None, exclude=True) field = f.field self.assertIsInstance(field, LookupTypeField) self.assertEqual(field.help_text, 'This is an exclusion filter')
def test_field_with_exclusion(self): f = Filter(exclude=True) field = f.field self.assertIsInstance(field, forms.Field) self.assertEqual(field.help_text, 'This is an exclusion filter')
def test_filtering_uses_distinct(self): qs = mock.Mock(spec=['filter', 'distinct']) f = Filter(name='somefield', distinct=True) f.filter(qs, 'value') result = qs.distinct.assert_called_once_with() self.assertNotEqual(qs, result)
def test_filtering(self): qs = mock.Mock(spec=['filter']) f = Filter() result = f.filter(qs, 'value') qs.filter.assert_called_once_with(None__exact='value') self.assertNotEqual(qs, result)
class F(FilterSet): f = Filter(method='filter_f') def filter_f(self, qs, name, value): pass
def test_filtering_exclude(self): qs = mock.Mock(spec=['filter', 'exclude']) f = Filter(exclude=True) result = f.filter(qs, 'value') qs.exclude.assert_called_once_with(None__exact='value') self.assertNotEqual(qs, result)
class F(FilterSet): f = Filter(method='filter_f') def filter_f(self, qs, name, value): # call mock request object to prove self.request can be accessed self.request()
def test_filtering_uses_name(self): qs = mock.Mock(spec=['filter']) f = Filter(name='somefield') f.filter(qs, 'value') result = qs.filter.assert_called_once_with(somefield__exact='value') self.assertNotEqual(qs, result)
class F(FilterSet): f = Filter(method='filter_f') filter_f = 4
def test_field_with_single_lookup_type(self): f = Filter(lookup_type='iexact') field = f.field self.assertIsInstance(field, forms.Field)