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