forked from MemTensor/MemOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_deprecation.py
More file actions
174 lines (128 loc) · 5.45 KB
/
test_deprecation.py
File metadata and controls
174 lines (128 loc) · 5.45 KB
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
import warnings
from src.memos.deprecation import (
deprecated,
deprecated_class,
deprecated_parameter,
get_deprecation_info,
is_deprecated,
warn_deprecated,
)
class TestDeprecated:
"""Test the @deprecated decorator"""
def test_deprecated_function_warns(self):
"""Test that deprecated function issues warning"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
@deprecated(reason="Test reason", version="1.0.0", alternative="new_func")
def old_func():
return "result"
result = old_func()
assert result == "result"
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "old_func" in str(w[0].message)
assert "Test reason" in str(w[0].message)
assert "1.0.0" in str(w[0].message)
assert "new_func" in str(w[0].message)
def test_deprecated_function_metadata(self):
"""Test that deprecated function has correct metadata"""
@deprecated(reason="Test", version="1.0.0", alternative="new_func")
def old_func():
return "result"
assert is_deprecated(old_func)
info = get_deprecation_info(old_func)
assert info["reason"] == "Test"
assert info["version"] == "1.0.0"
assert info["alternative"] == "new_func"
def test_deprecated_minimal(self):
"""Test deprecated decorator with minimal parameters"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
@deprecated()
def old_func():
return "result"
result = old_func()
assert result == "result"
assert len(w) == 1
assert "old_func" in str(w[0].message)
class TestDeprecatedClass:
"""Test the @deprecated_class decorator"""
def test_deprecated_class_warns(self):
"""Test that deprecated class issues warning on instantiation"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
@deprecated_class(reason="Test reason", version="1.0.0", alternative="NewClass")
class OldClass:
def __init__(self, value):
self.value = value
obj = OldClass("test")
assert obj.value == "test"
assert len(w) == 1
assert issubclass(w[0].category, DeprecationWarning)
assert "OldClass" in str(w[0].message)
assert "Test reason" in str(w[0].message)
def test_deprecated_class_metadata(self):
"""Test that deprecated class has correct metadata"""
@deprecated_class(reason="Test", version="1.0.0")
class OldClass:
pass
assert is_deprecated(OldClass)
info = get_deprecation_info(OldClass)
assert info["reason"] == "Test"
assert info["version"] == "1.0.0"
class TestDeprecatedParameter:
"""Test the @deprecated_parameter decorator"""
def test_deprecated_parameter_warns(self):
"""Test that deprecated parameter issues warning when used"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
@deprecated_parameter("old_param", alternative="new_param", version="1.0.0")
def test_func(new_param=None, old_param=None):
return new_param or old_param
# Using new parameter should not warn
result1 = test_func(new_param="new_value")
assert result1 == "new_value"
assert len(w) == 0
# Using old parameter should warn
result2 = test_func(old_param="old_value")
assert result2 == "old_value"
assert len(w) == 1
assert "old_param" in str(w[0].message)
assert "new_param" in str(w[0].message)
class TestWarnDeprecated:
"""Test the warn_deprecated function"""
def test_warn_deprecated_basic(self):
"""Test basic deprecation warning"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
warn_deprecated(
"old_item", "function", reason="Test", version="1.0.0", alternative="new_item"
)
assert len(w) == 1
assert "old_item" in str(w[0].message)
assert "Test" in str(w[0].message)
assert "1.0.0" in str(w[0].message)
assert "new_item" in str(w[0].message)
def test_warn_deprecated_minimal(self):
"""Test deprecation warning with minimal parameters"""
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
warn_deprecated("old_item")
assert len(w) == 1
assert "old_item" in str(w[0].message)
class TestDeprecationUtilities:
"""Test utility functions"""
def test_is_deprecated_false(self):
"""Test is_deprecated returns False for non-deprecated items"""
def normal_func():
pass
class NormalClass:
pass
assert not is_deprecated(normal_func)
assert not is_deprecated(NormalClass)
assert not is_deprecated("string")
def test_get_deprecation_info_none(self):
"""Test get_deprecation_info returns None for non-deprecated items"""
def normal_func():
pass
assert get_deprecation_info(normal_func) is None