Skip to content

Commit 47c28ba

Browse files
committed
test(cli): test for build_query_results
1 parent e882140 commit 47c28ba

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

tests/subcommands/query/test_query.py

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
from unittest.mock import AsyncMock, MagicMock, patch
1+
from unittest.mock import AsyncMock, MagicMock, mock_open, patch
22

33
import pytest
4+
from chromadb import GetResult
45
from chromadb.api.models.AsyncCollection import AsyncCollection
56
from chromadb.api.types import IncludeEnum
67
from chromadb.errors import InvalidCollectionException, InvalidDimensionException
78

89
from vectorcode.cli_utils import CliAction, Config, QueryInclude
9-
from vectorcode.subcommands.query import get_query_result_files, query
10+
from vectorcode.subcommands.query import (
11+
build_query_results,
12+
get_query_result_files,
13+
query,
14+
)
1015

1116

1217
@pytest.fixture
@@ -17,8 +22,16 @@ def mock_collection():
1722
"ids": [["id1", "id2", "id3"], ["id4", "id5", "id6"]],
1823
"distances": [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]],
1924
"metadatas": [
20-
[{"path": "file1.py"}, {"path": "file2.py"}, {"path": "file3.py"}],
21-
[{"path": "file2.py"}, {"path": "file4.py"}, {"path": "file3.py"}],
25+
[
26+
{"path": "file1.py", "start": 1, "end": 1},
27+
{"path": "file2.py", "start": 1, "end": 1},
28+
{"path": "file3.py", "start": 1, "end": 1},
29+
],
30+
[
31+
{"path": "file2.py", "start": 1, "end": 1},
32+
{"path": "file4.py", "start": 1, "end": 1},
33+
{"path": "file3.py", "start": 1, "end": 1},
34+
],
2235
],
2336
"documents": [
2437
["content1", "content2", "content3"],
@@ -83,6 +96,83 @@ async def test_get_query_result_files(mock_collection, mock_config):
8396
assert result == ["file1.py", "file2.py", "file3.py"]
8497

8598

99+
@pytest.mark.asyncio
100+
async def test_get_query_result_files_include_chunk(mock_collection, mock_config):
101+
"""Test get_query_result_files when QueryInclude.chunk is included."""
102+
mock_config.include = [QueryInclude.chunk] # Include chunk
103+
104+
with patch("vectorcode.subcommands.query.reranker.NaiveReranker") as MockReranker:
105+
mock_reranker_instance = MagicMock()
106+
mock_reranker_instance.rerank.return_value = ["chunk1"]
107+
MockReranker.return_value = mock_reranker_instance
108+
109+
await get_query_result_files(mock_collection, mock_config)
110+
111+
# Check query call includes where clause for chunks
112+
mock_collection.query.assert_called_once()
113+
_, kwargs = mock_collection.query.call_args
114+
# Line 43: Check the 'if' condition branch
115+
assert kwargs["where"] == {"start": {"$gte": 0}}
116+
assert kwargs["n_results"] == 3 # n_result should be used directly
117+
118+
119+
@pytest.mark.asyncio
120+
async def test_build_query_results_chunk_mode_success(mock_collection, mock_config):
121+
"""Test build_query_results in chunk mode successfully retrieves chunk details."""
122+
mock_config.include = [QueryInclude.chunk, QueryInclude.path]
123+
mock_config.project_root = "/test/project"
124+
mock_config.use_absolute_path = False
125+
identifier = "chunk_id_1"
126+
file_path = "/test/project/subdir/file1.py"
127+
relative_path = "subdir/file1.py"
128+
start_line = 5
129+
end_line = 10
130+
131+
full_file_content_lines = [f"line {i}\n" for i in range(15)]
132+
full_file_content = "".join(full_file_content_lines)
133+
134+
expected_chunk_content = "".join(full_file_content_lines[start_line : end_line + 1])
135+
136+
mock_get_result = GetResult(
137+
ids=[identifier],
138+
embeddings=None,
139+
documents=["original chunk doc in db"],
140+
metadatas=[{"path": file_path, "start": start_line, "end": end_line}],
141+
)
142+
143+
with (
144+
patch(
145+
"vectorcode.subcommands.query.get_query_result_files",
146+
return_value=[identifier],
147+
),
148+
patch("os.path.isfile", return_value=False),
149+
patch("builtins.open", mock_open(read_data=full_file_content)) as mocked_open,
150+
patch("os.path.relpath", return_value=relative_path) as mock_relpath,
151+
):
152+
mock_collection.get = AsyncMock(return_value=mock_get_result)
153+
154+
results = await build_query_results(mock_collection, mock_config)
155+
156+
mock_collection.get.assert_called_once_with(
157+
identifier, include=[IncludeEnum.metadatas, IncludeEnum.documents]
158+
)
159+
160+
mocked_open.assert_called_once_with(file_path)
161+
162+
mock_relpath.assert_called_once_with(file_path, str(mock_config.project_root))
163+
164+
assert len(results) == 1
165+
166+
expected_full_result = {
167+
"path": relative_path,
168+
"chunk": expected_chunk_content,
169+
"start_line": start_line,
170+
"end_line": end_line,
171+
}
172+
173+
assert results[0] == expected_full_result
174+
175+
86176
@pytest.mark.asyncio
87177
async def test_get_query_result_files_with_query_exclude(mock_collection, mock_config):
88178
# Setup query_exclude

0 commit comments

Comments
 (0)