openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/conftest.py
91lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | import logging |
| 7 | from typing import TYPE_CHECKING, Iterator, AsyncIterator |
| 8 | |
| 9 | import httpx |
| 10 | import pytest |
| 11 | from pytest_asyncio import is_async_test |
| 12 | |
| 13 | from openai import OpenAI, AsyncOpenAI, DefaultAioHttpClient |
| 14 | from openai._utils import is_dict |
| 15 | |
| 16 | if TYPE_CHECKING: |
| 17 | from _pytest.fixtures import FixtureRequest # pyright: ignore[reportPrivateImportUsage] |
| 18 | |
| 19 | pytest.register_assert_rewrite("tests.utils") |
| 20 | |
| 21 | logging.getLogger("openai").setLevel(logging.DEBUG) |
| 22 | |
| 23 | |
| 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) |
| 31 | |
| 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 | |
| 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 | admin_api_key = "My Admin API Key" |
| 50 | |
| 51 | |
| 52 | @pytest.fixture(scope="session") |
| 53 | def client(request: FixtureRequest) -> Iterator[OpenAI]: |
| 54 | strict = getattr(request, "param", True) |
| 55 | if not isinstance(strict, bool): |
| 56 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") |
| 57 | |
| 58 | with OpenAI( |
| 59 | base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=strict |
| 60 | ) as client: |
| 61 | yield client |
| 62 | |
| 63 | |
| 64 | @pytest.fixture(scope="session") |
| 65 | async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]: |
| 66 | param = getattr(request, "param", True) |
| 67 | |
| 68 | # defaults |
| 69 | strict = True |
| 70 | http_client: None | httpx.AsyncClient = None |
| 71 | |
| 72 | if isinstance(param, bool): |
| 73 | strict = param |
| 74 | elif is_dict(param): |
| 75 | strict = param.get("strict", True) |
| 76 | assert isinstance(strict, bool) |
| 77 | |
| 78 | http_client_type = param.get("http_client", "httpx") |
| 79 | if http_client_type == "aiohttp": |
| 80 | http_client = DefaultAioHttpClient() |
| 81 | else: |
| 82 | raise TypeError(f"Unexpected fixture parameter type {type(param)}, expected bool or dict") |
| 83 | |
| 84 | async with AsyncOpenAI( |
| 85 | base_url=base_url, |
| 86 | api_key=api_key, |
| 87 | admin_api_key=admin_api_key, |
| 88 | _strict_response_validation=strict, |
| 89 | http_client=http_client, |
| 90 | ) as client: |
| 91 | yield client |