Skip to content

Commit 180c479

Browse files
committed
add project_root as a index key
and use_cache as a test_config
1 parent caac361 commit 180c479

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ class TestFunction:
6767

6868

6969
class TestsCache:
70-
def __init__(self) -> None:
70+
def __init__(self, project_root_path: str | Path) -> None:
71+
self.project_root_path = Path(project_root_path).resolve().as_posix()
7172
self.connection = sqlite3.connect(codeflash_cache_db)
7273
self.cur = self.connection.cursor()
7374
self.cur.execute(
7475
"""
7576
CREATE TABLE IF NOT EXISTS discovered_tests(
77+
project_root_path TEXT,
7678
file_path TEXT,
7779
file_hash TEXT,
7880
qualified_name_with_modules_from_root TEXT,
@@ -87,8 +89,8 @@ def __init__(self) -> None:
8789
)
8890
self.cur.execute(
8991
"""
90-
CREATE INDEX IF NOT EXISTS idx_discovered_tests_file_path_hash
91-
ON discovered_tests (file_path, file_hash)
92+
CREATE INDEX IF NOT EXISTS idx_discovered_tests_project_file_path_hash
93+
ON discovered_tests (project_root_path, file_path, file_hash)
9294
"""
9395
)
9496

@@ -108,8 +110,9 @@ def insert_test(
108110
) -> None:
109111
test_type_value = test_type.value if hasattr(test_type, "value") else test_type
110112
self.cur.execute(
111-
"INSERT INTO discovered_tests VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
113+
"INSERT INTO discovered_tests VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
112114
(
115+
self.project_root_path,
113116
file_path,
114117
file_hash,
115118
qualified_name_with_modules_from_root,
@@ -126,24 +129,27 @@ def insert_test(
126129
def get_function_to_test_map_for_file(
127130
self, file_path: str, file_hash: str
128131
) -> dict[str, set[FunctionCalledInTest]] | None:
129-
cache_key = (file_path, file_hash)
132+
cache_key = (self.project_root_path, file_path, file_hash)
130133
if cache_key in self.memory_cache:
131134
return self.memory_cache[cache_key]
132135

133-
self.cur.execute("SELECT * FROM discovered_tests WHERE file_path = ? AND file_hash = ?", (file_path, file_hash))
136+
self.cur.execute(
137+
"SELECT * FROM discovered_tests WHERE project_root_path = ? AND file_path = ? AND file_hash = ?",
138+
(self.project_root_path, file_path, file_hash)
139+
)
134140
rows = self.cur.fetchall()
135141
if not rows:
136142
return None
137143

138144
function_to_test_map = defaultdict(set)
139145

140146
for row in rows:
141-
qualified_name_with_modules_from_root = row[2]
147+
qualified_name_with_modules_from_root = row[3]
142148
function_called_in_test = FunctionCalledInTest(
143149
tests_in_file=TestsInFile(
144-
test_file=Path(row[0]), test_class=row[4], test_function=row[5], test_type=TestType(int(row[6]))
150+
test_file=Path(row[1]), test_class=row[5], test_function=row[6], test_type=TestType(int(row[7]))
145151
),
146-
position=CodePosition(line_no=row[7], col_no=row[8]),
152+
position=CodePosition(line_no=row[8], col_no=row[9]),
147153
)
148154
function_to_test_map[qualified_name_with_modules_from_root].add(function_called_in_test)
149155

@@ -566,7 +572,7 @@ def process_test_files(
566572
num_discovered_replay_tests = 0
567573
jedi_project = jedi.Project(path=project_root_path)
568574

569-
tests_cache = TestsCache()
575+
tests_cache = TestsCache(project_root_path)
570576

571577
with test_files_progress_bar(total=len(file_to_test_map), description="Processing test files") as (
572578
progress,
@@ -577,7 +583,7 @@ def process_test_files(
577583

578584
cached_function_to_test_map = tests_cache.get_function_to_test_map_for_file(str(test_file), file_hash)
579585

580-
if cached_function_to_test_map:
586+
if cfg.use_cache and cached_function_to_test_map:
581587
for qualified_name, test_set in cached_function_to_test_map.items():
582588
function_to_test_map[qualified_name].update(test_set)
583589

codeflash/verification/verification_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ class TestConfig:
7676
concolic_test_root_dir: Optional[Path] = None
7777
pytest_cmd: str = "pytest"
7878
benchmark_tests_root: Optional[Path] = None
79+
use_cache: bool = True

0 commit comments

Comments
 (0)