Skip to content

Commit 9cfd99a

Browse files
author
Zhe Yu
committed
improve coverage
1 parent edab435 commit 9cfd99a

3 files changed

Lines changed: 50 additions & 48 deletions

File tree

src/vectorcode/chunking.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ def export_dict(self):
5050
"end": {"row": self.end.row, "column": self.end.column},
5151
}
5252
)
53-
if self.path is not None:
54-
d["path"] = self.path
55-
if self.id:
56-
d["chunk_id"] = self.id
5753
return d
5854

5955

src/vectorcode/subcommands/query/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def make_output_path(path: str, absolute: bool) -> str:
140140
else:
141141
rel_path = os.path.relpath(path, configs.project_root)
142142
if isinstance(rel_path, bytes): # pragma: nocover
143-
# for some reasons some python versions report that `os.path.relpath` returns a string.
143+
# for some reasons, some python versions report that `os.path.relpath` returns a string.
144144
rel_path = rel_path.decode()
145145
return rel_path
146146

tests/subcommands/query/test_query.py

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -133,50 +133,56 @@ async def test_get_query_result_files_include_chunk(mock_collection, mock_config
133133
@pytest.mark.asyncio
134134
async def test_build_query_results_chunk_mode_success(mock_collection, mock_config):
135135
"""Test build_query_results in chunk mode successfully retrieves chunk details."""
136-
mock_config.include = [QueryInclude.chunk, QueryInclude.path]
137-
mock_config.project_root = "/test/project"
138-
mock_config.use_absolute_path = False
139-
mock_config.query = ["dummy_query"]
140-
identifier = "chunk_id"
141-
file_path = "/test/project/subdir/file1.py"
142-
relative_path = "subdir/file1.py"
143-
start_line = 5
144-
end_line = 10
145-
146-
full_file_content_lines = [f"line {i}\n" for i in range(15)]
147-
148-
expected_chunk_content = "".join(full_file_content_lines[start_line : end_line + 1])
149-
150-
mock_get_result = QueryResult(
151-
ids=[[identifier]],
152-
documents=[[expected_chunk_content]],
153-
metadatas=[[{"path": file_path, "start": start_line, "end": end_line}]],
154-
distances=[[0.2]],
155-
)
156-
mock_collection.query = AsyncMock(return_value=mock_get_result)
157-
with (
158-
patch(
159-
"vectorcode.subcommands.query.get_query_result_files",
160-
return_value=await get_query_result_files(mock_collection, mock_config),
161-
),
162-
patch("os.path.isfile", return_value=False),
163-
patch("os.path.relpath", return_value=relative_path) as mock_relpath,
164-
):
165-
results = await build_query_results(mock_collection, mock_config)
166-
167-
mock_relpath.assert_called_once_with(file_path, str(mock_config.project_root))
168-
169-
assert len(results) == 1
170-
171-
expected_full_result = {
172-
"path": relative_path,
173-
"chunk": expected_chunk_content,
174-
"start_line": start_line,
175-
"end_line": end_line,
176-
"chunk_id": identifier,
177-
}
136+
for request_abs_path in (True, False):
137+
mock_config.include = [QueryInclude.chunk, QueryInclude.path]
138+
mock_config.project_root = "/test/project"
139+
mock_config.use_absolute_path = request_abs_path
140+
mock_config.query = ["dummy_query"]
141+
identifier = "chunk_id"
142+
file_path = "/test/project/subdir/file1.py"
143+
relative_path = "subdir/file1.py"
144+
start_line = 5
145+
end_line = 10
146+
147+
full_file_content_lines = [f"line {i}\n" for i in range(15)]
148+
149+
expected_chunk_content = "".join(
150+
full_file_content_lines[start_line : end_line + 1]
151+
)
178152

179-
assert results[0] == expected_full_result
153+
mock_get_result = QueryResult(
154+
ids=[[identifier]],
155+
documents=[[expected_chunk_content]],
156+
metadatas=[[{"path": file_path, "start": start_line, "end": end_line}]],
157+
distances=[[0.2]],
158+
)
159+
mock_collection.query = AsyncMock(return_value=mock_get_result)
160+
with (
161+
patch(
162+
"vectorcode.subcommands.query.get_query_result_files",
163+
return_value=await get_query_result_files(mock_collection, mock_config),
164+
),
165+
patch("os.path.isfile", return_value=False),
166+
patch("os.path.relpath", return_value=relative_path) as mock_relpath,
167+
):
168+
results = await build_query_results(mock_collection, mock_config)
169+
170+
if not request_abs_path:
171+
mock_relpath.assert_called_once_with(
172+
file_path, str(mock_config.project_root)
173+
)
174+
175+
assert len(results) == 1
176+
177+
expected_full_result = {
178+
"path": file_path if request_abs_path else relative_path,
179+
"chunk": expected_chunk_content,
180+
"start_line": start_line,
181+
"end_line": end_line,
182+
"chunk_id": identifier,
183+
}
184+
185+
assert results[0] == expected_full_result
180186

181187

182188
@pytest.mark.asyncio

0 commit comments

Comments
 (0)