2013-03-08 14:15:23 +00:00
|
|
|
import warnings
|
|
|
|
|
2015-01-09 20:08:16 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-09-26 17:38:04 +00:00
|
|
|
from django.utils.deprecation import (
|
2022-02-03 19:24:19 +00:00
|
|
|
DeprecationInstanceCheck,
|
2022-02-18 20:22:50 +00:00
|
|
|
RemovedAfterNextVersionWarning,
|
2022-02-03 19:24:19 +00:00
|
|
|
RemovedInNextVersionWarning,
|
|
|
|
RenameMethodsBase,
|
2015-09-26 17:38:04 +00:00
|
|
|
)
|
2013-03-08 14:15:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RenameManagerMethods(RenameMethodsBase):
|
2022-02-03 19:24:19 +00:00
|
|
|
renamed_methods = (("old", "new", DeprecationWarning),)
|
2013-03-08 14:15:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RenameMethodsTests(SimpleTestCase):
|
|
|
|
"""
|
|
|
|
Tests the `RenameMethodsBase` type introduced to rename `get_query_set`
|
|
|
|
to `get_queryset` across the code base following #15363.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_class_definition_warnings(self):
|
|
|
|
"""
|
|
|
|
Ensure a warning is raised upon class definition to suggest renaming
|
|
|
|
the faulty method.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Manager.old` method should be renamed `new`."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2017-01-07 11:11:46 +00:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
2013-03-08 14:15:23 +00:00
|
|
|
def old(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_get_new_defined(self):
|
|
|
|
"""
|
|
|
|
Ensure `old` complains and not `new` when only `new` is defined.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
manager = Manager()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2022-02-03 19:24:19 +00:00
|
|
|
warnings.simplefilter("always")
|
2013-03-08 14:15:23 +00:00
|
|
|
manager.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Manager.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
manager.old()
|
|
|
|
|
|
|
|
def test_get_old_defined(self):
|
|
|
|
"""
|
|
|
|
Ensure `old` complains when only `old` is defined.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Manager.old` method should be renamed `new`."
|
2018-07-11 13:10:31 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-07-11 13:10:31 +00:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def old(self):
|
|
|
|
pass
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
manager = Manager()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2022-02-03 19:24:19 +00:00
|
|
|
warnings.simplefilter("always")
|
2013-03-08 14:15:23 +00:00
|
|
|
manager.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Manager.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
manager.old()
|
|
|
|
|
|
|
|
def test_deprecated_subclass_renamed(self):
|
|
|
|
"""
|
|
|
|
Ensure the correct warnings are raised when a class that didn't rename
|
|
|
|
`old` subclass one that did.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class Renamed(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Deprecated.old` method should be renamed `new`."
|
2018-07-11 13:10:31 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-07-11 13:10:31 +00:00
|
|
|
class Deprecated(Renamed):
|
|
|
|
def old(self):
|
|
|
|
super().old()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
deprecated = Deprecated()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Renamed.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Deprecated.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.old()
|
|
|
|
|
|
|
|
def test_renamed_subclass_deprecated(self):
|
|
|
|
"""
|
|
|
|
Ensure the correct warnings are raised when a class that renamed
|
|
|
|
`old` subclass one that didn't.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Deprecated.old` method should be renamed `new`."
|
2018-07-11 13:10:31 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-07-11 13:10:31 +00:00
|
|
|
class Deprecated(metaclass=RenameManagerMethods):
|
|
|
|
def old(self):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class Renamed(Deprecated):
|
|
|
|
def new(self):
|
|
|
|
super().new()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
renamed = Renamed()
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2022-02-03 19:24:19 +00:00
|
|
|
warnings.simplefilter("always")
|
2013-03-08 14:15:23 +00:00
|
|
|
renamed.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Renamed.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
renamed.old()
|
|
|
|
|
|
|
|
def test_deprecated_subclass_renamed_and_mixins(self):
|
|
|
|
"""
|
|
|
|
Ensure the correct warnings are raised when a subclass inherit from a
|
|
|
|
class that renamed `old` and mixins that may or may not have renamed
|
|
|
|
`new`.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class Renamed(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class RenamedMixin:
|
|
|
|
def new(self):
|
|
|
|
super().new()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
class DeprecatedMixin:
|
|
|
|
def old(self):
|
|
|
|
super().old()
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`DeprecatedMixin.old` method should be renamed `new`."
|
2018-07-11 13:10:31 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2018-07-11 13:10:31 +00:00
|
|
|
class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
deprecated = Deprecated()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`RenamedMixin.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`DeprecatedMixin.old` is deprecated, use `new` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.old()
|
2015-09-26 17:38:04 +00:00
|
|
|
|
2022-02-18 20:22:50 +00:00
|
|
|
def test_removedafternextversionwarning_pending(self):
|
|
|
|
self.assertTrue(
|
|
|
|
issubclass(RemovedAfterNextVersionWarning, PendingDeprecationWarning)
|
|
|
|
)
|
|
|
|
|
2015-09-26 17:38:04 +00:00
|
|
|
|
|
|
|
class DeprecationInstanceCheckTest(SimpleTestCase):
|
|
|
|
def test_warning(self):
|
2017-01-07 11:11:46 +00:00
|
|
|
class Manager(metaclass=DeprecationInstanceCheck):
|
2022-02-03 19:24:19 +00:00
|
|
|
alternative = "fake.path.Foo"
|
2015-09-26 17:38:04 +00:00
|
|
|
deprecation_warning = RemovedInNextVersionWarning
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "`Manager` is deprecated, use `fake.path.Foo` instead."
|
2018-04-27 21:18:15 +00:00
|
|
|
with self.assertWarnsMessage(RemovedInNextVersionWarning, msg):
|
|
|
|
isinstance(object, Manager)
|