Skip to content

Commit d554a48

Browse files
committed
chore: Fix unit tests
1 parent 15f102d commit d554a48

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

packages/toolbox-core/tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ async def test_add_headers_success(
14431443
)
14441444

14451445
async with ToolboxClient(TEST_BASE_URL) as client:
1446-
await client.add_headers(static_header)
1446+
client.add_headers(static_header)
14471447
assert client._ToolboxClient__client_headers == static_header
14481448

14491449
tool = await client.load_tool(tool_name)

packages/toolbox-core/tests/test_sync_client.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import inspect
1717
from typing import Any, Callable, Mapping, Optional
18-
from unittest.mock import AsyncMock, patch
18+
from unittest.mock import AsyncMock, Mock, patch
1919

2020
import pytest
2121
from aioresponses import CallbackResult, aioresponses
@@ -44,8 +44,12 @@ def sync_client_environment():
4444
# This ensures any client created will start a new loop/thread.
4545

4646
# Ensure no loop/thread is running from a previous misbehaving test or setup
47-
assert original_loop is None or not original_loop.is_running()
48-
assert original_thread is None or not original_thread.is_alive()
47+
if original_loop and original_loop.is_running():
48+
original_loop.call_soon_threadsafe(original_loop.stop)
49+
if original_thread and original_thread.is_alive():
50+
original_thread.join(timeout=5)
51+
ToolboxSyncClient._ToolboxSyncClient__loop = None
52+
ToolboxSyncClient._ToolboxSyncClient__thread = None
4953

5054
ToolboxSyncClient._ToolboxSyncClient__loop = None
5155
ToolboxSyncClient._ToolboxSyncClient__thread = None
@@ -408,20 +412,32 @@ def post_callback(url, **kwargs):
408412
result = tool(param1="test")
409413
assert result == expected_payload["result"]
410414

411-
@pytest.mark.usefixtures("sync_client_environment")
412415
def test_sync_add_headers_duplicate_fail(self):
413-
"""
414-
Tests that adding a duplicate header via add_headers raises ValueError.
415-
Manually create client to control initial headers.
416-
"""
416+
"""Tests that adding a duplicate header via add_headers raises ValueError (from async client)."""
417417
initial_headers = {"X-Initial-Header": "initial_value"}
418+
mock_async_client = AsyncMock(spec=ToolboxClient)
418419

419-
with ToolboxSyncClient(TEST_BASE_URL, client_headers=initial_headers) as client:
420-
with pytest.raises(
421-
ValueError,
422-
match="Client header\\(s\\) `X-Initial-Header` already registered",
423-
):
424-
client.add_headers({"X-Initial-Header": "another_value"})
420+
# Configure add_headers to simulate the ValueError from ToolboxClient
421+
def mock_add_headers(headers):
422+
# Simulate ToolboxClient's check
423+
if "X-Initial-Header" in headers:
424+
raise ValueError(
425+
"Client header(s) `X-Initial-Header` already registered"
426+
)
427+
428+
mock_async_client.add_headers = Mock(side_effect=mock_add_headers)
429+
430+
with patch(
431+
"toolbox_core.sync_client.ToolboxClient", return_value=mock_async_client
432+
):
433+
with ToolboxSyncClient(
434+
TEST_BASE_URL, client_headers=initial_headers
435+
) as client:
436+
with pytest.raises(
437+
ValueError,
438+
match="Client header\\(s\\) `X-Initial-Header` already registered",
439+
):
440+
client.add_headers({"X-Initial-Header": "another_value"})
425441

426442

427443
class TestSyncAuth:

0 commit comments

Comments
 (0)