-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_function_discovery.py
More file actions
315 lines (281 loc) · 10.7 KB
/
Copy pathtest_function_discovery.py
File metadata and controls
315 lines (281 loc) · 10.7 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import tempfile
from pathlib import Path
from codeflash.discovery.functions_to_optimize import (
filter_files_optimized,
find_all_functions_in_file,
get_functions_to_optimize,
inspect_top_level_functions_or_methods,
)
from codeflash.verification.verification_utils import TestConfig
def test_function_eligible_for_optimization() -> None:
function = """def test_function_eligible_for_optimization():
a = 5
return a**2
"""
functions_found = {}
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(function)
f.flush()
functions_found = find_all_functions_in_file(Path(f.name))
assert functions_found[Path(f.name)][0].function_name == "test_function_eligible_for_optimization"
# Has no return statement
function = """def test_function_not_eligible_for_optimization():
a = 5
print(a)
"""
functions_found = {}
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(function)
f.flush()
functions_found = find_all_functions_in_file(Path(f.name))
assert len(functions_found[Path(f.name)]) == 0
# we want to trigger an error in the function discovery
function = """def test_invalid_code():"""
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(function)
f.flush()
functions_found = find_all_functions_in_file(Path(f.name))
assert functions_found == {}
def test_find_top_level_function_or_method():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""def functionA():
def functionB():
return 5
class E:
def functionF():
pass
return functionA()
class A:
def functionC():
def functionD():
pass
return 6
class AirbyteEntrypoint(object):
@staticmethod
def handle_record_counts(message: AirbyteMessage, stream_message_count: DefaultDict[HashableStreamDescriptor, float]) -> AirbyteMessage:
return "idontcare"
@classmethod
def functionE(cls, num):
return AirbyteEntrypoint.handle_record_counts(num)
def non_classmethod_function(cls, name):
return cls.name
"""
)
f.flush()
path_obj_name = Path(f.name)
assert inspect_top_level_functions_or_methods(path_obj_name, "functionA").is_top_level
assert not inspect_top_level_functions_or_methods(path_obj_name, "functionB").is_top_level
assert inspect_top_level_functions_or_methods(path_obj_name, "functionC", class_name="A").is_top_level
assert not inspect_top_level_functions_or_methods(path_obj_name, "functionD", class_name="A").is_top_level
assert not inspect_top_level_functions_or_methods(path_obj_name, "functionF", class_name="E").is_top_level
assert not inspect_top_level_functions_or_methods(path_obj_name, "functionA").has_args
staticmethod_func = inspect_top_level_functions_or_methods(
path_obj_name, "handle_record_counts", class_name=None, line_no=15
)
assert staticmethod_func.is_staticmethod
assert staticmethod_func.staticmethod_class_name == "AirbyteEntrypoint"
assert inspect_top_level_functions_or_methods(
path_obj_name, "functionE", class_name="AirbyteEntrypoint"
).is_classmethod
assert not inspect_top_level_functions_or_methods(
path_obj_name, "non_classmethod_function", class_name="AirbyteEntrypoint"
).is_top_level
# needed because this will be traced with a class_name being passed
# we want to write invalid code to ensure that the function discovery does not crash
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""def functionA():
"""
)
f.flush()
path_obj_name = Path(f.name)
assert not inspect_top_level_functions_or_methods(path_obj_name, "functionA")
def test_class_method_discovery():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""class A:
def functionA():
return True
def functionB():
return False
class X:
def functionA():
return True
def functionB():
return False
def functionA():
return True"""
)
f.flush()
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
path_obj_name = Path(f.name)
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
only_get_this_function="A.functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
for file in functions:
assert functions[file][0].qualified_name == "A.functionA"
assert functions[file][0].function_name == "functionA"
assert functions[file][0].top_level_parent_name == "A"
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
only_get_this_function="X.functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
for file in functions:
assert functions[file][0].qualified_name == "X.functionA"
assert functions[file][0].function_name == "functionA"
assert functions[file][0].top_level_parent_name == "X"
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
only_get_this_function="functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
for file in functions:
assert functions[file][0].qualified_name == "functionA"
assert functions[file][0].function_name == "functionA"
def test_nested_function():
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""
import copy
def propagate_attributes(
nodes: dict[str, dict], edges: list[dict], source_node_id: str, attribute: str
) -> dict[str, dict]:
modified_nodes = copy.deepcopy(nodes)
# Build an adjacency list for faster traversal
adjacency = {}
for edge in edges:
src = edge["source"]
tgt = edge["target"]
if src not in adjacency:
adjacency[src] = []
adjacency[src].append(tgt)
# Track visited nodes to avoid cycles
visited = set()
def traverse(node_id):
if node_id in visited:
return
visited.add(node_id)
# Propagate attribute from source node
if (
node_id != source_node_id
and source_node_id in modified_nodes
and attribute in modified_nodes[source_node_id]
):
if node_id in modified_nodes:
modified_nodes[node_id][attribute] = modified_nodes[source_node_id][
attribute
]
# Continue propagation to neighbors
for neighbor in adjacency.get(node_id, []):
traverse(neighbor)
traverse(source_node_id)
return modified_nodes
"""
)
f.flush()
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
path_obj_name = Path(f.name)
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
assert functions_count == 1
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""
def outer_function():
def inner_function():
pass
return inner_function
"""
)
f.flush()
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
path_obj_name = Path(f.name)
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
assert functions_count == 1
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as f:
f.write(
"""
def outer_function():
def inner_function():
pass
def another_inner_function():
pass
return inner_function, another_inner_function
"""
)
f.flush()
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
path_obj_name = Path(f.name)
functions, functions_count = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=path_obj_name,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=path_obj_name.parent,
module_root=path_obj_name.parent,
)
assert len(functions) == 1
assert functions_count == 1
def test_filter_files_optimized():
tests_root = Path("tests").resolve()
module_root = Path().resolve()
ignore_paths = []
file_path_test = Path("tests/test_function_discovery.py").resolve()
file_path_same_level = Path("file.py").resolve()
file_path_different_level = Path("src/file.py").resolve()
file_path_above_level = Path("../file.py").resolve()
assert not filter_files_optimized(file_path_test, tests_root, ignore_paths, module_root)
assert filter_files_optimized(file_path_same_level, tests_root, ignore_paths, module_root)
assert filter_files_optimized(file_path_different_level, tests_root, ignore_paths, module_root)
assert not filter_files_optimized(file_path_above_level, tests_root, ignore_paths, module_root)