Skip to content

Commit ab6900a

Browse files
committed
chore: Fix unit tests
1 parent 2ca4038 commit ab6900a

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
@@ -411,20 +415,32 @@ def post_callback(url, **kwargs):
411415
result = tool(param1="test")
412416
assert result == expected_payload["result"]
413417

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

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

429445

430446
class TestSyncAuth:

0 commit comments

Comments
 (0)