Skip to content

Commit dae749f

Browse files
committed
Fix test mocking
1 parent d51af26 commit dae749f

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

tests/vec_inf/cli/test_cli.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def _exists(p):
6868
# Allow access to the default config file
6969
if str(p).endswith("config/models.yaml"):
7070
return True
71+
# Allow access to the default log directory
72+
if str(p).endswith(".vec-inf-logs"):
73+
return True
7174
# Use mock_exists for other paths
7275
return mock_exists(p)
7376

@@ -143,13 +146,24 @@ def test_paths():
143146
def mock_truediv(test_paths):
144147
"""Fixture providing path joining mock."""
145148

146-
def _mock_truediv(self, other):
149+
def _mock_truediv(*args):
150+
# Handle the case where it's called with just one argument
151+
if len(args) == 1:
152+
other = args[0]
153+
if str(other) == ".vec-inf-logs":
154+
return test_paths["log_dir"]
155+
return Path(str(other))
156+
157+
# Normal case with self and other
158+
self, other = args
147159
if str(self) == str(test_paths["weights_dir"]) and other == "unknown-model":
148160
return test_paths["unknown_model"]
149161
if str(self) == str(test_paths["log_dir"]):
150162
return test_paths["log_dir"] / other
151163
if str(self) == str(test_paths["log_dir"] / "model_family_placeholder"):
152164
return test_paths["log_dir"] / "model_family_placeholder" / other
165+
if str(self) == "/home/user" and str(other) == ".vec-inf-logs":
166+
return test_paths["log_dir"]
153167
return Path(str(self)) / str(other)
154168

155169
return _mock_truediv
@@ -197,16 +211,26 @@ def base_patches(test_paths, mock_truediv, debug_helper):
197211
patch("pathlib.Path.open", debug_helper.tracked_mock_open),
198212
patch("pathlib.Path.expanduser", return_value=test_paths["log_dir"]),
199213
patch("pathlib.Path.resolve", return_value=debug_helper.config_file.parent),
200-
patch(
201-
"pathlib.Path.parent", return_value=debug_helper.config_file.parent.parent
202-
),
214+
patch("pathlib.Path.parent", return_value=debug_helper.config_file.parent.parent),
203215
patch("pathlib.Path.__truediv__", side_effect=mock_truediv),
216+
patch("pathlib.Path.iterdir", return_value=[]), # Mock empty directory listing
204217
patch("json.dump"),
205218
patch("pathlib.Path.touch"),
206219
patch("vec_inf.cli._helper.Path", return_value=test_paths["weights_dir"]),
220+
patch("pathlib.Path.home", return_value=Path("/home/user")), # Mock home directory
207221
]
208222

209223

224+
@pytest.fixture
225+
def apply_base_patches(base_patches):
226+
"""Fixture to apply all base patches."""
227+
with ExitStack() as stack:
228+
# Apply all patches
229+
for patch_obj in base_patches:
230+
stack.enter_context(patch_obj)
231+
yield
232+
233+
210234
def test_launch_command_success(runner, mock_launch_output, path_exists, debug_helper):
211235
"""Test successful model launch with minimal required arguments."""
212236
test_log_dir = Path("/tmp/test_vec_inf_logs")
@@ -374,7 +398,7 @@ def test_list_single_model(runner):
374398

375399

376400
def test_metrics_command_pending_server(
377-
runner, mock_status_output, path_exists, debug_helper
401+
runner, mock_status_output, path_exists, debug_helper, apply_base_patches
378402
):
379403
"""Test metrics command when server is pending."""
380404
with (
@@ -398,7 +422,7 @@ def test_metrics_command_pending_server(
398422

399423

400424
def test_metrics_command_server_not_ready(
401-
runner, mock_status_output, path_exists, debug_helper
425+
runner, mock_status_output, path_exists, debug_helper, apply_base_patches
402426
):
403427
"""Test metrics command when server is running but not ready."""
404428
with (
@@ -420,7 +444,7 @@ def test_metrics_command_server_not_ready(
420444

421445
@patch("vec_inf.cli._helper.requests.get")
422446
def test_metrics_command_server_ready(
423-
mock_get, runner, mock_status_output, path_exists, debug_helper
447+
mock_get, runner, mock_status_output, path_exists, debug_helper, apply_base_patches
424448
):
425449
"""Test metrics command when server is ready and returning metrics."""
426450
metrics_response = """
@@ -459,7 +483,7 @@ def test_metrics_command_server_ready(
459483

460484
@patch("vec_inf.cli._helper.requests.get")
461485
def test_metrics_command_request_failed(
462-
mock_get, runner, mock_status_output, path_exists, debug_helper
486+
mock_get, runner, mock_status_output, path_exists, debug_helper, apply_base_patches
463487
):
464488
"""Test metrics command when request to metrics endpoint fails."""
465489
mock_get.side_effect = requests.exceptions.RequestException("Connection refused")

0 commit comments

Comments
 (0)