@@ -230,7 +230,7 @@ def test_sort():
230230 test_path_perf .unlink (missing_ok = True )
231231
232232
233- def test_class_method_full_instrumentation () -> None :
233+ def test_method_full_instrumentation () -> None :
234234 code = """from code_to_optimize.bubble_sort_method import BubbleSorter
235235
236236
@@ -493,6 +493,334 @@ def sorter(self, arr):
493493 assert new_test_results [3 ].did_pass
494494 assert not compare_test_results (test_results , new_test_results )
495495
496+ finally :
497+ fto_path .write_text (original_code , "utf-8" )
498+ test_path .unlink (missing_ok = True )
499+ test_path_perf .unlink (missing_ok = True )
500+
501+
502+ def test_classmethod_full_instrumentation () -> None :
503+ code = """from code_to_optimize.bubble_sort_method import BubbleSorter
504+
505+
506+ def test_sort():
507+ input = [5, 4, 3, 2, 1, 0]
508+ output = BubbleSorter.sorter_classmethod(input)
509+ assert output == [0, 1, 2, 3, 4, 5]
510+
511+ input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
512+ output = BubbleSorter.sorter_classmethod(input)
513+ assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]"""
514+
515+ expected = (
516+ """import gc
517+ import inspect
518+ import os
519+ import sqlite3
520+ import time
521+
522+ import dill as pickle
523+
524+ from code_to_optimize.bubble_sort_method import BubbleSorter
525+
526+
527+ """
528+ + codeflash_wrap_string
529+ + """
530+ def test_sort():
531+ codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
532+ codeflash_iteration = os.environ['CODEFLASH_TEST_ITERATION']
533+ codeflash_con = sqlite3.connect(f'{tmp_dir_path}_{{codeflash_iteration}}.sqlite')
534+ codeflash_cur = codeflash_con.cursor()
535+ codeflash_cur.execute('CREATE TABLE IF NOT EXISTS test_results (test_module_path TEXT, test_class_name TEXT, test_function_name TEXT, function_getting_tested TEXT, loop_index INTEGER, iteration_id TEXT, runtime INTEGER, return_value BLOB, verification_type TEXT)')
536+ input = [5, 4, 3, 2, 1, 0]
537+ _call__bound__arguments = inspect.signature(BubbleSorter.sorter_classmethod).bind(input)
538+ _call__bound__arguments.apply_defaults()
539+ output = codeflash_wrap(BubbleSorter.sorter_classmethod, '{module_path}', None, 'test_sort', 'BubbleSorter.sorter_classmethod', '1', codeflash_loop_index, codeflash_cur, codeflash_con, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
540+ assert output == [0, 1, 2, 3, 4, 5]
541+ input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
542+ _call__bound__arguments = inspect.signature(BubbleSorter.sorter_classmethod).bind(input)
543+ _call__bound__arguments.apply_defaults()
544+ output = codeflash_wrap(BubbleSorter.sorter_classmethod, '{module_path}', None, 'test_sort', 'BubbleSorter.sorter_classmethod', '4', codeflash_loop_index, codeflash_cur, codeflash_con, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
545+ assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
546+ codeflash_con.close()
547+ """
548+ )
549+ fto_path = (Path (__file__ ).parent .resolve () / "../code_to_optimize/bubble_sort_method.py" ).resolve ()
550+ original_code = fto_path .read_text ("utf-8" )
551+ fto = FunctionToOptimize (
552+ function_name = "sorter_classmethod" , parents = [FunctionParent (name = "BubbleSorter" , type = "ClassDef" )], file_path = Path (fto_path )
553+ )
554+ with tempfile .TemporaryDirectory () as tmpdirname :
555+ tmp_test_path = Path (tmpdirname ) / "test_classmethod_behavior_results_temp.py"
556+ tmp_test_path .write_text (code , encoding = "utf-8" )
557+
558+ success , new_test = inject_profiling_into_existing_test (
559+ tmp_test_path , [CodePosition (6 , 13 ), CodePosition (10 , 13 )], fto , tmp_test_path .parent , "pytest"
560+ )
561+ assert success
562+ assert new_test .replace ('"' , "'" ) == expected .format (
563+ module_path = tmp_test_path .stem , tmp_dir_path = get_run_tmp_file (Path ("test_return_values" )).as_posix ()
564+ ).replace ('"' , "'" )
565+ tests_root = (Path (__file__ ).parent .resolve () / "../code_to_optimize/tests/pytest/" ).resolve ()
566+ test_path = tests_root / "test_classmethod_behavior_results_temp.py"
567+ test_path_perf = tests_root / "test_classmethod_behavior_results_perf_temp.py"
568+ project_root_path = (Path (__file__ ).parent / ".." ).resolve ()
569+
570+ try :
571+ new_test = expected .format (
572+ module_path = "code_to_optimize.tests.pytest.test_classmethod_behavior_results_temp" ,
573+ tmp_dir_path = get_run_tmp_file (Path ("test_return_values" )).as_posix (),
574+ )
575+
576+ with test_path .open ("w" ) as f :
577+ f .write (new_test )
578+
579+ # Add codeflash capture
580+ instrument_codeflash_capture (fto , {}, tests_root )
581+
582+ opt = Optimizer (
583+ Namespace (
584+ project_root = project_root_path ,
585+ disable_telemetry = True ,
586+ tests_root = tests_root ,
587+ test_framework = "pytest" ,
588+ pytest_cmd = "pytest" ,
589+ experiment_id = None ,
590+ test_project_root = project_root_path ,
591+ )
592+ )
593+
594+ test_env = os .environ .copy ()
595+ test_env ["CODEFLASH_TEST_ITERATION" ] = "0"
596+ test_env ["CODEFLASH_LOOP_INDEX" ] = "1"
597+ test_type = TestType .EXISTING_UNIT_TEST
598+ func_optimizer = opt .create_function_optimizer (fto )
599+ func_optimizer .test_files = TestFiles (
600+ test_files = [
601+ TestFile (
602+ instrumented_behavior_file_path = test_path ,
603+ test_type = test_type ,
604+ original_file_path = test_path ,
605+ benchmarking_file_path = test_path_perf ,
606+ )
607+ ]
608+ )
609+ test_results , coverage_data = func_optimizer .run_and_parse_tests (
610+ testing_type = TestingMode .BEHAVIOR ,
611+ test_env = test_env ,
612+ test_files = func_optimizer .test_files ,
613+ optimization_iteration = 0 ,
614+ pytest_min_loops = 1 ,
615+ pytest_max_loops = 1 ,
616+ testing_time = 0.1 ,
617+ )
618+ assert len (test_results ) == 2
619+ assert test_results [0 ].id .function_getting_tested == "BubbleSorter.sorter_classmethod"
620+ assert test_results [0 ].id .iteration_id == "1_0"
621+ assert test_results [0 ].id .test_class_name is None
622+ assert test_results [0 ].id .test_function_name == "test_sort"
623+ assert (
624+ test_results [0 ].id .test_module_path
625+ == "code_to_optimize.tests.pytest.test_classmethod_behavior_results_temp"
626+ )
627+ assert test_results [0 ].runtime > 0
628+ assert test_results [0 ].did_pass
629+ assert test_results [0 ].return_value == ([0 , 1 , 2 , 3 , 4 , 5 ],)
630+ out_str = """codeflash stdout : BubbleSorter.sorter_classmethod() called
631+ """
632+ assert test_results [0 ].stdout == out_str
633+ assert compare_test_results (test_results , test_results )
634+
635+ assert test_results [1 ].id .function_getting_tested == "BubbleSorter.sorter_classmethod"
636+ assert test_results [1 ].id .iteration_id == "4_0"
637+ assert test_results [1 ].id .test_class_name is None
638+ assert test_results [1 ].id .test_function_name == "test_sort"
639+ assert (
640+ test_results [1 ].id .test_module_path
641+ == "code_to_optimize.tests.pytest.test_classmethod_behavior_results_temp"
642+ )
643+ assert test_results [1 ].runtime > 0
644+ assert test_results [1 ].did_pass
645+ assert test_results [1 ].stdout == """codeflash stdout : BubbleSorter.sorter_classmethod() called
646+ """
647+
648+ results2 , _ = func_optimizer .run_and_parse_tests (
649+ testing_type = TestingMode .BEHAVIOR ,
650+ test_env = test_env ,
651+ test_files = func_optimizer .test_files ,
652+ optimization_iteration = 0 ,
653+ pytest_min_loops = 1 ,
654+ pytest_max_loops = 1 ,
655+ testing_time = 0.1 ,
656+ )
657+
658+ assert compare_test_results (test_results , results2 )
659+
660+ finally :
661+ fto_path .write_text (original_code , "utf-8" )
662+ test_path .unlink (missing_ok = True )
663+ test_path_perf .unlink (missing_ok = True )
664+
665+
666+ def test_staticmethod_full_instrumentation () -> None :
667+ code = """from code_to_optimize.bubble_sort_method import BubbleSorter
668+
669+
670+ def test_sort():
671+ input = [5, 4, 3, 2, 1, 0]
672+ output = BubbleSorter.sorter_staticmethod(input)
673+ assert output == [0, 1, 2, 3, 4, 5]
674+
675+ input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
676+ output = BubbleSorter.sorter_staticmethod(input)
677+ assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]"""
678+
679+ expected = (
680+ """import gc
681+ import inspect
682+ import os
683+ import sqlite3
684+ import time
685+
686+ import dill as pickle
687+
688+ from code_to_optimize.bubble_sort_method import BubbleSorter
689+
690+
691+ """
692+ + codeflash_wrap_string
693+ + """
694+ def test_sort():
695+ codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
696+ codeflash_iteration = os.environ['CODEFLASH_TEST_ITERATION']
697+ codeflash_con = sqlite3.connect(f'{tmp_dir_path}_{{codeflash_iteration}}.sqlite')
698+ codeflash_cur = codeflash_con.cursor()
699+ codeflash_cur.execute('CREATE TABLE IF NOT EXISTS test_results (test_module_path TEXT, test_class_name TEXT, test_function_name TEXT, function_getting_tested TEXT, loop_index INTEGER, iteration_id TEXT, runtime INTEGER, return_value BLOB, verification_type TEXT)')
700+ input = [5, 4, 3, 2, 1, 0]
701+ _call__bound__arguments = inspect.signature(BubbleSorter.sorter_staticmethod).bind(input)
702+ _call__bound__arguments.apply_defaults()
703+ output = codeflash_wrap(BubbleSorter.sorter_staticmethod, '{module_path}', None, 'test_sort', 'BubbleSorter.sorter_staticmethod', '1', codeflash_loop_index, codeflash_cur, codeflash_con, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
704+ assert output == [0, 1, 2, 3, 4, 5]
705+ input = [5.0, 4.0, 3.0, 2.0, 1.0, 0.0]
706+ _call__bound__arguments = inspect.signature(BubbleSorter.sorter_staticmethod).bind(input)
707+ _call__bound__arguments.apply_defaults()
708+ output = codeflash_wrap(BubbleSorter.sorter_staticmethod, '{module_path}', None, 'test_sort', 'BubbleSorter.sorter_staticmethod', '4', codeflash_loop_index, codeflash_cur, codeflash_con, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
709+ assert output == [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
710+ codeflash_con.close()
711+ """
712+ )
713+ fto_path = (Path (__file__ ).parent .resolve () / "../code_to_optimize/bubble_sort_method.py" ).resolve ()
714+ original_code = fto_path .read_text ("utf-8" )
715+ fto = FunctionToOptimize (
716+ function_name = "sorter_staticmethod" , parents = [FunctionParent (name = "BubbleSorter" , type = "ClassDef" )], file_path = Path (fto_path )
717+ )
718+ with tempfile .TemporaryDirectory () as tmpdirname :
719+ tmp_test_path = Path (tmpdirname ) / "test_staticmethod_behavior_results_temp.py"
720+ tmp_test_path .write_text (code , encoding = "utf-8" )
721+
722+ success , new_test = inject_profiling_into_existing_test (
723+ tmp_test_path , [CodePosition (6 , 13 ), CodePosition (10 , 13 )], fto , tmp_test_path .parent , "pytest"
724+ )
725+ assert success
726+ assert new_test .replace ('"' , "'" ) == expected .format (
727+ module_path = tmp_test_path .stem , tmp_dir_path = get_run_tmp_file (Path ("test_return_values" )).as_posix ()
728+ ).replace ('"' , "'" )
729+ tests_root = (Path (__file__ ).parent .resolve () / "../code_to_optimize/tests/pytest/" ).resolve ()
730+ test_path = tests_root / "test_staticmethod_behavior_results_temp.py"
731+ test_path_perf = tests_root / "test_staticmethod_behavior_results_perf_temp.py"
732+ project_root_path = (Path (__file__ ).parent / ".." ).resolve ()
733+
734+ try :
735+ new_test = expected .format (
736+ module_path = "code_to_optimize.tests.pytest.test_staticmethod_behavior_results_temp" ,
737+ tmp_dir_path = get_run_tmp_file (Path ("test_return_values" )).as_posix (),
738+ )
739+
740+ with test_path .open ("w" ) as f :
741+ f .write (new_test )
742+
743+ # Add codeflash capture
744+ instrument_codeflash_capture (fto , {}, tests_root )
745+
746+ opt = Optimizer (
747+ Namespace (
748+ project_root = project_root_path ,
749+ disable_telemetry = True ,
750+ tests_root = tests_root ,
751+ test_framework = "pytest" ,
752+ pytest_cmd = "pytest" ,
753+ experiment_id = None ,
754+ test_project_root = project_root_path ,
755+ )
756+ )
757+
758+ test_env = os .environ .copy ()
759+ test_env ["CODEFLASH_TEST_ITERATION" ] = "0"
760+ test_env ["CODEFLASH_LOOP_INDEX" ] = "1"
761+ test_type = TestType .EXISTING_UNIT_TEST
762+ func_optimizer = opt .create_function_optimizer (fto )
763+ func_optimizer .test_files = TestFiles (
764+ test_files = [
765+ TestFile (
766+ instrumented_behavior_file_path = test_path ,
767+ test_type = test_type ,
768+ original_file_path = test_path ,
769+ benchmarking_file_path = test_path_perf ,
770+ )
771+ ]
772+ )
773+ test_results , coverage_data = func_optimizer .run_and_parse_tests (
774+ testing_type = TestingMode .BEHAVIOR ,
775+ test_env = test_env ,
776+ test_files = func_optimizer .test_files ,
777+ optimization_iteration = 0 ,
778+ pytest_min_loops = 1 ,
779+ pytest_max_loops = 1 ,
780+ testing_time = 0.1 ,
781+ )
782+ assert len (test_results ) == 2
783+ assert test_results [0 ].id .function_getting_tested == "BubbleSorter.sorter_staticmethod"
784+ assert test_results [0 ].id .iteration_id == "1_0"
785+ assert test_results [0 ].id .test_class_name is None
786+ assert test_results [0 ].id .test_function_name == "test_sort"
787+ assert (
788+ test_results [0 ].id .test_module_path
789+ == "code_to_optimize.tests.pytest.test_staticmethod_behavior_results_temp"
790+ )
791+ assert test_results [0 ].runtime > 0
792+ assert test_results [0 ].did_pass
793+ assert test_results [0 ].return_value == ([0 , 1 , 2 , 3 , 4 , 5 ],)
794+ out_str = """codeflash stdout : BubbleSorter.sorter_staticmethod() called
795+ """
796+ assert test_results [0 ].stdout == out_str
797+ assert compare_test_results (test_results , test_results )
798+
799+ assert test_results [1 ].id .function_getting_tested == "BubbleSorter.sorter_staticmethod"
800+ assert test_results [1 ].id .iteration_id == "4_0"
801+ assert test_results [1 ].id .test_class_name is None
802+ assert test_results [1 ].id .test_function_name == "test_sort"
803+ assert (
804+ test_results [1 ].id .test_module_path
805+ == "code_to_optimize.tests.pytest.test_staticmethod_behavior_results_temp"
806+ )
807+ assert test_results [1 ].runtime > 0
808+ assert test_results [1 ].did_pass
809+ assert test_results [1 ].stdout == """codeflash stdout : BubbleSorter.sorter_staticmethod() called
810+ """
811+
812+ results2 , _ = func_optimizer .run_and_parse_tests (
813+ testing_type = TestingMode .BEHAVIOR ,
814+ test_env = test_env ,
815+ test_files = func_optimizer .test_files ,
816+ optimization_iteration = 0 ,
817+ pytest_min_loops = 1 ,
818+ pytest_max_loops = 1 ,
819+ testing_time = 0.1 ,
820+ )
821+
822+ assert compare_test_results (test_results , results2 )
823+
496824 finally :
497825 fto_path .write_text (original_code , "utf-8" )
498826 test_path .unlink (missing_ok = True )
0 commit comments