Skip to content

Commit b0da369

Browse files
committed
respond to code review
1 parent e784545 commit b0da369

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def insert_test(
107107
)
108108
self.connection.commit()
109109

110-
def get_tests_for_file(self, file_path: str, file_hash: str) -> list[FunctionCalledInTest] | None:
110+
def get_function_to_test_map_for_file(
111+
self, file_path: str, file_hash: str
112+
) -> dict[str, set[FunctionCalledInTest]] | None:
111113
cache_key = (file_path, file_hash)
112114
if cache_key in self.memory_cache:
113115
return self.memory_cache[cache_key]
@@ -117,15 +119,20 @@ def get_tests_for_file(self, file_path: str, file_hash: str) -> list[FunctionCal
117119
if not rows:
118120
return None
119121

120-
result = [
121-
FunctionCalledInTest(
122+
function_to_test_map = defaultdict(set)
123+
124+
for row in rows:
125+
qualified_name_with_modules_from_root = row[2]
126+
function_called_in_test = FunctionCalledInTest(
122127
tests_in_file=TestsInFile(
123128
test_file=Path(row[0]), test_class=row[4], test_function=row[5], test_type=TestType(int(row[6]))
124129
),
125130
position=CodePosition(line_no=row[7], col_no=row[8]),
131+
qualified_name_with_modules_from_root=qualified_name_with_modules_from_root,
126132
)
127-
for row in rows
128-
]
133+
function_to_test_map[qualified_name_with_modules_from_root].add(function_called_in_test)
134+
135+
result = dict(function_to_test_map)
129136
self.memory_cache[cache_key] = result
130137
return result
131138

@@ -552,28 +559,16 @@ def process_test_files(
552559
for test_file, functions in file_to_test_map.items():
553560
file_hash = TestsCache.compute_file_hash(test_file)
554561

555-
cached_tests = tests_cache.get_tests_for_file(str(test_file), file_hash)
556-
557-
if cached_tests:
558-
# Rebuild function_to_test_map from cached data
559-
tests_cache.cur.execute(
560-
"SELECT * FROM discovered_tests WHERE file_path = ? AND file_hash = ?", (str(test_file), file_hash)
561-
)
562-
for row in tests_cache.cur.fetchall():
563-
qualified_name_with_modules_from_root = row[2]
564-
test_type = TestType(int(row[6]))
565-
566-
function_called_in_test = FunctionCalledInTest(
567-
tests_in_file=TestsInFile(
568-
test_file=test_file, test_class=row[4], test_function=row[5], test_type=test_type
569-
),
570-
position=CodePosition(line_no=row[7], col_no=row[8]),
571-
)
572-
573-
function_to_test_map[qualified_name_with_modules_from_root].add(function_called_in_test)
574-
if test_type == TestType.REPLAY_TEST:
575-
num_discovered_replay_tests += 1
576-
num_discovered_tests += 1
562+
cached_function_to_test_map = tests_cache.get_function_to_test_map_for_file(str(test_file), file_hash)
563+
564+
if cached_function_to_test_map:
565+
for qualified_name, test_set in cached_function_to_test_map.items():
566+
function_to_test_map[qualified_name].update(test_set)
567+
568+
for function_called_in_test in test_set:
569+
if function_called_in_test.tests_in_file.test_type == TestType.REPLAY_TEST:
570+
num_discovered_replay_tests += 1
571+
num_discovered_tests += 1
577572

578573
progress.advance(task_id)
579574
continue
@@ -707,6 +702,7 @@ def process_test_files(
707702
test_type=test_func.test_type,
708703
),
709704
position=CodePosition(line_no=name.line, col_no=name.column),
705+
qualified_name_with_modules_from_root=qualified_name_with_modules_from_root,
710706
)
711707
)
712708
tests_cache.insert_test(

0 commit comments

Comments
 (0)