|
1 | | -from contextlib import asynccontextmanager |
2 | 1 | from unittest.mock import AsyncMock, patch |
3 | 2 |
|
4 | 3 | import pytest |
5 | 4 |
|
6 | 5 | from vectorcode.cli_utils import Config |
| 6 | +from vectorcode.database.errors import CollectionNotFoundError |
7 | 7 | from vectorcode.subcommands.drop import drop |
8 | 8 |
|
9 | 9 |
|
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 | | - |
31 | 10 | @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 |
40 | 15 | ): |
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() |
54 | 18 |
|
55 | 19 |
|
56 | 20 | @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