|
| 1 | +import sys |
| 2 | +import typing |
| 3 | +import unittest |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import httpx |
| 7 | + |
| 8 | + |
| 9 | +class TestMakeDefaultAsyncClient(unittest.TestCase): |
| 10 | + """Tests for _make_default_async_client in base_client.py.""" |
| 11 | + |
| 12 | + def test_without_httpx_aiohttp_returns_httpx_async_client(self) -> None: |
| 13 | + """When httpx_aiohttp is not installed, returns plain httpx.AsyncClient.""" |
| 14 | + with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}): |
| 15 | + # Re-import to pick up the mocked module state |
| 16 | + from cohere.base_client import _make_default_async_client |
| 17 | + |
| 18 | + client = _make_default_async_client(timeout=300, follow_redirects=True) |
| 19 | + self.assertIsInstance(client, httpx.AsyncClient) |
| 20 | + self.assertEqual(client.timeout.read, 300) |
| 21 | + self.assertTrue(client.follow_redirects) |
| 22 | + |
| 23 | + def test_without_httpx_aiohttp_follow_redirects_none(self) -> None: |
| 24 | + """When follow_redirects is None, omits it from httpx.AsyncClient.""" |
| 25 | + with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}): |
| 26 | + from cohere.base_client import _make_default_async_client |
| 27 | + |
| 28 | + client = _make_default_async_client(timeout=300, follow_redirects=None) |
| 29 | + self.assertIsInstance(client, httpx.AsyncClient) |
| 30 | + # httpx default is False when not specified |
| 31 | + self.assertFalse(client.follow_redirects) |
| 32 | + |
| 33 | + def test_with_httpx_aiohttp_returns_aiohttp_client(self) -> None: |
| 34 | + """When httpx_aiohttp is installed, returns HttpxAiohttpClient.""" |
| 35 | + try: |
| 36 | + import httpx_aiohttp |
| 37 | + except ImportError: |
| 38 | + self.skipTest("httpx_aiohttp not installed") |
| 39 | + |
| 40 | + from cohere.base_client import _make_default_async_client |
| 41 | + |
| 42 | + client = _make_default_async_client(timeout=300, follow_redirects=True) |
| 43 | + self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient) |
| 44 | + self.assertEqual(client.timeout.read, 300) |
| 45 | + self.assertTrue(client.follow_redirects) |
| 46 | + |
| 47 | + def test_with_httpx_aiohttp_follow_redirects_none(self) -> None: |
| 48 | + """When httpx_aiohttp is installed and follow_redirects is None, omits it.""" |
| 49 | + try: |
| 50 | + import httpx_aiohttp |
| 51 | + except ImportError: |
| 52 | + self.skipTest("httpx_aiohttp not installed") |
| 53 | + |
| 54 | + from cohere.base_client import _make_default_async_client |
| 55 | + |
| 56 | + client = _make_default_async_client(timeout=300, follow_redirects=None) |
| 57 | + self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient) |
| 58 | + # httpx default is False when not specified |
| 59 | + self.assertFalse(client.follow_redirects) |
| 60 | + |
| 61 | + def test_explicit_httpx_client_bypasses_autodetect(self) -> None: |
| 62 | + """When user passes httpx_client explicitly, auto-detect is not used.""" |
| 63 | + explicit_client = httpx.AsyncClient(timeout=60) |
| 64 | + # Simulate what AsyncBaseCohere.__init__ does: |
| 65 | + # httpx_client if httpx_client is not None else _make_default_async_client(...) |
| 66 | + result = explicit_client if explicit_client is not None else None |
| 67 | + self.assertIs(result, explicit_client) |
| 68 | + self.assertEqual(result.timeout.read, 60) |
| 69 | + |
| 70 | + |
| 71 | +class TestDefaultClients(unittest.TestCase): |
| 72 | + """Tests for convenience classes in _default_clients.py.""" |
| 73 | + |
| 74 | + def test_default_async_httpx_client_defaults(self) -> None: |
| 75 | + """DefaultAsyncHttpxClient applies SDK defaults.""" |
| 76 | + from cohere._default_clients import COHERE_DEFAULT_TIMEOUT, DefaultAsyncHttpxClient |
| 77 | + |
| 78 | + client = DefaultAsyncHttpxClient() |
| 79 | + self.assertIsInstance(client, httpx.AsyncClient) |
| 80 | + self.assertEqual(client.timeout.read, COHERE_DEFAULT_TIMEOUT) |
| 81 | + self.assertTrue(client.follow_redirects) |
| 82 | + |
| 83 | + def test_default_async_httpx_client_overrides(self) -> None: |
| 84 | + """DefaultAsyncHttpxClient allows overriding defaults.""" |
| 85 | + from cohere._default_clients import DefaultAsyncHttpxClient |
| 86 | + |
| 87 | + client = DefaultAsyncHttpxClient(timeout=60, follow_redirects=False) |
| 88 | + self.assertEqual(client.timeout.read, 60) |
| 89 | + self.assertFalse(client.follow_redirects) |
| 90 | + |
| 91 | + def test_default_aiohttp_client_without_package(self) -> None: |
| 92 | + """DefaultAioHttpClient raises RuntimeError when httpx_aiohttp not installed.""" |
| 93 | + with mock.patch.dict(sys.modules, {"httpx_aiohttp": None}): |
| 94 | + # Need to reload the module to pick up the mock |
| 95 | + import importlib |
| 96 | + import cohere._default_clients |
| 97 | + |
| 98 | + importlib.reload(cohere._default_clients) |
| 99 | + |
| 100 | + with self.assertRaises(RuntimeError) as ctx: |
| 101 | + cohere._default_clients.DefaultAioHttpClient() |
| 102 | + self.assertIn("pip install cohere[aiohttp]", str(ctx.exception)) |
| 103 | + |
| 104 | + # Reload again to restore original state |
| 105 | + importlib.reload(cohere._default_clients) |
| 106 | + |
| 107 | + def test_default_aiohttp_client_with_package(self) -> None: |
| 108 | + """DefaultAioHttpClient works when httpx_aiohttp is installed.""" |
| 109 | + try: |
| 110 | + import httpx_aiohttp |
| 111 | + except ImportError: |
| 112 | + self.skipTest("httpx_aiohttp not installed") |
| 113 | + |
| 114 | + from cohere._default_clients import COHERE_DEFAULT_TIMEOUT, DefaultAioHttpClient |
| 115 | + |
| 116 | + client = DefaultAioHttpClient() |
| 117 | + self.assertIsInstance(client, httpx_aiohttp.HttpxAiohttpClient) |
| 118 | + self.assertEqual(client.timeout.read, COHERE_DEFAULT_TIMEOUT) |
| 119 | + self.assertTrue(client.follow_redirects) |
0 commit comments