1. import warnings
    
  2. 
    
  3. from django.test import SimpleTestCase
    
  4. from django.utils.deprecation import (
    
  5.     DeprecationInstanceCheck,
    
  6.     RemovedAfterNextVersionWarning,
    
  7.     RemovedInNextVersionWarning,
    
  8.     RenameMethodsBase,
    
  9. )
    
  10. 
    
  11. 
    
  12. class RenameManagerMethods(RenameMethodsBase):
    
  13.     renamed_methods = (("old", "new", DeprecationWarning),)
    
  14. 
    
  15. 
    
  16. class RenameMethodsTests(SimpleTestCase):
    
  17.     """
    
  18.     Tests the `RenameMethodsBase` type introduced to rename `get_query_set`
    
  19.     to `get_queryset` across the code base following #15363.
    
  20.     """
    
  21. 
    
  22.     def test_class_definition_warnings(self):
    
  23.         """
    
  24.         Ensure a warning is raised upon class definition to suggest renaming
    
  25.         the faulty method.
    
  26.         """
    
  27.         msg = "`Manager.old` method should be renamed `new`."
    
  28.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  29. 
    
  30.             class Manager(metaclass=RenameManagerMethods):
    
  31.                 def old(self):
    
  32.                     pass
    
  33. 
    
  34.     def test_get_new_defined(self):
    
  35.         """
    
  36.         Ensure `old` complains and not `new` when only `new` is defined.
    
  37.         """
    
  38. 
    
  39.         class Manager(metaclass=RenameManagerMethods):
    
  40.             def new(self):
    
  41.                 pass
    
  42. 
    
  43.         manager = Manager()
    
  44. 
    
  45.         with warnings.catch_warnings(record=True) as recorded:
    
  46.             warnings.simplefilter("always")
    
  47.             manager.new()
    
  48.         self.assertEqual(len(recorded), 0)
    
  49. 
    
  50.         msg = "`Manager.old` is deprecated, use `new` instead."
    
  51.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  52.             manager.old()
    
  53. 
    
  54.     def test_get_old_defined(self):
    
  55.         """
    
  56.         Ensure `old` complains when only `old` is defined.
    
  57.         """
    
  58.         msg = "`Manager.old` method should be renamed `new`."
    
  59.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  60. 
    
  61.             class Manager(metaclass=RenameManagerMethods):
    
  62.                 def old(self):
    
  63.                     pass
    
  64. 
    
  65.         manager = Manager()
    
  66. 
    
  67.         with warnings.catch_warnings(record=True) as recorded:
    
  68.             warnings.simplefilter("always")
    
  69.             manager.new()
    
  70.         self.assertEqual(len(recorded), 0)
    
  71. 
    
  72.         msg = "`Manager.old` is deprecated, use `new` instead."
    
  73.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  74.             manager.old()
    
  75. 
    
  76.     def test_deprecated_subclass_renamed(self):
    
  77.         """
    
  78.         Ensure the correct warnings are raised when a class that didn't rename
    
  79.         `old` subclass one that did.
    
  80.         """
    
  81. 
    
  82.         class Renamed(metaclass=RenameManagerMethods):
    
  83.             def new(self):
    
  84.                 pass
    
  85. 
    
  86.         msg = "`Deprecated.old` method should be renamed `new`."
    
  87.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  88. 
    
  89.             class Deprecated(Renamed):
    
  90.                 def old(self):
    
  91.                     super().old()
    
  92. 
    
  93.         deprecated = Deprecated()
    
  94. 
    
  95.         msg = "`Renamed.old` is deprecated, use `new` instead."
    
  96.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  97.             deprecated.new()
    
  98. 
    
  99.         msg = "`Deprecated.old` is deprecated, use `new` instead."
    
  100.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  101.             deprecated.old()
    
  102. 
    
  103.     def test_renamed_subclass_deprecated(self):
    
  104.         """
    
  105.         Ensure the correct warnings are raised when a class that renamed
    
  106.         `old` subclass one that didn't.
    
  107.         """
    
  108.         msg = "`Deprecated.old` method should be renamed `new`."
    
  109.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  110. 
    
  111.             class Deprecated(metaclass=RenameManagerMethods):
    
  112.                 def old(self):
    
  113.                     pass
    
  114. 
    
  115.         class Renamed(Deprecated):
    
  116.             def new(self):
    
  117.                 super().new()
    
  118. 
    
  119.         renamed = Renamed()
    
  120. 
    
  121.         with warnings.catch_warnings(record=True) as recorded:
    
  122.             warnings.simplefilter("always")
    
  123.             renamed.new()
    
  124.         self.assertEqual(len(recorded), 0)
    
  125. 
    
  126.         msg = "`Renamed.old` is deprecated, use `new` instead."
    
  127.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  128.             renamed.old()
    
  129. 
    
  130.     def test_deprecated_subclass_renamed_and_mixins(self):
    
  131.         """
    
  132.         Ensure the correct warnings are raised when a subclass inherit from a
    
  133.         class that renamed `old` and mixins that may or may not have renamed
    
  134.         `new`.
    
  135.         """
    
  136. 
    
  137.         class Renamed(metaclass=RenameManagerMethods):
    
  138.             def new(self):
    
  139.                 pass
    
  140. 
    
  141.         class RenamedMixin:
    
  142.             def new(self):
    
  143.                 super().new()
    
  144. 
    
  145.         class DeprecatedMixin:
    
  146.             def old(self):
    
  147.                 super().old()
    
  148. 
    
  149.         msg = "`DeprecatedMixin.old` method should be renamed `new`."
    
  150.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  151. 
    
  152.             class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
    
  153.                 pass
    
  154. 
    
  155.         deprecated = Deprecated()
    
  156. 
    
  157.         msg = "`RenamedMixin.old` is deprecated, use `new` instead."
    
  158.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  159.             deprecated.new()
    
  160. 
    
  161.         msg = "`DeprecatedMixin.old` is deprecated, use `new` instead."
    
  162.         with self.assertWarnsMessage(DeprecationWarning, msg):
    
  163.             deprecated.old()
    
  164. 
    
  165.     def test_removedafternextversionwarning_pending(self):
    
  166.         self.assertTrue(
    
  167.             issubclass(RemovedAfterNextVersionWarning, PendingDeprecationWarning)
    
  168.         )
    
  169. 
    
  170. 
    
  171. class DeprecationInstanceCheckTest(SimpleTestCase):
    
  172.     def test_warning(self):
    
  173.         class Manager(metaclass=DeprecationInstanceCheck):
    
  174.             alternative = "fake.path.Foo"
    
  175.             deprecation_warning = RemovedInNextVersionWarning
    
  176. 
    
  177.         msg = "`Manager` is deprecated, use `fake.path.Foo` instead."
    
  178.         with self.assertWarnsMessage(RemovedInNextVersionWarning, msg):
    
  179.             isinstance(object, Manager)