tests.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from __future__ import unicode_literals
  2. import os
  3. import unittest
  4. import warnings
  5. from django.test import SimpleTestCase, override_settings
  6. from django.test.utils import reset_warning_registry
  7. from django.utils import six, translation
  8. from django.utils.deprecation import RenameMethodsBase
  9. from django.utils.encoding import force_text
  10. class RenameManagerMethods(RenameMethodsBase):
  11. renamed_methods = (
  12. ('old', 'new', DeprecationWarning),
  13. )
  14. class RenameMethodsTests(SimpleTestCase):
  15. """
  16. Tests the `RenameMethodsBase` type introduced to rename `get_query_set`
  17. to `get_queryset` across the code base following #15363.
  18. """
  19. def test_class_definition_warnings(self):
  20. """
  21. Ensure a warning is raised upon class definition to suggest renaming
  22. the faulty method.
  23. """
  24. reset_warning_registry()
  25. with warnings.catch_warnings(record=True) as recorded:
  26. warnings.simplefilter('always')
  27. class Manager(six.with_metaclass(RenameManagerMethods)):
  28. def old(self):
  29. pass
  30. self.assertEqual(len(recorded), 1)
  31. msg = str(recorded[0].message)
  32. self.assertEqual(msg,
  33. '`Manager.old` method should be renamed `new`.')
  34. def test_get_new_defined(self):
  35. """
  36. Ensure `old` complains and not `new` when only `new` is defined.
  37. """
  38. with warnings.catch_warnings(record=True) as recorded:
  39. warnings.simplefilter('ignore')
  40. class Manager(six.with_metaclass(RenameManagerMethods)):
  41. def new(self):
  42. pass
  43. warnings.simplefilter('always')
  44. manager = Manager()
  45. manager.new()
  46. self.assertEqual(len(recorded), 0)
  47. manager.old()
  48. self.assertEqual(len(recorded), 1)
  49. msg = str(recorded.pop().message)
  50. self.assertEqual(msg,
  51. '`Manager.old` is deprecated, use `new` instead.')
  52. def test_get_old_defined(self):
  53. """
  54. Ensure `old` complains when only `old` is defined.
  55. """
  56. with warnings.catch_warnings(record=True) as recorded:
  57. warnings.simplefilter('ignore')
  58. class Manager(six.with_metaclass(RenameManagerMethods)):
  59. def old(self):
  60. pass
  61. warnings.simplefilter('always')
  62. manager = Manager()
  63. manager.new()
  64. self.assertEqual(len(recorded), 0)
  65. manager.old()
  66. self.assertEqual(len(recorded), 1)
  67. msg = str(recorded.pop().message)
  68. self.assertEqual(msg,
  69. '`Manager.old` is deprecated, use `new` instead.')
  70. def test_deprecated_subclass_renamed(self):
  71. """
  72. Ensure the correct warnings are raised when a class that didn't rename
  73. `old` subclass one that did.
  74. """
  75. with warnings.catch_warnings(record=True) as recorded:
  76. warnings.simplefilter('ignore')
  77. class Renamed(six.with_metaclass(RenameManagerMethods)):
  78. def new(self):
  79. pass
  80. class Deprecated(Renamed):
  81. def old(self):
  82. super(Deprecated, self).old()
  83. warnings.simplefilter('always')
  84. deprecated = Deprecated()
  85. deprecated.new()
  86. self.assertEqual(len(recorded), 1)
  87. msg = str(recorded.pop().message)
  88. self.assertEqual(msg,
  89. '`Renamed.old` is deprecated, use `new` instead.')
  90. recorded[:] = []
  91. deprecated.old()
  92. self.assertEqual(len(recorded), 2)
  93. msgs = [str(warning.message) for warning in recorded]
  94. self.assertEqual(msgs, [
  95. '`Deprecated.old` is deprecated, use `new` instead.',
  96. '`Renamed.old` is deprecated, use `new` instead.',
  97. ])
  98. def test_renamed_subclass_deprecated(self):
  99. """
  100. Ensure the correct warnings are raised when a class that renamed
  101. `old` subclass one that didn't.
  102. """
  103. with warnings.catch_warnings(record=True) as recorded:
  104. warnings.simplefilter('ignore')
  105. class Deprecated(six.with_metaclass(RenameManagerMethods)):
  106. def old(self):
  107. pass
  108. class Renamed(Deprecated):
  109. def new(self):
  110. super(Renamed, self).new()
  111. warnings.simplefilter('always')
  112. renamed = Renamed()
  113. renamed.new()
  114. self.assertEqual(len(recorded), 0)
  115. renamed.old()
  116. self.assertEqual(len(recorded), 1)
  117. msg = str(recorded.pop().message)
  118. self.assertEqual(msg,
  119. '`Renamed.old` is deprecated, use `new` instead.')
  120. def test_deprecated_subclass_renamed_and_mixins(self):
  121. """
  122. Ensure the correct warnings are raised when a subclass inherit from a
  123. class that renamed `old` and mixins that may or may not have renamed
  124. `new`.
  125. """
  126. with warnings.catch_warnings(record=True) as recorded:
  127. warnings.simplefilter('ignore')
  128. class Renamed(six.with_metaclass(RenameManagerMethods)):
  129. def new(self):
  130. pass
  131. class RenamedMixin(object):
  132. def new(self):
  133. super(RenamedMixin, self).new()
  134. class DeprecatedMixin(object):
  135. def old(self):
  136. super(DeprecatedMixin, self).old()
  137. class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
  138. pass
  139. warnings.simplefilter('always')
  140. deprecated = Deprecated()
  141. deprecated.new()
  142. self.assertEqual(len(recorded), 1)
  143. msg = str(recorded.pop().message)
  144. self.assertEqual(msg,
  145. '`RenamedMixin.old` is deprecated, use `new` instead.')
  146. deprecated.old()
  147. self.assertEqual(len(recorded), 2)
  148. msgs = [str(warning.message) for warning in recorded]
  149. self.assertEqual(msgs, [
  150. '`DeprecatedMixin.old` is deprecated, use `new` instead.',
  151. '`RenamedMixin.old` is deprecated, use `new` instead.',
  152. ])
  153. @override_settings(USE_I18N=True)
  154. class DeprecatedChineseLanguageCodes(SimpleTestCase):
  155. def test_deprecation_warning(self):
  156. with warnings.catch_warnings(record=True) as recorded:
  157. warnings.simplefilter('always')
  158. with translation.override('zh-cn'):
  159. pass
  160. with translation.override('zh-tw'):
  161. pass
  162. msgs = [str(warning.message) for warning in recorded]
  163. self.assertEqual(msgs, [
  164. "The use of the language code 'zh-cn' is deprecated. "
  165. "Please use the 'zh-hans' translation instead.",
  166. "The use of the language code 'zh-tw' is deprecated. "
  167. "Please use the 'zh-hant' translation instead.",
  168. ])
  169. class DeprecatingSimpleTestCaseUrls(unittest.TestCase):
  170. def test_deprecation(self):
  171. """
  172. Ensure the correct warning is raised when SimpleTestCase.urls is used.
  173. """
  174. class TempTestCase(SimpleTestCase):
  175. urls = 'tests.urls'
  176. def test(self):
  177. pass
  178. with warnings.catch_warnings(record=True) as recorded:
  179. warnings.filterwarnings('always')
  180. suite = unittest.TestLoader().loadTestsFromTestCase(TempTestCase)
  181. with open(os.devnull, 'w') as devnull:
  182. unittest.TextTestRunner(stream=devnull, verbosity=2).run(suite)
  183. msg = force_text(recorded.pop().message)
  184. self.assertEqual(msg,
  185. "SimpleTestCase.urls is deprecated and will be removed in "
  186. "Django 2.0. Use @override_settings(ROOT_URLCONF=...) "
  187. "in TempTestCase instead.")