openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/conftest.py
49lines · modecode
| 1 | from __future__ import annotations |
| 2 | |
| 3 | import os |
| 4 | import asyncio |
| 5 | import logging |
| 6 | from typing import TYPE_CHECKING, Iterator, AsyncIterator |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | |
| 12 | if TYPE_CHECKING: |
| 13 | from _pytest.fixtures import FixtureRequest |
| 14 | |
| 15 | pytest.register_assert_rewrite("tests.utils") |
| 16 | |
| 17 | logging.getLogger("openai").setLevel(logging.DEBUG) |
| 18 | |
| 19 | |
| 20 | @pytest.fixture(scope="session") |
| 21 | def event_loop() -> Iterator[asyncio.AbstractEventLoop]: |
| 22 | loop = asyncio.new_event_loop() |
| 23 | yield loop |
| 24 | loop.close() |
| 25 | |
| 26 | |
| 27 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 28 | |
| 29 | api_key = "My API Key" |
| 30 | |
| 31 | |
| 32 | @pytest.fixture(scope="session") |
| 33 | def client(request: FixtureRequest) -> Iterator[OpenAI]: |
| 34 | strict = getattr(request, "param", True) |
| 35 | if not isinstance(strict, bool): |
| 36 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") |
| 37 | |
| 38 | with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client: |
| 39 | yield client |
| 40 | |
| 41 | |
| 42 | @pytest.fixture(scope="session") |
| 43 | async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]: |
| 44 | strict = getattr(request, "param", True) |
| 45 | if not isinstance(strict, bool): |
| 46 | raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}") |
| 47 | |
| 48 | async with AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client: |
| 49 | yield client |
| 50 | |