-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest_function_discovery.py
More file actions
592 lines (503 loc) · 21.8 KB
/
test_function_discovery.py
File metadata and controls
592 lines (503 loc) · 21.8 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
import tempfile
from pathlib import Path
import os
import unittest.mock
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,
filter_functions,
get_all_files_and_functions
)
from codeflash.verification.verification_utils import TestConfig
from codeflash.code_utils.compat import codeflash_temp_dir
def test_function_eligible_for_optimization() -> None:
function = """def test_function_eligible_for_optimization():
a = 5
return a**2
"""
functions_found = {}
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(function)
functions_found = find_all_functions_in_file(file_path)
assert functions_found[file_path][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.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(function)
functions_found = find_all_functions_in_file(file_path)
assert len(functions_found[file_path]) == 0
# we want to trigger an error in the function discovery
function = """def test_invalid_code():"""
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(function)
functions_found = find_all_functions_in_file(file_path)
assert functions_found == {}
def test_find_top_level_function_or_method():
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") 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
"""
)
assert inspect_top_level_functions_or_methods(file_path, "functionA").is_top_level
assert not inspect_top_level_functions_or_methods(file_path, "functionB").is_top_level
assert inspect_top_level_functions_or_methods(file_path, "functionC", class_name="A").is_top_level
assert not inspect_top_level_functions_or_methods(file_path, "functionD", class_name="A").is_top_level
assert not inspect_top_level_functions_or_methods(file_path, "functionF", class_name="E").is_top_level
assert not inspect_top_level_functions_or_methods(file_path, "functionA").has_args
staticmethod_func = inspect_top_level_functions_or_methods(
file_path, "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(
file_path, "functionE", class_name="AirbyteEntrypoint"
).is_classmethod
assert not inspect_top_level_functions_or_methods(
file_path, "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.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(
"""def functionA():
"""
)
assert not inspect_top_level_functions_or_methods(file_path, "functionA")
def test_class_method_discovery():
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") 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"""
)
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
functions, functions_count, _ = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=file_path,
only_get_this_function="A.functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.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=file_path,
only_get_this_function="X.functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.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=file_path,
only_get_this_function="functionA",
test_cfg=test_config,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.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.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") 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
"""
)
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
functions, functions_count, _ = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=file_path,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.parent,
)
assert len(functions) == 1
assert functions_count == 1
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(
"""
def outer_function():
def inner_function():
pass
return inner_function
"""
)
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
functions, functions_count, _ = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=file_path,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.parent,
)
assert len(functions) == 1
assert functions_count == 1
with tempfile.TemporaryDirectory() as temp_dir:
temp_dir_path = Path(temp_dir)
file_path = temp_dir_path / "test_function.py"
with file_path.open("w") as f:
f.write(
"""
def outer_function():
def inner_function():
pass
def another_inner_function():
pass
return inner_function, another_inner_function
"""
)
test_config = TestConfig(
tests_root="tests", project_root_path=".", test_framework="pytest", tests_project_rootdir=Path()
)
functions, functions_count, _ = get_functions_to_optimize(
optimize_all=None,
replay_test=None,
file=file_path,
test_cfg=test_config,
only_get_this_function=None,
ignore_paths=[Path("/bruh/")],
project_root=file_path.parent,
module_root=file_path.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)
def test_filter_functions():
with tempfile.TemporaryDirectory() as temp_dir_str:
temp_dir = Path(temp_dir_str)
# Create a test file in the temporary directory
test_file_path = temp_dir.joinpath("test_get_functions_to_optimize.py")
with test_file_path.open("w") 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
def vanilla_function():
return "This is a vanilla function."
def not_in_checkpoint_function():
return "This function is not in the checkpoint."
"""
)
discovered = find_all_functions_in_file(test_file_path)
modified_functions = {test_file_path: discovered[test_file_path]}
# Use an absolute path for tests_root that won't match the temp directory
# This avoids path resolution issues in CI where the working directory might differ
tests_root_absolute = (temp_dir.parent / "nonexistent_tests_dir").resolve()
with unittest.mock.patch("codeflash.discovery.functions_to_optimize.get_blocklisted_functions", return_value={}):
filtered, count = filter_functions(
modified_functions,
tests_root=tests_root_absolute,
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
function_names = [fn.function_name for fn in filtered.get(test_file_path, [])]
assert "propagate_attributes" in function_names
assert count == 3
# Create a tests directory inside our temp directory
tests_root_dir = temp_dir.joinpath("tests")
tests_root_dir.mkdir(exist_ok=True)
test_file_path = tests_root_dir.joinpath("test_functions.py")
with test_file_path.open("w") as f:
f.write(
"""
def test_function_in_tests_dir():
return "This function is in a test directory and should be filtered out."
"""
)
discovered_test_file = find_all_functions_in_file(test_file_path)
modified_functions_test = {test_file_path: discovered_test_file.get(test_file_path, [])}
filtered_test_file, count_test_file = filter_functions(
modified_functions_test,
tests_root=tests_root_dir,
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_test_file
assert count_test_file == 0
# Test ignored directory
ignored_dir = temp_dir.joinpath("ignored_dir")
ignored_dir.mkdir(exist_ok=True)
ignored_file_path = ignored_dir.joinpath("ignored_file.py")
with ignored_file_path.open("w") as f:
f.write("def ignored_func(): return 1")
discovered_ignored = find_all_functions_in_file(ignored_file_path)
modified_functions_ignored = {ignored_file_path: discovered_ignored.get(ignored_file_path, [])}
filtered_ignored, count_ignored = filter_functions(
modified_functions_ignored,
tests_root=Path("tests"),
ignore_paths=[ignored_dir],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_ignored
assert count_ignored == 0
# Test submodule paths
with unittest.mock.patch("codeflash.discovery.functions_to_optimize.ignored_submodule_paths",
return_value=[str(temp_dir.joinpath("submodule_dir"))]):
submodule_dir = temp_dir.joinpath("submodule_dir")
submodule_dir.mkdir(exist_ok=True)
submodule_file_path = submodule_dir.joinpath("submodule_file.py")
with submodule_file_path.open("w") as f:
f.write("def submodule_func(): return 1")
discovered_submodule = find_all_functions_in_file(submodule_file_path)
modified_functions_submodule = {submodule_file_path: discovered_submodule.get(submodule_file_path, [])}
filtered_submodule, count_submodule = filter_functions(
modified_functions_submodule,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_submodule
assert count_submodule == 0
# Test site packages
with unittest.mock.patch("codeflash.discovery.functions_to_optimize.path_belongs_to_site_packages",
return_value=True):
site_package_file_path = temp_dir.joinpath("site_package_file.py")
with site_package_file_path.open("w") as f:
f.write("def site_package_func(): return 1")
discovered_site_package = find_all_functions_in_file(site_package_file_path)
modified_functions_site_package = {site_package_file_path: discovered_site_package.get(site_package_file_path, [])}
filtered_site_package, count_site_package = filter_functions(
modified_functions_site_package,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_site_package
assert count_site_package == 0
# Test outside module root
parent_dir = temp_dir.parent
outside_module_root_path = parent_dir.joinpath("outside_module_root_file.py")
try:
with outside_module_root_path.open("w") as f:
f.write("def func_outside_module_root(): return 1")
discovered_outside_module = find_all_functions_in_file(outside_module_root_path)
modified_functions_outside_module = {outside_module_root_path: discovered_outside_module.get(outside_module_root_path, [])}
filtered_outside_module, count_outside_module = filter_functions(
modified_functions_outside_module,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_outside_module
assert count_outside_module == 0
finally:
outside_module_root_path.unlink(missing_ok=True)
# Test invalid module name
invalid_module_file_path = temp_dir.joinpath("invalid-module-name.py")
with invalid_module_file_path.open("w") as f:
f.write("def func_in_invalid_module(): return 1")
discovered_invalid_module = find_all_functions_in_file(invalid_module_file_path)
modified_functions_invalid_module = {invalid_module_file_path: discovered_invalid_module.get(invalid_module_file_path, [])}
filtered_invalid_module, count_invalid_module = filter_functions(
modified_functions_invalid_module,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert not filtered_invalid_module
assert count_invalid_module == 0
original_file_path = temp_dir.joinpath("test_get_functions_to_optimize.py")
with unittest.mock.patch("codeflash.discovery.functions_to_optimize.get_blocklisted_functions",
return_value={original_file_path.name: {"propagate_attributes", "other_blocklisted_function"}}):
filtered_funcs, count = filter_functions(
modified_functions,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
)
assert "propagate_attributes" not in [fn.function_name for fn in filtered_funcs.get(original_file_path, [])]
assert count == 2
module_name = "test_get_functions_to_optimize"
qualified_name_for_checkpoint = f"{module_name}.propagate_attributes"
other_qualified_name_for_checkpoint = f"{module_name}.vanilla_function"
with unittest.mock.patch("codeflash.discovery.functions_to_optimize.get_blocklisted_functions", return_value={}):
filtered_checkpoint, count_checkpoint = filter_functions(
modified_functions,
tests_root=Path("tests"),
ignore_paths=[],
project_root=temp_dir,
module_root=temp_dir,
previous_checkpoint_functions={qualified_name_for_checkpoint: {"status": "optimized"}, other_qualified_name_for_checkpoint: {}}
)
assert filtered_checkpoint.get(original_file_path)
assert count_checkpoint == 1
remaining_functions = [fn.function_name for fn in filtered_checkpoint.get(original_file_path, [])]
assert "not_in_checkpoint_function" in remaining_functions
assert "propagate_attributes" not in remaining_functions
assert "vanilla_function" not in remaining_functions
files_and_funcs = get_all_files_and_functions(module_root_path=temp_dir)
assert len(files_and_funcs) == 6