openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/conftest.py
84lines · modeblame
cc2c1fc1stainless-app[bot]1 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | | |
98d779fbStainless Bot2 years ago | 3 | from __future__ import annotations |
| 4 | | |
| 5 | import os | |
08b8179aDavid Schnurr2 years ago | 6 | import logging |
98d779fbStainless Bot2 years ago | 7 | from typing import TYPE_CHECKING, Iterator, AsyncIterator |
08b8179aDavid Schnurr2 years ago | 8 | |
c62e9907stainless-app[bot]1 years ago | 9 | import httpx |
08b8179aDavid Schnurr2 years ago | 10 | import pytest |
44ac1cefStainless Bot1 years ago | 11 | from pytest_asyncio import is_async_test |
08b8179aDavid Schnurr2 years ago | 12 | |
c62e9907stainless-app[bot]1 years ago | 13 | from openai import OpenAI, AsyncOpenAI, DefaultAioHttpClient |
| 14 | from openai._utils import is_dict | |
98d779fbStainless Bot2 years ago | 15 | |
| 16 | if TYPE_CHECKING: | |
6d110a14stainless-app[bot]1 years ago | 17 | from _pytest.fixtures import FixtureRequest # pyright: ignore[reportPrivateImportUsage] |
98d779fbStainless Bot2 years ago | 18 | |
08b8179aDavid Schnurr2 years ago | 19 | pytest.register_assert_rewrite("tests.utils") |
| 20 | | |
| 21 | logging.getLogger("openai").setLevel(logging.DEBUG) | |
| 22 | | |
| 23 | | |
44ac1cefStainless Bot1 years ago | 24 | # automatically add `pytest.mark.asyncio()` to all of our async tests |
| 25 | # so we don't have to add that boilerplate everywhere | |
| 26 | def pytest_collection_modifyitems(items: list[pytest.Function]) -> None: | |
| 27 | pytest_asyncio_tests = (item for item in items if is_async_test(item)) | |
| 28 | session_scope_marker = pytest.mark.asyncio(loop_scope="session") | |
| 29 | for async_test in pytest_asyncio_tests: | |
| 30 | async_test.add_marker(session_scope_marker, append=False) | |
98d779fbStainless Bot2 years ago | 31 | |
c62e9907stainless-app[bot]1 years ago | 32 | # We skip tests that use both the aiohttp client and respx_mock as respx_mock |
| 33 | # doesn't support custom transports. | |
| 34 | for item in items: | |
| 35 | if "async_client" not in item.fixturenames or "respx_mock" not in item.fixturenames: | |
| 36 | continue | |
| 37 | | |
| 38 | if not hasattr(item, "callspec"): | |
| 39 | continue | |
| 40 | | |
| 41 | async_client_param = item.callspec.params.get("async_client") | |
| 42 | if is_dict(async_client_param) and async_client_param.get("http_client") == "aiohttp": | |
| 43 | item.add_marker(pytest.mark.skip(reason="aiohttp client is not compatible with respx_mock")) | |
| 44 | | |
98d779fbStainless Bot2 years ago | 45 | |
| 46 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 47 | | |
| 48 | api_key = "My API Key" | |
| 49 | | |
| 50 | | |
| 51 | @pytest.fixture(scope="session") | |
| 52 | def client(request: FixtureRequest) -> Iterator[OpenAI]: | |
| 53 | strict = getattr(request, "param", True) | |
| 54 | if not isinstance(strict, bool): | |
| 55 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") | |
| 56 | | |
| 57 | with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client: | |
| 58 | yield client | |
| 59 | | |
| 60 | | |
| 61 | @pytest.fixture(scope="session") | |
| 62 | async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]: | |
c62e9907stainless-app[bot]1 years ago | 63 | param = getattr(request, "param", True) |
| 64 | | |
| 65 | # defaults | |
| 66 | strict = True | |
| 67 | http_client: None | httpx.AsyncClient = None | |
| 68 | | |
| 69 | if isinstance(param, bool): | |
| 70 | strict = param | |
| 71 | elif is_dict(param): | |
| 72 | strict = param.get("strict", True) | |
| 73 | assert isinstance(strict, bool) | |
| 74 | | |
| 75 | http_client_type = param.get("http_client", "httpx") | |
| 76 | if http_client_type == "aiohttp": | |
| 77 | http_client = DefaultAioHttpClient() | |
| 78 | else: | |
| 79 | raise TypeError(f"Unexpected fixture parameter type {type(param)}, expected bool or dict") | |
| 80 | | |
| 81 | async with AsyncOpenAI( | |
| 82 | base_url=base_url, api_key=api_key, _strict_response_validation=strict, http_client=http_client | |
| 83 | ) as client: | |
98d779fbStainless Bot2 years ago | 84 | yield client |