|
11 | 11 | from codeflash.discovery.functions_to_optimize import FunctionToOptimize |
12 | 12 | from codeflash.models.models import FunctionParent |
13 | 13 | from codeflash.optimization.optimizer import Optimizer |
| 14 | +from codeflash.code_utils.code_replacer import replace_functions_and_add_imports |
| 15 | +from codeflash.code_utils.code_extractor import add_global_assignments |
14 | 16 |
|
15 | 17 |
|
16 | 18 | class HelperClass: |
@@ -2434,3 +2436,22 @@ def simple_method(self): |
2434 | 2436 | assert "class SimpleClass:" in code_content |
2435 | 2437 | assert "def simple_method(self):" in code_content |
2436 | 2438 | assert "return 42" in code_content |
| 2439 | + |
| 2440 | + |
| 2441 | + |
| 2442 | +def test_replace_functions_and_add_imports(): |
| 2443 | + path_to_root = Path(__file__).resolve().parent.parent / "code_to_optimize" / "code_directories" / "circular_deps" |
| 2444 | + file_abs_path = path_to_root / "api_client.py" |
| 2445 | + optimized_code = Path(path_to_root / "optimized.py").read_text(encoding="utf-8") |
| 2446 | + content = Path(file_abs_path).read_text(encoding="utf-8") |
| 2447 | + new_code = replace_functions_and_add_imports( |
| 2448 | + source_code= add_global_assignments(optimized_code, content), |
| 2449 | + function_names= ["ApiClient.get_console_url"], |
| 2450 | + optimized_code= optimized_code, |
| 2451 | + module_abspath= Path(file_abs_path), |
| 2452 | + preexisting_objects= {('ApiClient', ()), ('get_console_url', (FunctionParent(name='ApiClient', type='ClassDef'),))}, |
| 2453 | + project_root_path= Path(path_to_root), |
| 2454 | + ) |
| 2455 | + assert "import ApiClient" not in new_code, "Error: Circular dependency found" |
| 2456 | + |
| 2457 | + assert "import urllib.parse" in new_code, "Make sure imports for optimization global assignments exist" |
0 commit comments