|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import inspect
|
17 |
| -from typing import Any, Callable, Mapping, Optional |
| 17 | +from asyncio import AbstractEventLoop |
| 18 | +from threading import Thread |
| 19 | +from typing import Any, Callable, Generator, Mapping, Optional |
18 | 20 | from unittest.mock import AsyncMock, Mock, patch
|
19 | 21 |
|
20 | 22 | import pytest
|
@@ -608,3 +610,53 @@ def test_constructor_getters_missing_fail(
|
608 | 610 | tool_name_auth,
|
609 | 611 | auth_token_getters={UNUSED_AUTH_SERVICE: lambda: "token"},
|
610 | 612 | )
|
| 613 | + |
| 614 | + |
| 615 | +# --- Tests for @property methods of ToolboxSyncClient --- |
| 616 | + |
| 617 | + |
| 618 | +@pytest.fixture |
| 619 | +def sync_client_with_mocks() -> Generator[ToolboxSyncClient, Any, Any]: |
| 620 | + """ |
| 621 | + Fixture to create a ToolboxSyncClient with mocked internal async client |
| 622 | + without relying on actual network calls during init. |
| 623 | + """ |
| 624 | + with patch( |
| 625 | + "toolbox_core.sync_client.ToolboxClient", autospec=True |
| 626 | + ) as MockToolboxClient: |
| 627 | + # Mock the async client's constructor to return an AsyncMock instance |
| 628 | + mock_async_client_instance = AsyncMock(spec=ToolboxClient) |
| 629 | + MockToolboxClient.return_value = mock_async_client_instance |
| 630 | + |
| 631 | + # Mock the run_coroutine_threadsafe and its result() |
| 632 | + with patch("asyncio.run_coroutine_threadsafe") as mock_run_coroutine_threadsafe: |
| 633 | + mock_future = Mock() |
| 634 | + mock_future.result.return_value = mock_async_client_instance |
| 635 | + mock_run_coroutine_threadsafe.return_value = mock_future |
| 636 | + |
| 637 | + client = ToolboxSyncClient(TEST_BASE_URL) |
| 638 | + yield client |
| 639 | + |
| 640 | + |
| 641 | +def test_sync_client_underscore_async_client_property( |
| 642 | + sync_client_with_mocks: ToolboxSyncClient, |
| 643 | +): |
| 644 | + """Tests the _async_client property.""" |
| 645 | + assert isinstance(sync_client_with_mocks._async_client, AsyncMock) |
| 646 | + |
| 647 | + |
| 648 | +def test_sync_client_underscore_loop_property( |
| 649 | + sync_client_with_mocks: ToolboxSyncClient, |
| 650 | +): |
| 651 | + """Tests the _loop property.""" |
| 652 | + assert sync_client_with_mocks._loop is not None |
| 653 | + assert isinstance(sync_client_with_mocks._loop, AbstractEventLoop) |
| 654 | + |
| 655 | + |
| 656 | +def test_sync_client_underscore_thread_property( |
| 657 | + sync_client_with_mocks: ToolboxSyncClient, |
| 658 | +): |
| 659 | + """Tests the _thread property.""" |
| 660 | + assert sync_client_with_mocks._thread is not None |
| 661 | + assert isinstance(sync_client_with_mocks._thread, Thread) |
| 662 | + assert sync_client_with_mocks._thread.is_alive() |
0 commit comments