From 5691fee82c0f08a9ec577eff0bd54af148cbe6f9 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Thu, 19 Oct 2017 05:33:13 -0400 Subject: [PATCH] [2.x] Remove some deprecations (#795) * Remove deprecation tests * Remove 'Filter.name' deprecation * Fixup 'Filter.field_name' in tests --- django_filters/filters.py | 15 ------- tests/rest_framework/test_filterset.py | 2 +- tests/test_deprecations.py | 54 -------------------------- tests/test_filtering.py | 10 ++--- tests/test_filters.py | 50 ++++++++++++------------ tests/test_filterset.py | 28 ++++++------- tests/test_forms.py | 6 +-- 7 files changed, 48 insertions(+), 117 deletions(-) delete mode 100644 tests/test_deprecations.py diff --git a/django_filters/filters.py b/django_filters/filters.py index e336bdd4c..90701a27d 100644 --- a/django_filters/filters.py +++ b/django_filters/filters.py @@ -89,9 +89,6 @@ class Filter(object): def __init__(self, field_name=None, label=None, method=None, lookup_expr='exact', distinct=False, exclude=False, **kwargs): self.field_name = field_name - if field_name is None and 'name' in kwargs: - deprecate("`Filter.name` has been renamed to `Filter.field_name`.") - self.field_name = kwargs.pop('name') self.label = label self.method = method self.lookup_expr = lookup_expr @@ -132,18 +129,6 @@ def fset(self, value): return locals() method = property(**method()) - def name(): - def fget(self): - deprecate("`Filter.name` has been renamed to `Filter.field_name`.") - return self.field_name - - def fset(self, value): - deprecate("`Filter.name` has been renamed to `Filter.field_name`.") - self.field_name = value - - return locals() - name = property(**name()) - def label(): def fget(self): if self._label is None and hasattr(self, 'parent'): diff --git a/tests/rest_framework/test_filterset.py b/tests/rest_framework/test_filterset.py index bc04ba981..af1b343ef 100644 --- a/tests/rest_framework/test_filterset.py +++ b/tests/rest_framework/test_filterset.py @@ -23,7 +23,7 @@ def test_isodatetimefilter(self): field = Article._meta.get_field('published') result = FilterSet.filter_for_field(field, 'published') self.assertIsInstance(result, filters.IsoDateTimeFilter) - self.assertEqual(result.name, 'published') + self.assertEqual(result.field_name, 'published') def test_booleanfilter_widget(self): field = User._meta.get_field('is_active') diff --git a/tests/test_deprecations.py b/tests/test_deprecations.py deleted file mode 100644 index 7dd5d983e..000000000 --- a/tests/test_deprecations.py +++ /dev/null @@ -1,54 +0,0 @@ - -import warnings - -from django.test import TestCase - -from django_filters import FilterSet, filters - - -class TogetherOptionDeprecationTests(TestCase): - - def test_deprecation(self): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - - class F(FilterSet): - class Meta: - together = ['a', 'b'] - - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, DeprecationWarning)) - self.assertIn('The `Meta.together` option has been deprecated', str(w[0].message)) - - -class FilterNameDeprecationTests(TestCase): - - def test_declaration(self): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - - class F(FilterSet): - foo = filters.CharFilter(name='foo') - - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, DeprecationWarning)) - self.assertIn("`Filter.name` has been renamed to `Filter.field_name`.", str(w[0].message)) - - def test_name_property(self): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - - filters.CharFilter().name - - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, DeprecationWarning)) - self.assertIn("`Filter.name` has been renamed to `Filter.field_name`.", str(w[0].message)) - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") - - filters.CharFilter().name = 'bar' - - self.assertEqual(len(w), 1) - self.assertTrue(issubclass(w[0].category, DeprecationWarning)) - self.assertIn("`Filter.name` has been renamed to `Filter.field_name`.", str(w[0].message)) diff --git a/tests/test_filtering.py b/tests/test_filtering.py index e4556844c..c5225ba53 100644 --- a/tests/test_filtering.py +++ b/tests/test_filtering.py @@ -492,8 +492,8 @@ class Meta: def test_filtering_with_multiple_lookup_exprs(self): class F(FilterSet): - min_duration = DurationFilter(name='duration', lookup_expr='gte') - max_duration = DurationFilter(name='duration', lookup_expr='lte') + min_duration = DurationFilter(field_name='duration', lookup_expr='gte') + max_duration = DurationFilter(field_name='duration', lookup_expr='lte') class Meta: model = SpacewalkRecord @@ -551,7 +551,7 @@ def users(request): return User.objects.filter(pk__lt=request.user.pk) class F(FilterSet): - author = ModelChoiceFilter(name='author', queryset=users) + author = ModelChoiceFilter(field_name='author', queryset=users) class Meta: model = Comment @@ -907,7 +907,7 @@ def test_filtering(self): Comment.objects.create(date=datetime.date(2016, 1, 3), **kwargs) class F(FilterSet): - published = DateFromToRangeFilter(name='date') + published = DateFromToRangeFilter(field_name='date') class Meta: model = Comment @@ -1886,7 +1886,7 @@ def setUp(self): def test_filtering_with_declared_filters(self): class F(FilterSet): - account = CharFilter(name='username') + account = CharFilter(field_name='username') class Meta: model = User diff --git a/tests/test_filters.py b/tests/test_filters.py index 2dd3eacd4..713230a80 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -118,7 +118,7 @@ def test_field_with_list_lookup_expr(self): def test_field_params(self): with mock.patch.object(Filter, 'field_class', spec=['__call__']) as mocked: - f = Filter(name='somefield', label='somelabel', + f = Filter(field_name='somefield', label='somelabel', widget='somewidget') f.field mocked.assert_called_once_with(required=False, @@ -159,7 +159,7 @@ def test_filtering_exclude(self): def test_filtering_uses_name(self): qs = mock.Mock(spec=['filter']) - f = Filter(name='somefield') + f = Filter(field_name='somefield') f.filter(qs, 'value') result = qs.filter.assert_called_once_with(somefield__exact='value') self.assertNotEqual(qs, result) @@ -180,14 +180,14 @@ def test_filtering_skipped_with_none_value(self): def test_filtering_with_list_value(self): qs = mock.Mock(spec=['filter']) - f = Filter(name='somefield', lookup_expr=['some_lookup_expr']) + f = Filter(field_name='somefield', lookup_expr=['some_lookup_expr']) result = f.filter(qs, Lookup('value', 'some_lookup_expr')) qs.filter.assert_called_once_with(somefield__some_lookup_expr='value') self.assertNotEqual(qs, result) def test_filtering_skipped_with_list_value_with_blank(self): qs = mock.Mock() - f = Filter(name='somefield', lookup_expr=['some_lookup_expr']) + f = Filter(field_name='somefield', lookup_expr=['some_lookup_expr']) result = f.filter(qs, Lookup('', 'some_lookup_expr')) self.assertListEqual(qs.method_calls, []) self.assertEqual(qs, result) @@ -195,7 +195,7 @@ def test_filtering_skipped_with_list_value_with_blank(self): def test_filtering_skipped_with_list_value_with_blank_lookup(self): return # Now field is required to provide valid lookup_expr if it provides any qs = mock.Mock(spec=['filter']) - f = Filter(name='somefield', lookup_expr=None) + f = Filter(field_name='somefield', lookup_expr=None) result = f.filter(qs, Lookup('value', '')) qs.filter.assert_called_once_with(somefield__exact='value') self.assertNotEqual(qs, result) @@ -210,7 +210,7 @@ def test_filter_using_method(self): def test_filtering_uses_distinct(self): qs = mock.Mock(spec=['filter', 'distinct']) - f = Filter(name='somefield', distinct=True) + f = Filter(field_name='somefield', distinct=True) f.filter(qs, 'value') result = qs.distinct.assert_called_once_with() self.assertNotEqual(qs, result) @@ -231,13 +231,13 @@ def filter(self_, qs, value): def test_lookup_false(self): qs = mock.Mock(spec=['filter']) - f = self.test_filter_class(name='somefield') + f = self.test_filter_class(field_name='somefield') result = f.filter(qs, Lookup('', 'exact')) self.assertEqual(qs, result) def test_lookup_true(self): qs = mock.Mock(spec=['filter']) - f = self.test_filter_class(name='somefield') + f = self.test_filter_class(field_name='somefield') result = f.filter(qs, Lookup('somesearch', 'exact')) qs.filter.assert_called_once_with(somefield__exact='somesearch') self.assertNotEqual(qs, result) @@ -268,35 +268,35 @@ def test_default_field(self): def test_filtering(self): qs = mock.Mock(spec=['filter']) - f = BooleanFilter(name='somefield') + f = BooleanFilter(field_name='somefield') result = f.filter(qs, True) qs.filter.assert_called_once_with(somefield__exact=True) self.assertNotEqual(qs, result) def test_filtering_exclude(self): qs = mock.Mock(spec=['exclude']) - f = BooleanFilter(name='somefield', exclude=True) + f = BooleanFilter(field_name='somefield', exclude=True) result = f.filter(qs, True) qs.exclude.assert_called_once_with(somefield__exact=True) self.assertNotEqual(qs, result) def test_filtering_skipped_with_blank_value(self): qs = mock.Mock() - f = BooleanFilter(name='somefield') + f = BooleanFilter(field_name='somefield') result = f.filter(qs, '') self.assertListEqual(qs.method_calls, []) self.assertEqual(qs, result) def test_filtering_skipped_with_none_value(self): qs = mock.Mock() - f = BooleanFilter(name='somefield') + f = BooleanFilter(field_name='somefield') result = f.filter(qs, None) self.assertListEqual(qs.method_calls, []) self.assertEqual(qs, result) def test_filtering_lookup_expr(self): qs = mock.Mock(spec=['filter']) - f = BooleanFilter(name='somefield', lookup_expr='isnull') + f = BooleanFilter(field_name='somefield', lookup_expr='isnull') result = f.filter(qs, True) qs.filter.assert_called_once_with(somefield__isnull=True) self.assertNotEqual(qs, result) @@ -447,7 +447,7 @@ def test_conjoined_true(self): def test_filtering(self): qs = mock.Mock(spec=['filter']) - f = MultipleChoiceFilter(name='somefield') + f = MultipleChoiceFilter(field_name='somefield') with mock.patch('django_filters.filters.Q') as mockQclass: mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock() mockQclass.side_effect = [mockQ1, mockQ2] @@ -462,7 +462,7 @@ def test_filtering(self): def test_filtering_exclude(self): qs = mock.Mock(spec=['exclude']) - f = MultipleChoiceFilter(name='somefield', exclude=True) + f = MultipleChoiceFilter(field_name='somefield', exclude=True) with mock.patch('django_filters.filters.Q') as mockQclass: mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock() mockQclass.side_effect = [mockQ1, mockQ2] @@ -477,7 +477,7 @@ def test_filtering_exclude(self): def test_filtering_on_required_skipped_when_len_of_value_is_len_of_field_choices(self): qs = mock.Mock(spec=[]) - f = MultipleChoiceFilter(name='somefield', required=True) + f = MultipleChoiceFilter(field_name='somefield', required=True) f.always_filter = False result = f.filter(qs, []) self.assertEqual(len(f.field.choices), 0) @@ -492,7 +492,7 @@ def test_filtering_on_required_skipped_when_len_of_value_is_len_of_field_choices def test_filtering_skipped_with_empty_list_value_and_some_choices(self): qs = mock.Mock(spec=[]) - f = MultipleChoiceFilter(name='somefield') + f = MultipleChoiceFilter(field_name='somefield') f.field.choices = ['some', 'values', 'here'] result = f.filter(qs, []) self.assertEqual(qs, result) @@ -551,7 +551,7 @@ def test_filter_conjoined_true(self): users = User.objects.all() for item in filter_list: - f = MultipleChoiceFilter(name='favorite_books__pk', conjoined=True) + f = MultipleChoiceFilter(field_name='favorite_books__pk', conjoined=True) queryset = f.filter(users, item[0]) expected_pks = [c[0] for c in queryset.values_list('pk')] self.assertListEqual( @@ -584,7 +584,7 @@ def test_conjoined_true(self): def test_filtering(self): qs = mock.Mock(spec=['filter']) - f = TypedMultipleChoiceFilter(name='somefield') + f = TypedMultipleChoiceFilter(field_name='somefield') with mock.patch('django_filters.filters.Q') as mockQclass: mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock() mockQclass.side_effect = [mockQ1, mockQ2] @@ -599,7 +599,7 @@ def test_filtering(self): def test_filtering_exclude(self): qs = mock.Mock(spec=['exclude']) - f = TypedMultipleChoiceFilter(name='somefield', exclude=True) + f = TypedMultipleChoiceFilter(field_name='somefield', exclude=True) with mock.patch('django_filters.filters.Q') as mockQclass: mockQ1, mockQ2 = mock.MagicMock(), mock.MagicMock() mockQclass.side_effect = [mockQ1, mockQ2] @@ -614,7 +614,7 @@ def test_filtering_exclude(self): def test_filtering_on_required_skipped_when_len_of_value_is_len_of_field_choices(self): qs = mock.Mock(spec=[]) - f = TypedMultipleChoiceFilter(name='somefield', required=True) + f = TypedMultipleChoiceFilter(field_name='somefield', required=True) f.always_filter = False result = f.filter(qs, []) self.assertEqual(len(f.field.choices), 0) @@ -629,7 +629,7 @@ def test_filtering_on_required_skipped_when_len_of_value_is_len_of_field_choices def test_filtering_skipped_with_empty_list_value_and_some_choices(self): qs = mock.Mock(spec=[]) - f = TypedMultipleChoiceFilter(name='somefield') + f = TypedMultipleChoiceFilter(field_name='somefield') f.field.choices = ['some', 'values', 'here'] result = f.filter(qs, []) self.assertEqual(qs, result) @@ -688,7 +688,7 @@ def test_filter_conjoined_true(self): users = User.objects.all() for item in filter_list: - f = TypedMultipleChoiceFilter(name='favorite_books__pk', conjoined=True) + f = TypedMultipleChoiceFilter(field_name='favorite_books__pk', conjoined=True) queryset = f.filter(users, item[0]) expected_pks = [c[0] for c in queryset.values_list('pk')] self.assertListEqual( @@ -807,7 +807,7 @@ def test_default_field_with_queryset(self): def test_filtering_to_field_name(self): qs = User.objects.all() - f = ModelMultipleChoiceFilter(name='first_name', + f = ModelMultipleChoiceFilter(field_name='first_name', to_field_name='first_name', queryset=qs) user = User.objects.create(first_name='Firstname') @@ -1203,7 +1203,7 @@ def test_default_field_with_assigning_model(self): self.assertIsInstance(field, forms.ChoiceField) def test_empty_value_in_choices(self): - f = AllValuesFilter(name='username') + f = AllValuesFilter(field_name='username') f.model = User self.assertEqual(list(f.field.choices), [ diff --git a/tests/test_filterset.py b/tests/test_filterset.py index f83958485..045eaed69 100644 --- a/tests/test_filterset.py +++ b/tests/test_filterset.py @@ -121,25 +121,25 @@ def test_filter_found_for_field(self): f = User._meta.get_field('username') result = FilterSet.filter_for_field(f, 'username') self.assertIsInstance(result, CharFilter) - self.assertEqual(result.name, 'username') + self.assertEqual(result.field_name, 'username') def test_filter_found_for_uuidfield(self): f = UUIDTestModel._meta.get_field('uuid') result = FilterSet.filter_for_field(f, 'uuid') self.assertIsInstance(result, UUIDFilter) - self.assertEqual(result.name, 'uuid') + self.assertEqual(result.field_name, 'uuid') def test_filter_found_for_autofield(self): f = User._meta.get_field('id') result = FilterSet.filter_for_field(f, 'id') self.assertIsInstance(result, NumberFilter) - self.assertEqual(result.name, 'id') + self.assertEqual(result.field_name, 'id') def test_field_with_extras(self): f = User._meta.get_field('favorite_books') result = FilterSet.filter_for_field(f, 'favorite_books') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'favorite_books') + self.assertEqual(result.field_name, 'favorite_books') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Book) @@ -148,7 +148,7 @@ def test_field_with_choices(self): f = User._meta.get_field('status') result = FilterSet.filter_for_field(f, 'status') self.assertIsInstance(result, ChoiceFilter) - self.assertEqual(result.name, 'status') + self.assertEqual(result.field_name, 'status') self.assertTrue('choices' in result.extra) self.assertIsNotNone(result.extra['choices']) @@ -172,7 +172,7 @@ def test_symmetrical_selfref_m2m_field(self): f = Node._meta.get_field('adjacents') result = FilterSet.filter_for_field(f, 'adjacents') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'adjacents') + self.assertEqual(result.field_name, 'adjacents') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Node) @@ -181,7 +181,7 @@ def test_non_symmetrical_selfref_m2m_field(self): f = DirectedNode._meta.get_field('outbound_nodes') result = FilterSet.filter_for_field(f, 'outbound_nodes') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'outbound_nodes') + self.assertEqual(result.field_name, 'outbound_nodes') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, DirectedNode) @@ -190,7 +190,7 @@ def test_m2m_field_with_through_model(self): f = Business._meta.get_field('employees') result = FilterSet.filter_for_field(f, 'employees') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'employees') + self.assertEqual(result.field_name, 'employees') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Worker) @@ -200,7 +200,7 @@ def test_transformed_lookup_expr(self): f = Comment._meta.get_field('date') result = FilterSet.filter_for_field(f, 'date', 'year__gte') self.assertIsInstance(result, NumberFilter) - self.assertEqual(result.name, 'date') + self.assertEqual(result.field_name, 'date') @unittest.skip('todo') def test_filter_overrides(self): @@ -253,7 +253,7 @@ def test_reverse_o2o_relationship(self): f = Account._meta.get_field('profile') result = FilterSet.filter_for_reverse_field(f, 'profile') self.assertIsInstance(result, ModelChoiceFilter) - self.assertEqual(result.name, 'profile') + self.assertEqual(result.field_name, 'profile') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Profile) @@ -262,7 +262,7 @@ def test_reverse_fk_relationship(self): f = User._meta.get_field('comments') result = FilterSet.filter_for_reverse_field(f, 'comments') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'comments') + self.assertEqual(result.field_name, 'comments') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Comment) @@ -271,7 +271,7 @@ def test_reverse_m2m_relationship(self): f = Book._meta.get_field('lovers') result = FilterSet.filter_for_reverse_field(f, 'lovers') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'lovers') + self.assertEqual(result.field_name, 'lovers') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, User) @@ -280,7 +280,7 @@ def test_reverse_non_symmetrical_selfref_m2m_field(self): f = DirectedNode._meta.get_field('inbound_nodes') result = FilterSet.filter_for_reverse_field(f, 'inbound_nodes') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'inbound_nodes') + self.assertEqual(result.field_name, 'inbound_nodes') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, DirectedNode) @@ -289,7 +289,7 @@ def test_reverse_m2m_field_with_through_model(self): f = Worker._meta.get_field('employers') result = FilterSet.filter_for_reverse_field(f, 'employers') self.assertIsInstance(result, ModelMultipleChoiceFilter) - self.assertEqual(result.name, 'employers') + self.assertEqual(result.field_name, 'employers') self.assertTrue('queryset' in result.extra) self.assertIsNotNone(result.extra['queryset']) self.assertEqual(result.extra['queryset'].model, Business) diff --git a/tests/test_forms.py b/tests/test_forms.py index e11ebd7d7..0932f35e6 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -81,7 +81,7 @@ class Meta: def test_complex_form_fields(self): class F(FilterSet): username = CharFilter(label='Filter for users with username') - exclude_username = CharFilter(name='username', lookup_expr='iexact', exclude=True) + exclude_username = CharFilter(field_name='username', lookup_expr='iexact', exclude=True) class Meta: model = User @@ -135,7 +135,7 @@ class Meta: def test_form_field_with_manual_name(self): class F(FilterSet): - book_title = CharFilter(name='title') + book_title = CharFilter(field_name='title') class Meta: model = Book @@ -147,7 +147,7 @@ class Meta: def test_form_field_with_manual_name_and_label(self): class F(FilterSet): - f1 = CharFilter(name='title', label="Book title") + f1 = CharFilter(field_name='title', label="Book title") class Meta: model = Book