-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_tracking.py
292 lines (223 loc) · 8.25 KB
/
test_tracking.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import os
import pathlib
import tempfile
import pytest
from pytest_func_cov import tracking
from .test_package.classes import SimpleClass
from .test_package.functions import simple_function, lambda_
from .test_package import functions
def create_files(*files):
"""
Creates files at given paths with given contents.
Args:
*files (Tuple[str, Optional[Any]]): first element is the absolute path to
a file to be created, and the second is used as the file content. If the content
is None the file will be empty.
"""
for path, contents in files:
with open(path, "w") as f:
if contents is not None:
f.write(contents)
@pytest.mark.parametrize(
"func",
[
simple_function,
lambda_,
SimpleClass.simple_method,
SimpleClass.simple_class_method,
SimpleClass.simple_static_method,
],
ids=["function", "lambda", "method", "class method", "static method"],
)
def test_get_full_function_name_correct_for_simple_function(func):
expected_name = f"{func.__module__}.{func.__qualname__}"
assert tracking.get_full_function_name(func) == expected_name
@pytest.fixture
def package_path():
with tempfile.TemporaryDirectory() as folder:
with open(os.path.join(folder, "__init__.py"), "w"):
yield folder
@pytest.fixture(scope="module")
def non_package_path():
with tempfile.TemporaryDirectory() as folder:
yield folder
@pytest.fixture
def directory_with_packages():
"""
Returns
tuple(str, list(str)): First element is the absolute path to the root folder,
the second element is the list of expected package names
"""
with tempfile.TemporaryDirectory() as root_folder:
with tempfile.TemporaryDirectory(
prefix=f"{os.path.basename(root_folder)}/"
) as package_folder:
with open(os.path.join(package_folder, "__init__.py"), "w"):
yield root_folder, [package_folder]
def test_get_methods_defined_in_class():
output = [m[1] for m in tracking.get_methods_defined_in_class(SimpleClass)]
expected = [
SimpleClass.__init__,
SimpleClass.simple_class_method,
SimpleClass.simple_method,
SimpleClass.simple_static_method,
]
assert len(output) == len(expected) and all(
[method in output for method in expected]
)
@pytest.fixture(scope="package")
def nested_package_directory():
"""
Creates the following structure (directory names are random):
package/
__init__.py
module1.py
non_py_file.ext
subpackage1/
__init__.py
module2.py
subpackage2/
module3.py
Returns
Tuple[str, Tuple[Tuple[str, str], ...]]: First element is the absolute path
to the created directory, and the second is a tuple of tuples, where the first
element is the absolute path to a .py file, and the second one is the name under
which is should be imported.
"""
with tempfile.TemporaryDirectory() as package_folder:
# Create inner package directories
pathlib.Path(os.path.join(package_folder, "subpackage1", "subpackage2")).mkdir(
parents=True
)
files_to_create = (
(os.path.join(package_folder, "__init__.py"), None),
(os.path.join(package_folder, "module1.py"), None),
(os.path.join(package_folder, "non_py_file.ext"), None),
(os.path.join(package_folder, "subpackage1", "__init__.py"), None),
(os.path.join(package_folder, "subpackage1", "module2.py"), None),
(
os.path.join(
package_folder, "subpackage1", "subpackage2", "module3.py"
),
None,
),
)
create_files(*files_to_create)
# Construct expected modulse
package = os.path.basename(package_folder)
expected_modules = (
(files_to_create[0][0], package),
(files_to_create[1][0], f"{package}.module1"),
(files_to_create[3][0], f"{package}.subpackage1"),
(files_to_create[4][0], f"{package}.subpackage1.module2"),
(files_to_create[5][0], f"{package}.subpackage1.subpackage2.module3"),
)
yield package_folder, expected_modules
def test_find_module_in_nested_package_directory(nested_package_directory):
directory, expected = nested_package_directory
output = tuple(tracking.find_modules(directory))
# Order is not guaranteed
assert sorted(output) == sorted(expected)
def test_get_functions_defined_in_module():
output = tracking.get_functions_defined_in_module(functions)
expected = [
("simple_function", functions.simple_function),
("lambda_", functions.lambda_),
]
assert all([function in output for function in expected])
@pytest.fixture(scope="function")
def empty_fcm():
fcm = tracking.FunctionCallMonitor()
fcm.register_target_module(__file__)
return fcm
def test_fcm_correctly_tracks_function(empty_fcm):
def f():
return 42
dec_f = empty_fcm.register_function(f)
assert (
(dec_f() == 42)
and (empty_fcm.registered_functions == ((f.__module__, (f,)),))
and (empty_fcm.called_functions == ((f.__module__, (f,)),))
)
def test_fcm_correctly_tracks_method(empty_fcm):
class A:
def m(self):
return 42
orig_m = A.m
A.m = empty_fcm.register_function(A.m)
assert (
(A().m() == 42)
and (empty_fcm.registered_functions == ((orig_m.__module__, (orig_m,)),))
and (empty_fcm.called_functions == ((orig_m.__module__, (orig_m,)),))
)
def test_fcm_correctly_tracks_classmethod(empty_fcm):
class A:
@classmethod
def cm(cls):
return 42
orig_cm = A.cm
A.cm = empty_fcm.register_function(A.cm, A)
assert (
(A.cm() == 42)
and (
empty_fcm.registered_functions
== ((orig_cm.__module__, (orig_cm.__func__,)),)
)
and (empty_fcm.called_functions == ((orig_cm.__module__, (orig_cm.__func__,)),))
)
def test_fcm_returns_correct_missed(empty_fcm):
def f():
pass
orig_f = f
empty_fcm.register_function(f)
assert empty_fcm.missed_functions == ((orig_f.__module__, (orig_f,)),)
def test_fcm_does_not_track_against_unregistered_targets():
fcm = tracking.FunctionCallMonitor()
def f():
pass
orig_f = f
f = fcm.register_function(f)
f()
assert fcm.missed_functions == ((orig_f.__module__, (orig_f,)),)
def test_fcm_record_call_raises_monitoring_error_if_f_not_tracked(empty_fcm):
with pytest.raises(tracking.MonitoringError):
empty_fcm.record_call(
lambda x: x,
__file__,
test_fcm_record_call_raises_monitoring_error_if_f_not_tracked,
)
def test_fcm_record_call_returns_false_if_source_not_registered(empty_fcm):
f = lambda x: x
dec_f = empty_fcm.register_function(f)
# Use other file as source as the source
assert not empty_fcm.record_call(f, "not/a/path", None)
def test_fcm_record_call_returns_true_if_source_registered(empty_fcm):
f = lambda x: x
dec_f = empty_fcm.register_function(f)
assert empty_fcm.record_call(f, __file__, None)
def test_module_loader_loads_all_modules_from_package(nested_package_directory):
directory, expected_modules = nested_package_directory
ml = tracking.ModuleLoader()
ml.load_from_package(directory)
module_paths, module_names = zip(*expected_modules)
assert all(
module_name in module_names
and module_obj.__file__ == module_paths[module_names.index(module_name)]
for module_name, module_obj in ml
)
@pytest.fixture(scope="package")
def fi_with_filters():
filters = ("^test_", "_end$")
return tracking.FunctionIndexer(filters)
@pytest.mark.parametrize(
["func_name", "expected_result"],
[
("test_x", True),
("atest_x", False),
("test_", True),
("x_end", True),
("x_end_x", False),
],
)
def test_fi_matches_filters(fi_with_filters, func_name, expected_result):
assert fi_with_filters.matches_filters(func_name) is expected_result