|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from aidial_client import AsyncDialClientPool, Dial, DialClientPool |
| 6 | +from aidial_client._client import AsyncDial |
| 7 | + |
| 8 | + |
| 9 | +def test_dial_close(): |
| 10 | + client = Dial(api_key="dummy", base_url="http://dial.core") |
| 11 | + |
| 12 | + with patch.object( |
| 13 | + client._http_client.internal_http_client, "close" |
| 14 | + ) as close_mock: |
| 15 | + client.close() |
| 16 | + |
| 17 | + close_mock.assert_called_once() |
| 18 | + |
| 19 | + |
| 20 | +def test_dial_context_manager(): |
| 21 | + client = Dial(api_key="dummy", base_url="http://dial.core") |
| 22 | + |
| 23 | + with ( |
| 24 | + patch.object( |
| 25 | + client._http_client.internal_http_client, "close" |
| 26 | + ) as close_mock, |
| 27 | + client as managed_client, |
| 28 | + ): |
| 29 | + assert managed_client is client |
| 30 | + |
| 31 | + close_mock.assert_called_once() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_async_dial_aclose(): |
| 36 | + client = AsyncDial(api_key="dummy", base_url="http://dial.core") |
| 37 | + |
| 38 | + with patch.object( |
| 39 | + client._http_client.internal_http_client, "aclose" |
| 40 | + ) as aclose_mock: |
| 41 | + await client.aclose() |
| 42 | + |
| 43 | + aclose_mock.assert_awaited_once() |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_async_dial_context_manager(): |
| 48 | + client = AsyncDial(api_key="dummy", base_url="http://dial.core") |
| 49 | + |
| 50 | + with patch.object( |
| 51 | + client._http_client.internal_http_client, "aclose" |
| 52 | + ) as aclose_mock: |
| 53 | + async with client as managed_client: |
| 54 | + assert managed_client is client |
| 55 | + |
| 56 | + aclose_mock.assert_awaited_once() |
| 57 | + |
| 58 | + |
| 59 | +def test_dial_client_pool_close(): |
| 60 | + pool = DialClientPool() |
| 61 | + |
| 62 | + with patch.object(pool._internal_http_client, "close") as close_mock: |
| 63 | + pool.close() |
| 64 | + |
| 65 | + close_mock.assert_called_once() |
| 66 | + |
| 67 | + |
| 68 | +def test_dial_client_pool_context_manager(): |
| 69 | + pool = DialClientPool() |
| 70 | + |
| 71 | + with ( |
| 72 | + patch.object(pool._internal_http_client, "close") as close_mock, |
| 73 | + pool as managed_pool, |
| 74 | + ): |
| 75 | + assert managed_pool is pool |
| 76 | + |
| 77 | + close_mock.assert_called_once() |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.asyncio |
| 81 | +async def test_async_dial_client_pool_aclose(): |
| 82 | + pool = AsyncDialClientPool() |
| 83 | + |
| 84 | + with patch.object(pool._internal_http_client, "aclose") as aclose_mock: |
| 85 | + await pool.aclose() |
| 86 | + |
| 87 | + aclose_mock.assert_awaited_once() |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.asyncio |
| 91 | +async def test_async_dial_client_pool_context_manager(): |
| 92 | + pool = AsyncDialClientPool() |
| 93 | + |
| 94 | + with patch.object(pool._internal_http_client, "aclose") as aclose_mock: |
| 95 | + async with pool as managed_pool: |
| 96 | + assert managed_pool is pool |
| 97 | + |
| 98 | + aclose_mock.assert_awaited_once() |
0 commit comments