Skip to content

Commit 2ccb987

Browse files
author
Zhe Yu
committed
tests(cli): fix failed tests due to ClientManager refactor.
1 parent 9e0b64e commit 2ccb987

9 files changed

Lines changed: 243 additions & 230 deletions

File tree

tests/subcommands/test_clean.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ async def mock_get_collections(client):
7373

7474
@pytest.mark.asyncio
7575
async def test_clean():
76-
mock_client = AsyncMock(spec=AsyncClientAPI)
76+
AsyncMock(spec=AsyncClientAPI)
7777
mock_config = Config(pipe=False)
7878

79-
with patch("vectorcode.subcommands.clean.get_client", return_value=mock_client):
79+
with patch("vectorcode.subcommands.clean.ClientManager"):
8080
result = await clean(mock_config)
8181

8282
assert result == 0

tests/subcommands/test_drop.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from contextlib import asynccontextmanager
12
from unittest.mock import AsyncMock, patch
23

34
import pytest
@@ -31,19 +32,31 @@ def mock_collection():
3132
async def test_drop_success(mock_config, mock_client, mock_collection):
3233
mock_client.get_collection.return_value = mock_collection
3334
mock_client.delete_collection = AsyncMock()
34-
with patch("vectorcode.subcommands.drop.get_client", return_value=mock_client):
35-
with patch(
35+
with (
36+
patch("vectorcode.subcommands.drop.ClientManager") as MockClientManager,
37+
patch(
3638
"vectorcode.subcommands.drop.get_collection", return_value=mock_collection
37-
):
38-
result = await drop(mock_config)
39-
assert result == 0
40-
mock_client.delete_collection.assert_called_once_with(mock_collection.name)
39+
),
40+
):
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)
4154

4255

4356
@pytest.mark.asyncio
4457
async def test_drop_collection_not_found(mock_config, mock_client):
4558
mock_client.get_collection.side_effect = ValueError("Collection not found")
46-
with patch("vectorcode.subcommands.drop.get_client", return_value=mock_client):
59+
with patch("vectorcode.subcommands.drop.ClientManager"):
4760
with patch(
4861
"vectorcode.subcommands.drop.get_collection",
4962
side_effect=ValueError("Collection not found"),

tests/subcommands/test_ls.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import socket
3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock, MagicMock, patch
44

55
import pytest
66
import tabulate
@@ -77,7 +77,7 @@ async def mock_get_collections(client):
7777
yield mock_collection
7878

7979
with (
80-
patch("vectorcode.subcommands.ls.get_client", return_value=mock_client),
80+
patch("vectorcode.subcommands.ls.ClientManager") as MockClientManager,
8181
patch(
8282
"vectorcode.subcommands.ls.get_collection_list",
8383
return_value=[
@@ -90,6 +90,10 @@ async def mock_get_collections(client):
9090
],
9191
),
9292
):
93+
mock_client = MagicMock()
94+
mock_client_manager = MockClientManager.return_value
95+
mock_client_manager._create_client = AsyncMock(return_value=mock_client)
96+
9397
config = Config(pipe=True)
9498
await ls(config)
9599
captured = capsys.readouterr()
@@ -126,7 +130,7 @@ async def mock_get_collections(client):
126130
yield mock_collection
127131

128132
with (
129-
patch("vectorcode.subcommands.ls.get_client", return_value=mock_client),
133+
patch("vectorcode.subcommands.ls.ClientManager") as MockClientManager,
130134
patch(
131135
"vectorcode.subcommands.ls.get_collection_list",
132136
return_value=[
@@ -139,6 +143,10 @@ async def mock_get_collections(client):
139143
],
140144
),
141145
):
146+
mock_client = MagicMock()
147+
mock_client_manager = MockClientManager.return_value
148+
mock_client_manager._create_client = AsyncMock(return_value=mock_client)
149+
142150
config = Config(pipe=False)
143151
await ls(config)
144152
captured = capsys.readouterr()
@@ -159,7 +167,7 @@ async def mock_get_collections(client):
159167
# Test with HOME environment variable set
160168
monkeypatch.setenv("HOME", "/test")
161169
with (
162-
patch("vectorcode.subcommands.ls.get_client", return_value=mock_client),
170+
patch("vectorcode.subcommands.ls.ClientManager") as MockClientManager,
163171
patch(
164172
"vectorcode.subcommands.ls.get_collection_list",
165173
return_value=[
@@ -172,6 +180,9 @@ async def mock_get_collections(client):
172180
],
173181
),
174182
):
183+
mock_client = MagicMock()
184+
mock_client_manager = MockClientManager.return_value
185+
mock_client_manager._create_client = AsyncMock(return_value=mock_client)
175186
config = Config(pipe=False)
176187
await ls(config)
177188
captured = capsys.readouterr()

tests/subcommands/test_update.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def test_update_success():
1919
mock_client.get_max_batch_size.return_value = 100
2020

2121
with (
22-
patch("vectorcode.subcommands.update.get_client", return_value=mock_client),
22+
patch("vectorcode.subcommands.update.ClientManager"),
2323
patch(
2424
"vectorcode.subcommands.update.get_collection", return_value=mock_collection
2525
),
@@ -50,7 +50,7 @@ async def test_update_with_orphans():
5050
mock_client.get_max_batch_size.return_value = 100
5151

5252
with (
53-
patch("vectorcode.subcommands.update.get_client", return_value=mock_client),
53+
patch("vectorcode.subcommands.update.ClientManager"),
5454
patch(
5555
"vectorcode.subcommands.update.get_collection", return_value=mock_collection
5656
),
@@ -78,10 +78,11 @@ async def test_update_index_error():
7878
# mock_collection = AsyncMock()
7979

8080
with (
81-
patch("vectorcode.subcommands.update.get_client", return_value=mock_client),
81+
patch("vectorcode.subcommands.update.ClientManager") as MockClientManager,
8282
patch("vectorcode.subcommands.update.get_collection", side_effect=IndexError),
8383
patch("sys.stderr"),
8484
):
85+
MockClientManager.return_value._create_client.return_value = mock_client
8586
config = Config(project_root="/test/project", pipe=False)
8687
result = await update(config)
8788

@@ -94,10 +95,11 @@ async def test_update_value_error():
9495
# mock_collection = AsyncMock()
9596

9697
with (
97-
patch("vectorcode.subcommands.update.get_client", return_value=mock_client),
98+
patch("vectorcode.subcommands.update.ClientManager") as MockClientManager,
9899
patch("vectorcode.subcommands.update.get_collection", side_effect=ValueError),
99100
patch("sys.stderr"),
100101
):
102+
MockClientManager.return_value._create_client.return_value = mock_client
101103
config = Config(project_root="/test/project", pipe=False)
102104
result = await update(config)
103105

@@ -110,13 +112,14 @@ async def test_update_invalid_collection_exception():
110112
# mock_collection = AsyncMock()
111113

112114
with (
113-
patch("vectorcode.subcommands.update.get_client", return_value=mock_client),
115+
patch("vectorcode.subcommands.update.ClientManager") as MockClientManager,
114116
patch(
115117
"vectorcode.subcommands.update.get_collection",
116118
side_effect=InvalidCollectionException,
117119
),
118120
patch("sys.stderr"),
119121
):
122+
MockClientManager.return_value._create_client.return_value = mock_client
120123
config = Config(project_root="/test/project", pipe=False)
121124
result = await update(config)
122125

tests/subcommands/test_vectorise.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ async def test_vectorise(capsys):
370370

371371
with ExitStack() as stack:
372372
stack.enter_context(
373-
patch(
374-
"vectorcode.subcommands.vectorise.get_client", return_value=mock_client
375-
)
373+
patch("vectorcode.subcommands.vectorise.ClientManager"),
376374
)
377375
stack.enter_context(patch("os.path.isfile", return_value=False))
378376
stack.enter_context(
@@ -427,7 +425,7 @@ async def mock_chunked_add(*args, **kwargs):
427425
"vectorcode.subcommands.vectorise.chunked_add", side_effect=mock_chunked_add
428426
) as mock_add,
429427
patch("sys.stderr") as mock_stderr,
430-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
428+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
431429
patch(
432430
"vectorcode.subcommands.vectorise.get_collection",
433431
return_value=mock_collection,
@@ -438,6 +436,7 @@ async def mock_chunked_add(*args, **kwargs):
438436
lambda x: not (x.endswith("gitignore") or x.endswith("vectorcode.exclude")),
439437
),
440438
):
439+
MockClientManager.return_value._create_client.return_value = mock_client
441440
result = await vectorise(configs)
442441
assert result == 1
443442
mock_add.assert_called_once()
@@ -458,7 +457,7 @@ async def test_vectorise_orphaned_files():
458457
pipe=False,
459458
)
460459

461-
mock_client = AsyncMock()
460+
AsyncMock()
462461
mock_collection = AsyncMock()
463462

464463
# Define a mock response for collection.get in vectorise
@@ -494,7 +493,7 @@ def is_file_side_effect(path):
494493
"vectorcode.subcommands.vectorise.TreeSitterChunker",
495494
return_value=mock_chunker,
496495
),
497-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
496+
patch("vectorcode.subcommands.vectorise.ClientManager"),
498497
patch(
499498
"vectorcode.subcommands.vectorise.get_collection",
500499
return_value=mock_collection,
@@ -532,10 +531,11 @@ async def test_vectorise_collection_index_error():
532531
mock_client = AsyncMock()
533532

534533
with (
535-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
534+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
536535
patch("vectorcode.subcommands.vectorise.get_collection") as mock_get_collection,
537536
patch("os.path.isfile", return_value=False),
538537
):
538+
MockClientManager.return_value._create_client.return_value = mock_client
539539
mock_get_collection.side_effect = IndexError("Collection not found")
540540
result = await vectorise(configs)
541541
assert result == 1
@@ -558,14 +558,15 @@ async def test_vectorise_verify_ef_false():
558558
mock_collection = AsyncMock()
559559

560560
with (
561-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
561+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
562562
patch(
563563
"vectorcode.subcommands.vectorise.get_collection",
564564
return_value=mock_collection,
565565
),
566566
patch("vectorcode.subcommands.vectorise.verify_ef", return_value=False),
567567
patch("os.path.isfile", return_value=False),
568568
):
569+
MockClientManager.return_value._create_client.return_value = mock_client
569570
result = await vectorise(configs)
570571
assert result == 1
571572

@@ -588,7 +589,7 @@ async def test_vectorise_gitignore():
588589
mock_collection.get.return_value = {"metadatas": []}
589590

590591
with (
591-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
592+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
592593
patch(
593594
"vectorcode.subcommands.vectorise.get_collection",
594595
return_value=mock_collection,
@@ -608,6 +609,7 @@ async def test_vectorise_gitignore():
608609
"vectorcode.subcommands.vectorise.exclude_paths_by_spec"
609610
) as mock_exclude_paths,
610611
):
612+
MockClientManager.return_value._create_client.return_value = mock_client
611613
await vectorise(configs)
612614
mock_exclude_paths.assert_called_once()
613615

@@ -635,7 +637,7 @@ async def test_vectorise_exclude_file(tmpdir):
635637
mock_collection.get.return_value = {"ids": []}
636638

637639
with (
638-
patch("vectorcode.subcommands.vectorise.get_client", return_value=mock_client),
640+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
639641
patch(
640642
"vectorcode.subcommands.vectorise.get_collection",
641643
return_value=mock_collection,
@@ -652,6 +654,7 @@ async def test_vectorise_exclude_file(tmpdir):
652654
),
653655
patch("vectorcode.subcommands.vectorise.chunked_add") as mock_chunked_add,
654656
):
657+
MockClientManager.return_value._create_client.return_value = mock_client
655658
await vectorise(configs)
656659
# Assert that chunked_add is only called for test_file.py, not excluded_file.py
657660
call_args = [call[0][0] for call in mock_chunked_add.call_args_list]
@@ -664,7 +667,6 @@ async def test_vectorise_exclude_file(tmpdir):
664667

665668

666669
@pytest.mark.asyncio
667-
@patch("vectorcode.subcommands.vectorise.get_client", new_callable=AsyncMock)
668670
@patch("vectorcode.subcommands.vectorise.get_collection", new_callable=AsyncMock)
669671
@patch("vectorcode.subcommands.vectorise.expand_globs", new_callable=AsyncMock)
670672
@patch("vectorcode.subcommands.vectorise.chunked_add", new_callable=AsyncMock)
@@ -681,7 +683,6 @@ async def test_vectorise_uses_global_exclude_when_local_missing(
681683
mock_chunked_add,
682684
mock_expand_globs,
683685
mock_get_collection,
684-
mock_get_client,
685686
tmp_path,
686687
):
687688
"""
@@ -712,14 +713,20 @@ def isfile_side_effect(p):
712713

713714
global_exclude_content = "*.bin"
714715
m_open = mock_open(read_data=global_exclude_content)
715-
with patch("builtins.open", m_open):
716+
with (
717+
patch("builtins.open", m_open),
718+
patch("vectorcode.subcommands.vectorise.ClientManager") as MockClientManager,
719+
):
716720
mock_spec_instance = MagicMock()
717721
mock_spec_instance.match_file = lambda path: str(path).endswith(".bin")
718722
mock_gitignore_spec.from_lines.return_value = mock_spec_instance
719723

720724
mock_client_instance = AsyncMock()
721725
mock_client_instance.get_max_batch_size = AsyncMock(return_value=100)
722-
mock_get_client.return_value = mock_client_instance
726+
727+
MockClientManager.return_value._create_client.return_value = (
728+
mock_client_instance
729+
)
723730

724731
mock_collection_instance = AsyncMock()
725732
mock_collection_instance.get = AsyncMock(

0 commit comments

Comments
 (0)