Skip to content

Commit d2289e5

Browse files
committed
tests work now, ready to merge
1 parent 2ad1029 commit d2289e5

File tree

3 files changed

+295
-290
lines changed

3 files changed

+295
-290
lines changed

codeflash/result/create_pr.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
git_root_dir,
1818
)
1919
from codeflash.code_utils.github_utils import github_pr_url
20+
from codeflash.code_utils.tabulate import tabulate
2021
from codeflash.code_utils.time_utils import format_time
2122
from codeflash.github.PrComment import FileDiffContent, PrComment
2223
from codeflash.result.critic import performance_gain
@@ -38,6 +39,8 @@ def existing_tests_source_for(
3839
if not test_files:
3940
return ""
4041
output = ""
42+
rows = []
43+
headers = ["Test File::Test Function", "Original ⏱️", "Optimized ⏱️", "Improvement"]
4144
tests_root = test_cfg.tests_root
4245
module_root = test_cfg.project_root_path
4346
rel_tests_root = tests_root.relative_to(module_root)
@@ -76,7 +79,6 @@ def existing_tests_source_for(
7679
original_tests_to_runtimes.keys()
7780
) # both will have the same keys as some default values are assigned in the previous loop
7881
for filename in sorted(all_rel_paths):
79-
output += f"- {filename}\n"
8082
all_qualified_names = original_tests_to_runtimes[
8183
filename
8284
].keys() # both will have the same keys as some default values are assigned in the previous loop
@@ -90,7 +92,6 @@ def existing_tests_source_for(
9092
print_original_runtime = "NaN"
9193
else:
9294
print_original_runtime = format_time(original_tests_to_runtimes[filename][qualified_name])
93-
arrow = "->"
9495
if (
9596
original_tests_to_runtimes[filename][qualified_name] != 0
9697
and optimized_tests_to_runtimes[filename][qualified_name] != 0
@@ -107,14 +108,32 @@ def existing_tests_source_for(
107108
* 100
108109
)
109110
if greater:
110-
output += f" - {qualified_name}: {print_original_runtime} {arrow} {print_optimized_runtime} $$\\color{{red}}({perf_gain:.2f}\\\\%)$$\n"
111+
rows.append(
112+
[
113+
f"`{filename}::{qualified_name}`",
114+
f"{print_original_runtime}",
115+
f"{print_optimized_runtime}",
116+
f"⚠️{perf_gain:.2f}%",
117+
]
118+
)
111119
else:
112-
output += f" - {qualified_name}: {print_original_runtime} {arrow} {print_optimized_runtime} $$\\color{{green}}({perf_gain:.2f}\\\\%)$$\n"
120+
rows.append(
121+
[
122+
f"`{filename}::{qualified_name}`",
123+
f"{print_original_runtime}",
124+
f"{print_optimized_runtime}",
125+
f"✅{perf_gain:.2f}%",
126+
]
127+
)
113128
else:
114129
# one of them is NaN
115-
output += f" - {qualified_name}: {print_original_runtime} {arrow} {print_optimized_runtime}\n"
116-
# output += f"$$\\colorbox{{pink}}\{{ - {qualified_name}: {print_original_runtime} {arrow} {print_optimized_runtime}}}$$\n"
117-
output += "\n"
130+
rows.append(
131+
[f"`{filename}::{qualified_name}`", f"{print_original_runtime}", f"{print_optimized_runtime}", "❌"]
132+
)
133+
output += tabulate(
134+
headers=headers, tabular_data=rows, tablefmt="pipe", colglobalalign=None, preserve_whitespace=True
135+
)
136+
output += "\n"
118137
return output
119138

120139

tests/test_add_runtime_comments.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,34 @@ def test_config():
1414
"""Create a mock TestConfig for testing."""
1515
config = Mock(spec=TestConfig)
1616
config.project_root_path = Path("/project")
17+
config.test_framework= "pytest"
18+
config.tests_project_rootdir = Path("/project/tests")
1719
config.tests_root = Path("/project/tests")
1820
return config
1921

2022

21-
@pytest.fixture
22-
def sample_invocation_id():
23-
"""Create a sample InvocationId for testing."""
24-
return InvocationId(
25-
test_module_path="tests.test_module",
26-
test_class_name="TestClass",
27-
test_function_name="test_function",
28-
)
29-
30-
31-
@pytest.fixture
32-
def sample_invocation_id_no_class():
33-
"""Create a sample InvocationId without class for testing."""
34-
return InvocationId(
35-
test_module_path="tests.test_module",
36-
test_class_name=None,
37-
test_function_name="test_function",
38-
)
23+
# @pytest.fixture
24+
# def sample_invocation_id():
25+
# """Create a sample InvocationId for testing."""
26+
# return InvocationId(
27+
# test_module_path="test_module_path",
28+
# test_class_name="test_class_name",
29+
# test_function_name="test_function_name",
30+
# function_getting_tested="function_getting_tested",
31+
# iteration_id="0",
32+
# )
33+
#
34+
#
35+
# @pytest.fixture
36+
# def sample_invocation_id_no_class():
37+
# """Create a sample InvocationId without class for testing."""
38+
# return InvocationId(
39+
# test_module_path="test_module_path",
40+
# test_class_name=None,
41+
# test_function_name="test_function_name",
42+
# function_getting_tested="function_getting_tested",
43+
# iteration_id="0",
44+
# )
3945

4046

4147
class TestAddRuntimeCommentsToGeneratedTests:
@@ -60,6 +66,8 @@ def test_add_runtime_comments_simple_function(self, test_config):
6066
test_module_path="tests.test_module",
6167
test_class_name=None,
6268
test_function_name="test_function",
69+
function_getting_tested="some_function",
70+
iteration_id="0",
6371
)
6472

6573
original_runtimes = {invocation_id: [1000000000, 1200000000]} # 1s, 1.2s in nanoseconds
@@ -70,7 +78,7 @@ def test_add_runtime_comments_simple_function(self, test_config):
7078
)
7179

7280
expected_source = '''def test_function():
73-
codeflash_output = some_function() # 1.00s -> 500.00ms (50.00%)
81+
codeflash_output = some_function() # 1.00s -> 500ms (100.00%)
7482
assert codeflash_output == expected
7583
'''
7684

@@ -99,6 +107,9 @@ def test_function(self):
99107
test_module_path="tests.test_module",
100108
test_class_name="TestClass",
101109
test_function_name="test_function",
110+
function_getting_tested="some_function",
111+
iteration_id="0",
112+
102113
)
103114

104115
original_runtimes = {invocation_id: [2000000000]} # 2s in nanoseconds
@@ -110,7 +121,7 @@ def test_function(self):
110121

111122
expected_source = '''class TestClass:
112123
def test_function(self):
113-
codeflash_output = some_function() # 2.00s -> 1.00s (50.00%)
124+
codeflash_output = some_function() # 2.00s -> 1.00s (100.00%)
114125
assert codeflash_output == expected
115126
'''
116127

@@ -141,6 +152,8 @@ def test_add_runtime_comments_multiple_assignments(self, test_config):
141152
test_module_path="tests.test_module",
142153
test_class_name=None,
143154
test_function_name="test_function",
155+
function_getting_tested="some_function",
156+
iteration_id="0",
144157
)
145158

146159
original_runtimes = {invocation_id: [1500000000]} # 1.5s in nanoseconds
@@ -152,9 +165,9 @@ def test_add_runtime_comments_multiple_assignments(self, test_config):
152165

153166
expected_source = '''def test_function():
154167
setup_data = prepare_test()
155-
codeflash_output = some_function() # 1.50s -> 750.00ms (50.00%)
168+
codeflash_output = some_function() # 1.50s -> 750ms (100.00%)
156169
assert codeflash_output == expected
157-
codeflash_output = another_function() # 1.50s -> 750.00ms (50.00%)
170+
codeflash_output = another_function() # 1.50s -> 750ms (100.00%)
158171
assert codeflash_output == expected2
159172
'''
160173

@@ -183,6 +196,8 @@ def test_add_runtime_comments_no_matching_runtimes(self, test_config):
183196
test_module_path="tests.other_module",
184197
test_class_name=None,
185198
test_function_name="other_function",
199+
function_getting_tested="some_other_function",
200+
iteration_id="0",
186201
)
187202

188203
original_runtimes = {invocation_id: [1000000000]}
@@ -217,6 +232,8 @@ def test_add_runtime_comments_no_codeflash_output(self, test_config):
217232
test_module_path="tests.test_module",
218233
test_class_name=None,
219234
test_function_name="test_function",
235+
function_getting_tested="some_function",
236+
iteration_id="0",
220237
)
221238

222239
original_runtimes = {invocation_id: [1000000000]}
@@ -264,12 +281,16 @@ def test_add_runtime_comments_multiple_tests(self, test_config):
264281
test_module_path="tests.test_module1",
265282
test_class_name=None,
266283
test_function_name="test_function1",
284+
function_getting_tested="some_function",
285+
iteration_id="0",
267286
)
268287

269288
invocation_id2 = InvocationId(
270289
test_module_path="tests.test_module2",
271290
test_class_name=None,
272291
test_function_name="test_function2",
292+
function_getting_tested="another_function",
293+
iteration_id = "0",
273294
)
274295

275296
original_runtimes = {
@@ -286,12 +307,12 @@ def test_add_runtime_comments_multiple_tests(self, test_config):
286307
)
287308

288309
expected_source1 = '''def test_function1():
289-
codeflash_output = some_function() # 1.00s -> 500.00ms (50.00%)
310+
codeflash_output = some_function() # 1.00s -> 500ms (100.00%)
290311
assert codeflash_output == expected
291312
'''
292313

293314
expected_source2 = '''def test_function2():
294-
codeflash_output = another_function() # 2.00s -> 800.00ms (60.00%)
315+
codeflash_output = another_function() # 2.00s -> 800ms (150.00%)
295316
assert codeflash_output == expected
296317
'''
297318

@@ -320,6 +341,8 @@ def test_add_runtime_comments_performance_regression(self, test_config):
320341
test_module_path="tests.test_module",
321342
test_class_name=None,
322343
test_function_name="test_function",
344+
function_getting_tested="some_function",
345+
iteration_id="0",
323346
)
324347

325348
original_runtimes = {invocation_id: [1000000000]} # 1s
@@ -330,7 +353,7 @@ def test_add_runtime_comments_performance_regression(self, test_config):
330353
)
331354

332355
expected_source = '''def test_function():
333-
codeflash_output = some_function() # 1.00s -> 1.50s (-50.00%)
356+
codeflash_output = some_function() # 1.00s -> 1.50s (-33.33%)
334357
assert codeflash_output == expected
335358
'''
336359

0 commit comments

Comments
 (0)