openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/conftest.py
53lines · 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 pytest |
| 10 | from pytest_asyncio import is_async_test |
| 11 | |
| 12 | from openai import OpenAI, AsyncOpenAI |
| 13 | |
| 14 | if TYPE_CHECKING: |
| 15 | from _pytest.fixtures import FixtureRequest # pyright: ignore[reportPrivateImportUsage] |
| 16 | |
| 17 | pytest.register_assert_rewrite("tests.utils") |
| 18 | |
| 19 | logging.getLogger("openai").setLevel(logging.DEBUG) |
| 20 | |
| 21 | |
| 22 | # automatically add `pytest.mark.asyncio()` to all of our async tests |
| 23 | # so we don't have to add that boilerplate everywhere |
| 24 | def pytest_collection_modifyitems(items: list[pytest.Function]) -> None: |
| 25 | pytest_asyncio_tests = (item for item in items if is_async_test(item)) |
| 26 | session_scope_marker = pytest.mark.asyncio(loop_scope="session") |
| 27 | for async_test in pytest_asyncio_tests: |
| 28 | async_test.add_marker(session_scope_marker, append=False) |
| 29 | |
| 30 | |
| 31 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 32 | |
| 33 | api_key = "My API Key" |
| 34 | |
| 35 | |
| 36 | @pytest.fixture(scope="session") |
| 37 | def client(request: FixtureRequest) -> Iterator[OpenAI]: |
| 38 | strict = getattr(request, "param", True) |
| 39 | if not isinstance(strict, bool): |
| 40 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") |
| 41 | |
| 42 | with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client: |
| 43 | yield client |
| 44 | |
| 45 | |
| 46 | @pytest.fixture(scope="session") |
| 47 | async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]: |
| 48 | strict = getattr(request, "param", True) |
| 49 | if not isinstance(strict, bool): |
| 50 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") |
| 51 | |
| 52 | async with AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client: |
| 53 | yield client |
| 54 | |