tests.py 5.6 KB

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