|
8 | 8 | filter_test_files_by_imports, |
9 | 9 | ) |
10 | 10 | from codeflash.discovery.functions_to_optimize import FunctionToOptimize |
11 | | -from codeflash.models.models import TestsInFile, TestType |
| 11 | +from codeflash.models.models import TestsInFile, TestType, FunctionParent |
12 | 12 | from codeflash.verification.verification_utils import TestConfig |
13 | 13 |
|
14 | 14 |
|
@@ -714,6 +714,61 @@ def test_add_with_parameters(self): |
714 | 714 | assert calculator_test.tests_in_file.test_file.resolve() == test_file_path.resolve() |
715 | 715 | assert calculator_test.tests_in_file.test_function == "test_add_with_parameters" |
716 | 716 |
|
| 717 | +def test_unittest_discovery_with_pytest_fixture(): |
| 718 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 719 | + path_obj_tmpdirname = Path(tmpdirname) |
| 720 | + |
| 721 | + # Create a simple code file |
| 722 | + code_file_path = path_obj_tmpdirname / "topological_sort.py" |
| 723 | + code_file_content = """ |
| 724 | +import uuid |
| 725 | +from collections import defaultdict |
| 726 | +
|
| 727 | +
|
| 728 | +class Graph: |
| 729 | + def __init__(self, vertices: int): |
| 730 | + self.vertices=vertices |
| 731 | +
|
| 732 | + def topologicalSort(self): |
| 733 | + return self.vertices |
| 734 | +
|
| 735 | +""" |
| 736 | + code_file_path.write_text(code_file_content) |
| 737 | + |
| 738 | + # Create a unittest test file with parameterized tests |
| 739 | + test_file_path = path_obj_tmpdirname / "test_topological_sort.py" |
| 740 | + test_file_content = """ |
| 741 | +from topological_sort import Graph |
| 742 | +import pytest |
| 743 | +
|
| 744 | +@pytest.fixture |
| 745 | +def g(self): |
| 746 | + return Graph(6) |
| 747 | +
|
| 748 | +def test_topological_sort(g): |
| 749 | + assert g.topologicalSort() == 6 |
| 750 | +""" |
| 751 | + test_file_path.write_text(test_file_content) |
| 752 | + |
| 753 | + # Configure test discovery |
| 754 | + test_config = TestConfig( |
| 755 | + tests_root=path_obj_tmpdirname, |
| 756 | + project_root_path=path_obj_tmpdirname, |
| 757 | + test_framework="pytest", # Using pytest framework to discover unittest tests |
| 758 | + tests_project_rootdir=path_obj_tmpdirname.parent, |
| 759 | + ) |
| 760 | + fto = FunctionToOptimize(function_name="topologicalSort", file_path=code_file_path, parents=[FunctionParent(name="Graph", type="ClassDef")]) |
| 761 | + # Discover tests |
| 762 | + discovered_tests, _, _ = discover_unit_tests(test_config, file_to_funcs_to_optimize={code_file_path: [fto]}) |
| 763 | + |
| 764 | + # Verify the unittest was discovered |
| 765 | + assert len(discovered_tests) == 1 |
| 766 | + assert "topological_sort.Graph.topologicalSort" in discovered_tests |
| 767 | + assert len(discovered_tests["topological_sort.Graph.topologicalSort"]) == 1 |
| 768 | + tpsort_test = next(iter(discovered_tests["topological_sort.Graph.topologicalSort"])) |
| 769 | + assert tpsort_test.tests_in_file.test_file.resolve() == test_file_path.resolve() |
| 770 | + assert tpsort_test.tests_in_file.test_function == "test_topological_sort" |
| 771 | + |
717 | 772 |
|
718 | 773 | def test_unittest_discovery_with_pytest_parameterized(): |
719 | 774 | with tempfile.TemporaryDirectory() as tmpdirname: |
@@ -1335,6 +1390,34 @@ def test_topological_sort(): |
1335 | 1390 |
|
1336 | 1391 | assert should_process is True |
1337 | 1392 |
|
| 1393 | +def test_analyze_imports_fixture(): |
| 1394 | + with tempfile.TemporaryDirectory() as tmpdirname: |
| 1395 | + test_file = Path(tmpdirname) / "test_example.py" |
| 1396 | + test_content = """ |
| 1397 | +from code_to_optimize.topological_sort import Graph |
| 1398 | +import pytest |
| 1399 | +
|
| 1400 | +@pytest.fixture |
| 1401 | +def g(self): |
| 1402 | + return Graph(6) |
| 1403 | +
|
| 1404 | +def test_topological_sort(g): |
| 1405 | + g.addEdge(5, 2) |
| 1406 | + g.addEdge(5, 0) |
| 1407 | + g.addEdge(4, 0) |
| 1408 | + g.addEdge(4, 1) |
| 1409 | + g.addEdge(2, 3) |
| 1410 | + g.addEdge(3, 1) |
| 1411 | +
|
| 1412 | + assert g.topologicalSort()[0] == [5, 4, 2, 3, 1, 0] |
| 1413 | +""" |
| 1414 | + test_file.write_text(test_content) |
| 1415 | + |
| 1416 | + target_functions = {"Graph.topologicalSort"} |
| 1417 | + should_process = analyze_imports_in_test_file(test_file, target_functions) |
| 1418 | + |
| 1419 | + assert should_process is True |
| 1420 | + |
1338 | 1421 | def test_analyze_imports_aliased_class_method_negative(): |
1339 | 1422 | with tempfile.TemporaryDirectory() as tmpdirname: |
1340 | 1423 | test_file = Path(tmpdirname) / "test_example.py" |
|
0 commit comments