Skip to content

Commit 938abf8

Browse files
author
Zhe Yu
committed
tests(cli): Refactor drop subcommand and add exception handling
1 parent 474e026 commit 938abf8

2 files changed

Lines changed: 17 additions & 53 deletions

File tree

src/vectorcode/subcommands/drop.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99

1010
async def drop(config: Config) -> int:
11-
database = get_database_connector(config)
1211
try:
12+
database = get_database_connector(config)
1313
await database.drop()
1414
if not config.pipe:
1515
print(f"Collection for {config.project_root} has been deleted.")
1616
return 0
1717
except CollectionNotFoundError:
1818
logger.warning(f"Collection for {config.project_root} doesn't exist.")
1919
return 1
20+
except Exception: # pragma: nocover
21+
raise

tests/subcommands/test_drop.py

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,27 @@
1-
from contextlib import asynccontextmanager
21
from unittest.mock import AsyncMock, patch
32

43
import pytest
54

65
from vectorcode.cli_utils import Config
6+
from vectorcode.database.errors import CollectionNotFoundError
77
from vectorcode.subcommands.drop import drop
88

99

10-
@pytest.fixture
11-
def mock_config():
12-
config = Config(
13-
project_root="/path/to/project",
14-
) # Removed positional args
15-
return config
16-
17-
18-
@pytest.fixture
19-
def mock_client():
20-
return AsyncMock()
21-
22-
23-
@pytest.fixture
24-
def mock_collection():
25-
collection = AsyncMock()
26-
collection.name = "test_collection"
27-
collection.metadata = {"path": "/path/to/project"}
28-
return collection
29-
30-
3110
@pytest.mark.asyncio
32-
async def test_drop_success(mock_config, mock_client, mock_collection):
33-
mock_client.get_collection.return_value = mock_collection
34-
mock_client.delete_collection = AsyncMock()
35-
with (
36-
patch("vectorcode.subcommands.drop.ClientManager") as MockClientManager,
37-
patch(
38-
"vectorcode.subcommands.drop.get_collection", return_value=mock_collection
39-
),
11+
async def test_drop_success():
12+
mock_db = AsyncMock()
13+
with patch(
14+
"vectorcode.subcommands.drop.get_database_connector", return_value=mock_db
4015
):
41-
mock_client = AsyncMock()
42-
43-
@asynccontextmanager
44-
async def _get_client(self, config=None, need_lock=True):
45-
yield mock_client
46-
47-
mock_client_manager = MockClientManager.return_value
48-
mock_client_manager._create_client = AsyncMock(return_value=mock_client)
49-
mock_client_manager.get_client = _get_client
50-
51-
result = await drop(mock_config)
52-
assert result == 0
53-
mock_client.delete_collection.assert_called_once_with(mock_collection.name)
16+
await drop(config=Config(project_root="DummyDir"))
17+
mock_db.drop.assert_called_once()
5418

5519

5620
@pytest.mark.asyncio
57-
async def test_drop_collection_not_found(mock_config, mock_client):
58-
mock_client.get_collection.side_effect = ValueError("Collection not found")
59-
with patch("vectorcode.subcommands.drop.ClientManager"):
60-
with patch(
61-
"vectorcode.subcommands.drop.get_collection",
62-
side_effect=ValueError("Collection not found"),
63-
):
64-
result = await drop(mock_config)
65-
assert result == 1
21+
async def test_drop_collection_not_found():
22+
mock_db = AsyncMock()
23+
mock_db.drop = AsyncMock(side_effect=CollectionNotFoundError)
24+
with patch(
25+
"vectorcode.subcommands.drop.get_database_connector", return_value=mock_db
26+
):
27+
assert await drop(config=Config(project_root="DummyDir")) != 0

0 commit comments

Comments
 (0)