2013-03-08 22:15:23 +08:00
|
|
|
import warnings
|
|
|
|
|
2015-01-10 04:08:16 +08:00
|
|
|
from django.test import SimpleTestCase
|
2015-09-27 01:38:04 +08:00
|
|
|
from django.utils.deprecation import (
|
|
|
|
DeprecationInstanceCheck,
|
2022-02-19 04:22:50 +08:00
|
|
|
RemovedAfterNextVersionWarning,
|
2015-09-27 01:38:04 +08:00
|
|
|
RemovedInNextVersionWarning,
|
|
|
|
RenameMethodsBase,
|
|
|
|
)
|
2013-03-08 22:15:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RenameManagerMethods(RenameMethodsBase):
|
2013-06-30 00:34:41 +08:00
|
|
|
renamed_methods = (("old", "new", DeprecationWarning),)
|
2013-03-08 22:15:23 +08: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.
|
|
|
|
"""
|
2018-04-28 05:18:15 +08:00
|
|
|
msg = "`Manager.old` method should be renamed `new`."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2017-01-07 19:11:46 +08:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
2013-03-08 22:15:23 +08:00
|
|
|
def old(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_get_new_defined(self):
|
|
|
|
"""
|
|
|
|
Ensure `old` complains and not `new` when only `new` is defined.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
manager = Manager()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2013-03-08 22:15:23 +08:00
|
|
|
warnings.simplefilter("always")
|
|
|
|
manager.new()
|
2018-04-28 05:18:15 +08:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = "`Manager.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08:00
|
|
|
manager.old()
|
|
|
|
|
|
|
|
def test_get_old_defined(self):
|
|
|
|
"""
|
|
|
|
Ensure `old` complains when only `old` is defined.
|
|
|
|
"""
|
2018-07-11 21:10:31 +08:00
|
|
|
msg = "`Manager.old` method should be renamed `new`."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def old(self):
|
|
|
|
pass
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
manager = Manager()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2013-03-08 22:15:23 +08:00
|
|
|
warnings.simplefilter("always")
|
|
|
|
manager.new()
|
2018-04-28 05:18:15 +08:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = "`Manager.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08: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-04 03:24:19 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class Renamed(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
msg = "`Deprecated.old` method should be renamed `new`."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
class Deprecated(Renamed):
|
|
|
|
def old(self):
|
|
|
|
super().old()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
deprecated = Deprecated()
|
|
|
|
|
|
|
|
msg = "`Renamed.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08:00
|
|
|
deprecated.new()
|
2018-04-28 05:18:15 +08:00
|
|
|
|
|
|
|
msg = "`Deprecated.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08: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.
|
|
|
|
"""
|
2018-07-11 21:10:31 +08:00
|
|
|
msg = "`Deprecated.old` method should be renamed `new`."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
class Deprecated(metaclass=RenameManagerMethods):
|
|
|
|
def old(self):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class Renamed(Deprecated):
|
|
|
|
def new(self):
|
|
|
|
super().new()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
renamed = Renamed()
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as recorded:
|
2013-03-08 22:15:23 +08:00
|
|
|
warnings.simplefilter("always")
|
|
|
|
renamed.new()
|
2018-04-28 05:18:15 +08:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = "`Renamed.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08: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-04 03:24:19 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class Renamed(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class RenamedMixin:
|
|
|
|
def new(self):
|
|
|
|
super().new()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
class DeprecatedMixin:
|
|
|
|
def old(self):
|
|
|
|
super().old()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
msg = "`DeprecatedMixin.old` method should be renamed `new`."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2018-07-11 21:10:31 +08:00
|
|
|
class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
|
|
|
|
pass
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
deprecated = Deprecated()
|
|
|
|
|
|
|
|
msg = "`RenamedMixin.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08:00
|
|
|
deprecated.new()
|
2018-04-28 05:18:15 +08:00
|
|
|
|
|
|
|
msg = "`DeprecatedMixin.old` is deprecated, use `new` instead."
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 22:15:23 +08:00
|
|
|
deprecated.old()
|
2015-09-27 01:38:04 +08:00
|
|
|
|
2022-02-19 04:22:50 +08:00
|
|
|
def test_removedafternextversionwarning_pending(self):
|
|
|
|
self.assertTrue(
|
|
|
|
issubclass(RemovedAfterNextVersionWarning, PendingDeprecationWarning)
|
|
|
|
)
|
|
|
|
|
2015-09-27 01:38:04 +08:00
|
|
|
|
|
|
|
class DeprecationInstanceCheckTest(SimpleTestCase):
|
|
|
|
def test_warning(self):
|
2017-01-07 19:11:46 +08:00
|
|
|
class Manager(metaclass=DeprecationInstanceCheck):
|
2015-09-27 01:38:04 +08:00
|
|
|
alternative = "fake.path.Foo"
|
|
|
|
deprecation_warning = RemovedInNextVersionWarning
|
|
|
|
|
|
|
|
msg = "`Manager` is deprecated, use `fake.path.Foo` instead."
|
2018-04-28 05:18:15 +08:00
|
|
|
with self.assertWarnsMessage(RemovedInNextVersionWarning, msg):
|
|
|
|
isinstance(object, Manager)
|