tests.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from __future__ import unicode_literals
  2. import warnings
  3. from django.test import SimpleTestCase, RequestFactory, override_settings
  4. from django.utils import six, translation
  5. from django.utils.deprecation import RenameMethodsBase
  6. from django.utils.functional import memoize
  7. class RenameManagerMethods(RenameMethodsBase):
  8. renamed_methods = (
  9. ('old', 'new', DeprecationWarning),
  10. )
  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. with warnings.catch_warnings(record=True) as recorded:
  22. warnings.simplefilter('always')
  23. class Manager(six.with_metaclass(RenameManagerMethods)):
  24. def old(self):
  25. pass
  26. self.assertEqual(len(recorded), 1)
  27. msg = str(recorded[0].message)
  28. self.assertEqual(msg,
  29. '`Manager.old` method should be renamed `new`.')
  30. def test_get_new_defined(self):
  31. """
  32. Ensure `old` complains and not `new` when only `new` is defined.
  33. """
  34. with warnings.catch_warnings(record=True) as recorded:
  35. warnings.simplefilter('ignore')
  36. class Manager(six.with_metaclass(RenameManagerMethods)):
  37. def new(self):
  38. pass
  39. warnings.simplefilter('always')
  40. manager = Manager()
  41. manager.new()
  42. self.assertEqual(len(recorded), 0)
  43. manager.old()
  44. self.assertEqual(len(recorded), 1)
  45. msg = str(recorded.pop().message)
  46. self.assertEqual(msg,
  47. '`Manager.old` is deprecated, use `new` instead.')
  48. def test_get_old_defined(self):
  49. """
  50. Ensure `old` complains when only `old` is defined.
  51. """
  52. with warnings.catch_warnings(record=True) as recorded:
  53. warnings.simplefilter('ignore')
  54. class Manager(six.with_metaclass(RenameManagerMethods)):
  55. def old(self):
  56. pass
  57. warnings.simplefilter('always')
  58. manager = Manager()
  59. manager.new()
  60. self.assertEqual(len(recorded), 0)
  61. manager.old()
  62. self.assertEqual(len(recorded), 1)
  63. msg = str(recorded.pop().message)
  64. self.assertEqual(msg,
  65. '`Manager.old` is deprecated, use `new` instead.')
  66. def test_deprecated_subclass_renamed(self):
  67. """
  68. Ensure the correct warnings are raised when a class that didn't rename
  69. `old` subclass one that did.
  70. """
  71. with warnings.catch_warnings(record=True) as recorded:
  72. warnings.simplefilter('ignore')
  73. class Renamed(six.with_metaclass(RenameManagerMethods)):
  74. def new(self):
  75. pass
  76. class Deprecated(Renamed):
  77. def old(self):
  78. super(Deprecated, self).old()
  79. warnings.simplefilter('always')
  80. deprecated = Deprecated()
  81. deprecated.new()
  82. self.assertEqual(len(recorded), 1)
  83. msg = str(recorded.pop().message)
  84. self.assertEqual(msg,
  85. '`Renamed.old` is deprecated, use `new` instead.')
  86. recorded[:] = []
  87. deprecated.old()
  88. self.assertEqual(len(recorded), 2)
  89. msgs = [str(warning.message) for warning in recorded]
  90. self.assertEqual(msgs, [
  91. '`Deprecated.old` is deprecated, use `new` instead.',
  92. '`Renamed.old` is deprecated, use `new` instead.',
  93. ])
  94. def test_renamed_subclass_deprecated(self):
  95. """
  96. Ensure the correct warnings are raised when a class that renamed
  97. `old` subclass one that didn't.
  98. """
  99. with warnings.catch_warnings(record=True) as recorded:
  100. warnings.simplefilter('ignore')
  101. class Deprecated(six.with_metaclass(RenameManagerMethods)):
  102. def old(self):
  103. pass
  104. class Renamed(Deprecated):
  105. def new(self):
  106. super(Renamed, self).new()
  107. warnings.simplefilter('always')
  108. renamed = Renamed()
  109. renamed.new()
  110. self.assertEqual(len(recorded), 0)
  111. renamed.old()
  112. self.assertEqual(len(recorded), 1)
  113. msg = str(recorded.pop().message)
  114. self.assertEqual(msg,
  115. '`Renamed.old` is deprecated, use `new` instead.')
  116. def test_deprecated_subclass_renamed_and_mixins(self):
  117. """
  118. Ensure the correct warnings are raised when a subclass inherit from a
  119. class that renamed `old` and mixins that may or may not have renamed
  120. `new`.
  121. """
  122. with warnings.catch_warnings(record=True) as recorded:
  123. warnings.simplefilter('ignore')
  124. class Renamed(six.with_metaclass(RenameManagerMethods)):
  125. def new(self):
  126. pass
  127. class RenamedMixin(object):
  128. def new(self):
  129. super(RenamedMixin, self).new()
  130. class DeprecatedMixin(object):
  131. def old(self):
  132. super(DeprecatedMixin, self).old()
  133. class Deprecated(DeprecatedMixin, RenamedMixin, Renamed):
  134. pass
  135. warnings.simplefilter('always')
  136. deprecated = Deprecated()
  137. deprecated.new()
  138. self.assertEqual(len(recorded), 1)
  139. msg = str(recorded.pop().message)
  140. self.assertEqual(msg,
  141. '`RenamedMixin.old` is deprecated, use `new` instead.')
  142. deprecated.old()
  143. self.assertEqual(len(recorded), 2)
  144. msgs = [str(warning.message) for warning in recorded]
  145. self.assertEqual(msgs, [
  146. '`DeprecatedMixin.old` is deprecated, use `new` instead.',
  147. '`RenamedMixin.old` is deprecated, use `new` instead.',
  148. ])
  149. class DeprecatingRequestMergeDictTest(SimpleTestCase):
  150. def test_deprecated_request(self):
  151. """
  152. Ensure the correct warning is raised when WSGIRequest.REQUEST is
  153. accessed.
  154. """
  155. with warnings.catch_warnings(record=True) as recorded:
  156. warnings.simplefilter('always')
  157. request = RequestFactory().get('/')
  158. request.REQUEST # evaluate
  159. msgs = [str(warning.message) for warning in recorded]
  160. self.assertEqual(msgs, [
  161. '`request.REQUEST` is deprecated, use `request.GET` or '
  162. '`request.POST` instead.',
  163. '`MergeDict` is deprecated, use `dict.update()` instead.',
  164. ])
  165. @override_settings(USE_I18N=True)
  166. class DeprecatedChineseLanguageCodes(SimpleTestCase):
  167. def test_deprecation_warning(self):
  168. warnings.simplefilter('always')
  169. with warnings.catch_warnings(record=True) as recorded:
  170. with translation.override('zh-cn'):
  171. pass
  172. with translation.override('zh-tw'):
  173. pass
  174. msgs = [str(warning.message) for warning in recorded]
  175. self.assertEqual(msgs, [
  176. "The use of the language code 'zh-cn' is deprecated. "
  177. "Please use the 'zh-hans' translation instead.",
  178. "The use of the language code 'zh-tw' is deprecated. "
  179. "Please use the 'zh-hant' translation instead.",
  180. ])
  181. class DeprecatingMemoizeTest(SimpleTestCase):
  182. def test_deprecated_memoize(self):
  183. """
  184. Ensure the correct warning is raised when memoize is used.
  185. """
  186. warnings.simplefilter('always')
  187. with warnings.catch_warnings(record=True) as recorded:
  188. memoize(lambda x: x, {}, 1)
  189. msg = str(recorded.pop().message)
  190. self.assertEqual(msg,
  191. 'memoize wrapper is deprecated and will be removed in Django '
  192. '1.9. Use django.utils.lru_cache instead.')