コード例 #1
0
 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)
コード例 #2
0
 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)
コード例 #3
0
 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)
コード例 #4
0
 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)
コード例 #5
0
    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)
コード例 #6
0
ファイル: test_filters.py プロジェクト: msk7777/django-filter
 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)
コード例 #7
0
 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)
コード例 #8
0
    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))
コード例 #9
0
    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))
コード例 #10
0
    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))
コード例 #11
0
 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)
コード例 #12
0
 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')
コード例 #13
0
 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)
コード例 #14
0
 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)
コード例 #15
0
ファイル: test_filters.py プロジェクト: rtindru/django-filter
 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))
コード例 #16
0
ファイル: test_filters.py プロジェクト: rtindru/django-filter
    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')
コード例 #17
0
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)
コード例 #18
0
ファイル: filters.py プロジェクト: Vacasa/drf-jsonapi
    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
コード例 #19
0
    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')
コード例 #20
0
 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)
コード例 #21
0
 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)
コード例 #22
0
 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))
コード例 #23
0
 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)
コード例 #24
0
 class F(FilterSet):
     f = Filter(method=filter_f)
コード例 #25
0
ファイル: filter_tests.py プロジェクト: Kami/django-filter
 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)
コード例 #26
0
 def test_creation(self):
     f = Filter()
     self.assertEqual(f.lookup_type, 'exact')
     self.assertEqual(f.exclude, False)
コード例 #27
0
 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)
コード例 #28
0
 def test_default_field(self):
     f = Filter()
     field = f.field
     self.assertIsInstance(field, forms.Field)
     self.assertEqual(field.help_text, 'Filter')
コード例 #29
0
 def test_creation_order(self):
     f = Filter()
     f2 = Filter()
     self.assertTrue(f2.creation_counter > f.creation_counter)
コード例 #30
0
 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)
コード例 #31
0
 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')
コード例 #32
0
 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')
コード例 #33
0
 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)
コード例 #34
0
 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)
コード例 #35
0
 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)
コード例 #36
0
 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)
コード例 #37
0
        class F(FilterSet):
            f = Filter(method='filter_f')

            def filter_f(self, qs, name, value):
                pass
コード例 #38
0
 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)
コード例 #39
0
        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()
コード例 #40
0
 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)
コード例 #41
0
 class F(FilterSet):
     f = Filter(method='filter_f')
     filter_f = 4
コード例 #42
0
 def test_field_with_single_lookup_type(self):
     f = Filter(lookup_type='iexact')
     field = f.field
     self.assertIsInstance(field, forms.Field)