Skip to content

Commit b290f30

Browse files
author
Zhe Yu
committed
tests for Chunk.export_dict/chunks pipe mode
1 parent 9cfd99a commit b290f30

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

src/vectorcode/chunking.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def export_dict(self):
5050
"end": {"row": self.end.row, "column": self.end.column},
5151
}
5252
)
53+
if self.path:
54+
d["path"] = self.path
55+
if self.id:
56+
d["chunk_id"] = self.id
5357
return d
5458

5559

@@ -140,7 +144,7 @@ def chunk(
140144
) -> Generator[Chunk, None, None]:
141145
logger.info("Started chunking %s using FileChunker.", data.name)
142146
lines = data.readlines()
143-
if len(lines) == 0:
147+
if len(lines) == 0: # pragma: nocover
144148
return
145149
if (
146150
self.config.chunk_size < 0

tests/subcommands/test_chunks.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from unittest.mock import MagicMock, patch
23

34
import pytest
@@ -37,3 +38,62 @@ async def test_chunks():
3738
assert mock_chunker.config == mock_config
3839
mock_chunker.chunk.assert_called()
3940
assert mock_chunker.chunk.call_count == 2
41+
42+
43+
@pytest.mark.asyncio
44+
async def test_chunks_pipe(capsys):
45+
# Mock the Config object
46+
mock_config = MagicMock(spec=Config)
47+
mock_config.chunk_size = 2000
48+
mock_config.overlap_ratio = 0.2
49+
mock_config.files = ["file1.py"]
50+
mock_config.pipe = True
51+
52+
# Mock the TreeSitterChunker
53+
mock_chunker = TreeSitterChunker(mock_config)
54+
mock_chunker.chunk = MagicMock()
55+
_chunks = [
56+
Chunk("chunk1_file1", Point(0, 1), Point(0, 12), path="file1.py", id="c1"),
57+
Chunk("chunk2_file1", Point(1, 1), Point(1, 12), path="file1.py", id="c2"),
58+
]
59+
mock_chunker.chunk.side_effect = [
60+
_chunks,
61+
]
62+
with patch(
63+
"vectorcode.subcommands.chunks.TreeSitterChunker", return_value=mock_chunker
64+
):
65+
# Call the chunks function
66+
result = await chunks(mock_config)
67+
68+
# Assertions
69+
assert result == 0
70+
assert mock_chunker.config == mock_config
71+
mock_chunker.chunk.assert_called()
72+
assert mock_chunker.chunk.call_count == 1
73+
74+
captured = capsys.readouterr()
75+
output = json.loads(captured.out.strip())
76+
assert output == [
77+
[
78+
{
79+
"text": "chunk1_file1",
80+
"start": {
81+
"row": 0,
82+
"column": 1,
83+
},
84+
"end": {"row": 0, "column": 12},
85+
"path": "file1.py",
86+
"chunk_id": "c1",
87+
},
88+
{
89+
"text": "chunk2_file1",
90+
"start": {
91+
"row": 1,
92+
"column": 1,
93+
},
94+
"end": {"row": 1, "column": 12},
95+
"path": "file1.py",
96+
"chunk_id": "c2",
97+
},
98+
]
99+
]

0 commit comments

Comments
 (0)