forked from tantale/deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_deprecated.py
256 lines (190 loc) · 6.49 KB
/
test_deprecated.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
import warnings
import pytest
import deprecated.classic
class MyDeprecationWarning(DeprecationWarning):
pass
_PARAMS = [
None,
((), {}),
(('Good reason',), {}),
((), {'reason': 'Good reason'}),
((), {'version': '1.2.3'}),
((), {'action': 'once'}),
((), {'category': MyDeprecationWarning}),
]
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_function(request):
if request.param is None:
@deprecated.classic.deprecated
def foo1():
pass
return foo1
else:
args, kwargs = request.param
@deprecated.classic.deprecated(*args, **kwargs)
def foo1():
pass
return foo1
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_class(request):
if request.param is None:
@deprecated.classic.deprecated
class Foo2(object):
pass
return Foo2
else:
args, kwargs = request.param
@deprecated.classic.deprecated(*args, **kwargs)
class Foo2(object):
pass
return Foo2
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_method(request):
if request.param is None:
class Foo3(object):
@deprecated.classic.deprecated
def foo3(self):
pass
return Foo3
else:
args, kwargs = request.param
class Foo3(object):
@deprecated.classic.deprecated(*args, **kwargs)
def foo3(self):
pass
return Foo3
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_static_method(request):
if request.param is None:
class Foo4(object):
@staticmethod
@deprecated.classic.deprecated
def foo4():
pass
return Foo4.foo4
else:
args, kwargs = request.param
class Foo4(object):
@staticmethod
@deprecated.classic.deprecated(*args, **kwargs)
def foo4():
pass
return Foo4.foo4
@pytest.fixture(scope="module", params=_PARAMS)
def classic_deprecated_class_method(request):
if request.param is None:
class Foo5(object):
@classmethod
@deprecated.classic.deprecated
def foo5(cls):
pass
return Foo5
else:
args, kwargs = request.param
class Foo5(object):
@classmethod
@deprecated.classic.deprecated(*args, **kwargs)
def foo5(cls):
pass
return Foo5
# noinspection PyShadowingNames
def test_classic_deprecated_function__warns(classic_deprecated_function):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_function()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_class__warns(classic_deprecated_class):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_class()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated class" in str(warn.message)
assert warn.filename == __file__, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_method__warns(classic_deprecated_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
obj = classic_deprecated_method()
obj.foo3()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated method" in str(warn.message)
assert warn.filename == __file__, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_static_method__warns(classic_deprecated_static_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
classic_deprecated_static_method()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__, 'Incorrect warning stackLevel'
# noinspection PyShadowingNames
def test_classic_deprecated_class_method__warns(classic_deprecated_class_method):
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
cls = classic_deprecated_class_method()
cls.foo5()
assert len(warns) == 1
warn = warns[0]
assert issubclass(warn.category, DeprecationWarning)
assert "deprecated function (or staticmethod)" in str(warn.message)
assert warn.filename == __file__, 'Incorrect warning stackLevel'
def test_should_raise_type_error():
try:
deprecated.classic.deprecated(5)
assert False, "TypeError not raised"
except TypeError:
pass
def test_warning_msg_has_reason():
reason = "Good reason"
@deprecated.classic.deprecated(reason=reason)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert reason in str(warn.message)
def test_warning_msg_has_version():
version = "1.2.3"
@deprecated.classic.deprecated(version=version)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert version in str(warn.message)
def test_warning_is_ignored():
@deprecated.classic.deprecated(action='ignore')
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
assert len(warns) == 0
def test_specific_warning_cls_is_used():
@deprecated.classic.deprecated(category=MyDeprecationWarning)
def foo():
pass
with warnings.catch_warnings(record=True) as warns:
foo()
warn = warns[0]
assert issubclass(warn.category, MyDeprecationWarning)
def test_respect_global_filter():
@deprecated.classic.deprecated(version='1.2.1', reason="deprecated function")
def fun():
print("fun")
warnings.simplefilter("once", category=DeprecationWarning)
with warnings.catch_warnings(record=True) as warns:
fun()
fun()
assert len(warns) == 1