|
20 | 20 | validate_python_code,
|
21 | 21 | )
|
22 | 22 | from codeflash.code_utils.concolic_utils import clean_concolic_tests
|
23 |
| -from codeflash.code_utils.coverage_utils import generate_candidates, prepare_coverage_files |
| 23 | +from codeflash.code_utils.coverage_utils import extract_dependent_function, generate_candidates, prepare_coverage_files |
24 | 24 |
|
25 | 25 |
|
26 | 26 | @pytest.fixture
|
@@ -308,6 +308,86 @@ def my_function():
|
308 | 308 | assert is_class_defined_in_file("MyClass", test_file) is False
|
309 | 309 |
|
310 | 310 |
|
| 311 | +@pytest.fixture |
| 312 | +def mock_code_context(): |
| 313 | + """Mock CodeOptimizationContext for testing extract_dependent_function.""" |
| 314 | + from unittest.mock import MagicMock |
| 315 | + from codeflash.models.models import CodeOptimizationContext |
| 316 | + |
| 317 | + context = MagicMock(spec=CodeOptimizationContext) |
| 318 | + context.preexisting_objects = [] |
| 319 | + return context |
| 320 | + |
| 321 | + |
| 322 | +def test_extract_dependent_function_sync_and_async(mock_code_context): |
| 323 | + """Test extract_dependent_function with both sync and async functions.""" |
| 324 | + # Test sync function extraction |
| 325 | + mock_code_context.testgen_context_code = """ |
| 326 | +def main_function(): |
| 327 | + pass |
| 328 | +
|
| 329 | +def helper_function(): |
| 330 | + pass |
| 331 | +""" |
| 332 | + assert extract_dependent_function("main_function", mock_code_context) == "helper_function" |
| 333 | + |
| 334 | + # Test async function extraction |
| 335 | + mock_code_context.testgen_context_code = """ |
| 336 | +def main_function(): |
| 337 | + pass |
| 338 | +
|
| 339 | +async def async_helper_function(): |
| 340 | + pass |
| 341 | +""" |
| 342 | + assert extract_dependent_function("main_function", mock_code_context) == "async_helper_function" |
| 343 | + |
| 344 | + |
| 345 | +def test_extract_dependent_function_edge_cases(mock_code_context): |
| 346 | + """Test extract_dependent_function edge cases.""" |
| 347 | + # No dependent functions |
| 348 | + mock_code_context.testgen_context_code = """ |
| 349 | +def main_function(): |
| 350 | + pass |
| 351 | +""" |
| 352 | + assert extract_dependent_function("main_function", mock_code_context) is False |
| 353 | + |
| 354 | + # Multiple dependent functions |
| 355 | + mock_code_context.testgen_context_code = """ |
| 356 | +def main_function(): |
| 357 | + pass |
| 358 | +
|
| 359 | +def helper1(): |
| 360 | + pass |
| 361 | +
|
| 362 | +async def helper2(): |
| 363 | + pass |
| 364 | +""" |
| 365 | + assert extract_dependent_function("main_function", mock_code_context) is False |
| 366 | + |
| 367 | + |
| 368 | +def test_extract_dependent_function_mixed_scenarios(mock_code_context): |
| 369 | + """Test extract_dependent_function with mixed sync/async scenarios.""" |
| 370 | + # Async main with sync helper |
| 371 | + mock_code_context.testgen_context_code = """ |
| 372 | +async def async_main(): |
| 373 | + pass |
| 374 | +
|
| 375 | +def sync_helper(): |
| 376 | + pass |
| 377 | +""" |
| 378 | + assert extract_dependent_function("async_main", mock_code_context) == "sync_helper" |
| 379 | + |
| 380 | + # Only async functions |
| 381 | + mock_code_context.testgen_context_code = """ |
| 382 | +async def async_main(): |
| 383 | + pass |
| 384 | +
|
| 385 | +async def async_helper(): |
| 386 | + pass |
| 387 | +""" |
| 388 | + assert extract_dependent_function("async_main", mock_code_context) == "async_helper" |
| 389 | + |
| 390 | + |
311 | 391 | def test_is_class_defined_in_file_with_non_existing_file() -> None:
|
312 | 392 | non_existing_file = Path("/non/existing/file.py")
|
313 | 393 |
|
|
0 commit comments