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 (
|
|
|
|
DeprecationInstanceCheck, RemovedInNextVersionWarning, RenameMethodsBase,
|
|
|
|
)
|
2013-03-08 14:15:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RenameManagerMethods(RenameMethodsBase):
|
|
|
|
renamed_methods = (
|
2013-06-29 16:34:41 +00:00
|
|
|
('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.
|
|
|
|
"""
|
2018-04-27 21:18:15 +00:00
|
|
|
msg = '`Manager.old` method should be renamed `new`.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
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.
|
|
|
|
"""
|
2018-04-27 21:18:15 +00:00
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def new(self):
|
|
|
|
pass
|
|
|
|
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:
|
2013-03-08 14:15:23 +00:00
|
|
|
warnings.simplefilter('always')
|
|
|
|
manager.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = '`Manager.old` is deprecated, use `new` instead.'
|
|
|
|
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.
|
|
|
|
"""
|
2018-07-11 13:10:31 +00:00
|
|
|
msg = '`Manager.old` method should be renamed `new`.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
|
|
|
class Manager(metaclass=RenameManagerMethods):
|
|
|
|
def old(self):
|
|
|
|
pass
|
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:
|
2013-03-08 14:15:23 +00:00
|
|
|
warnings.simplefilter('always')
|
|
|
|
manager.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = '`Manager.old` is deprecated, use `new` instead.'
|
|
|
|
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.
|
|
|
|
"""
|
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-07-11 13:10:31 +00:00
|
|
|
msg = '`Deprecated.old` method should be renamed `new`.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
|
|
|
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()
|
|
|
|
|
|
|
|
msg = '`Renamed.old` is deprecated, use `new` instead.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
|
|
|
|
msg = '`Deprecated.old` is deprecated, use `new` instead.'
|
|
|
|
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.
|
|
|
|
"""
|
2018-07-11 13:10:31 +00:00
|
|
|
msg = '`Deprecated.old` method should be renamed `new`.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
|
|
|
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:
|
2013-03-08 14:15:23 +00:00
|
|
|
warnings.simplefilter('always')
|
|
|
|
renamed.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(len(recorded), 0)
|
|
|
|
|
|
|
|
msg = '`Renamed.old` is deprecated, use `new` instead.'
|
|
|
|
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`.
|
|
|
|
"""
|
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
|
|
|
|
2018-07-11 13:10:31 +00:00
|
|
|
msg = '`DeprecatedMixin.old` method should be renamed `new`.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
|
|
|
class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
|
|
|
|
pass
|
2013-10-22 10:21:07 +00:00
|
|
|
|
2018-04-27 21:18:15 +00:00
|
|
|
deprecated = Deprecated()
|
|
|
|
|
|
|
|
msg = '`RenamedMixin.old` is deprecated, use `new` instead.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.new()
|
2018-04-27 21:18:15 +00:00
|
|
|
|
|
|
|
msg = '`DeprecatedMixin.old` is deprecated, use `new` instead.'
|
|
|
|
with self.assertWarnsMessage(DeprecationWarning, msg):
|
2013-03-08 14:15:23 +00:00
|
|
|
deprecated.old()
|
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):
|
2015-09-26 17:38:04 +00:00
|
|
|
alternative = 'fake.path.Foo'
|
|
|
|
deprecation_warning = RemovedInNextVersionWarning
|
|
|
|
|
|
|
|
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)
|