Skip to content

Commit 27c9a0d

Browse files
committed
tests(cli): fix failed tests due to ClientManager refactor.
1 parent 9e0b64e commit 27c9a0d

File tree

10 files changed

+269
-282
lines changed

10 files changed

+269
-282
lines changed

tests/subcommands/query/test_query.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ async def test_query_success(mock_config):
355355
mock_collection = AsyncMock()
356356

357357
with (
358-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
358+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
359359
patch(
360360
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
361361
),
@@ -367,6 +367,7 @@ async def test_query_success(mock_config):
367367
patch("os.path.relpath", return_value="rel/path.py"),
368368
patch("os.path.abspath", return_value="/abs/path.py"),
369369
):
370+
MockClientManager.return_value._create_client.return_value = mock_client
370371
# Set up the mock file paths and contents
371372
mock_get_files.return_value = ["file1.py", "file2.py"]
372373
mock_file_handle = MagicMock()
@@ -396,7 +397,7 @@ async def test_query_pipe_mode(mock_config):
396397
mock_collection = AsyncMock()
397398

398399
with (
399-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
400+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
400401
patch(
401402
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
402403
),
@@ -408,6 +409,7 @@ async def test_query_pipe_mode(mock_config):
408409
patch("os.path.relpath", return_value="rel/path.py"),
409410
patch("os.path.abspath", return_value="/abs/path.py"),
410411
):
412+
MockClientManager.return_value._create_client.return_value = mock_client
411413
# Set up the mock file paths and contents
412414
mock_get_files.return_value = ["file1.py", "file2.py"]
413415
mock_file_handle = MagicMock()
@@ -434,7 +436,7 @@ async def test_query_absolute_path(mock_config):
434436
mock_collection = AsyncMock()
435437

436438
with (
437-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
439+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
438440
patch(
439441
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
440442
),
@@ -445,6 +447,7 @@ async def test_query_absolute_path(mock_config):
445447
patch("os.path.relpath", return_value="rel/path.py"),
446448
patch("os.path.abspath", return_value="/abs/path.py"),
447449
):
450+
MockClientManager.return_value._create_client.return_value = mock_client
448451
# Set up the mock file paths and contents
449452
mock_get_files.return_value = ["file1.py"]
450453
mock_file_handle = MagicMock()
@@ -463,7 +466,7 @@ async def test_query_collection_not_found():
463466
config = Config(project_root="/test/project")
464467

465468
with (
466-
patch("vectorcode.subcommands.query.get_client"),
469+
patch("vectorcode.subcommands.query.ClientManager"),
467470
patch("vectorcode.subcommands.query.get_collection") as mock_get_collection,
468471
patch("sys.stderr"),
469472
):
@@ -482,7 +485,7 @@ async def test_query_invalid_collection():
482485
config = Config(project_root="/test/project")
483486

484487
with (
485-
patch("vectorcode.subcommands.query.get_client"),
488+
patch("vectorcode.subcommands.query.ClientManager"),
486489
patch("vectorcode.subcommands.query.get_collection") as mock_get_collection,
487490
patch("sys.stderr"),
488491
):
@@ -503,7 +506,7 @@ async def test_query_invalid_dimension():
503506
config = Config(project_root="/test/project")
504507

505508
with (
506-
patch("vectorcode.subcommands.query.get_client"),
509+
patch("vectorcode.subcommands.query.ClientManager"),
507510
patch("vectorcode.subcommands.query.get_collection") as mock_get_collection,
508511
patch("sys.stderr"),
509512
):
@@ -524,14 +527,15 @@ async def test_query_invalid_file(mock_config):
524527
mock_collection = AsyncMock()
525528

526529
with (
527-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
530+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
528531
patch(
529532
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
530533
),
531534
patch("vectorcode.subcommands.query.verify_ef", return_value=True),
532535
patch("vectorcode.subcommands.query.get_query_result_files") as mock_get_files,
533536
patch("os.path.isfile", return_value=False),
534537
):
538+
MockClientManager.return_value._create_client.return_value = mock_client
535539
# Set up the mock file paths
536540
mock_get_files.return_value = ["invalid_file.py"]
537541

@@ -549,12 +553,13 @@ async def test_query_invalid_ef(mock_config):
549553
mock_collection = AsyncMock()
550554

551555
with (
552-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
556+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
553557
patch(
554558
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
555559
),
556560
patch("vectorcode.subcommands.query.verify_ef", return_value=False),
557561
):
562+
MockClientManager.return_value._create_client.return_value = mock_client
558563
# Call the function
559564
result = await query(mock_config)
560565

@@ -580,13 +585,14 @@ async def test_query_chunk_mode_no_metadata_fallback(mock_config):
580585
mock_collection.get.return_value = {"ids": []}
581586

582587
with (
583-
patch("vectorcode.subcommands.query.get_client", return_value=mock_client),
588+
patch("vectorcode.subcommands.query.ClientManager") as MockClientManager,
584589
patch(
585590
"vectorcode.subcommands.query.get_collection", return_value=mock_collection
586591
),
587592
patch("vectorcode.subcommands.query.verify_ef", return_value=True),
588593
patch("vectorcode.subcommands.query.build_query_results") as mock_build_results,
589594
):
595+
MockClientManager.return_value._create_client.return_value = mock_client
590596
mock_build_results.return_value = [] # Return empty results for simplicity
591597

592598
result = await query(mock_config)

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

0 commit comments

Comments
 (0)