openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v2.41.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/conftest.py

91lines · modeblame

cc2c1fc1stainless-app[bot]1 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
98d779fbStainless Bot2 years ago3from __future__ import annotations
4
5import os
08b8179aDavid Schnurr2 years ago6import logging
98d779fbStainless Bot2 years ago7from typing import TYPE_CHECKING, Iterator, AsyncIterator
08b8179aDavid Schnurr2 years ago8
c62e9907stainless-app[bot]1 years ago9import httpx
08b8179aDavid Schnurr2 years ago10import pytest
44ac1cefStainless Bot1 years ago11from pytest_asyncio import is_async_test
08b8179aDavid Schnurr2 years ago12
c62e9907stainless-app[bot]1 years ago13from openai import OpenAI, AsyncOpenAI, DefaultAioHttpClient
14from openai._utils import is_dict
98d779fbStainless Bot2 years ago15
16if TYPE_CHECKING:
6d110a14stainless-app[bot]1 years ago17from _pytest.fixtures import FixtureRequest # pyright: ignore[reportPrivateImportUsage]
98d779fbStainless Bot2 years ago18
08b8179aDavid Schnurr2 years ago19pytest.register_assert_rewrite("tests.utils")
20
21logging.getLogger("openai").setLevel(logging.DEBUG)
22
23
44ac1cefStainless Bot1 years ago24# automatically add `pytest.mark.asyncio()` to all of our async tests
25# so we don't have to add that boilerplate everywhere
26def pytest_collection_modifyitems(items: list[pytest.Function]) -> None:
27pytest_asyncio_tests = (item for item in items if is_async_test(item))
28session_scope_marker = pytest.mark.asyncio(loop_scope="session")
29for async_test in pytest_asyncio_tests:
30async_test.add_marker(session_scope_marker, append=False)
98d779fbStainless Bot2 years ago31
c62e9907stainless-app[bot]1 years ago32# We skip tests that use both the aiohttp client and respx_mock as respx_mock
33# doesn't support custom transports.
34for item in items:
35if "async_client" not in item.fixturenames or "respx_mock" not in item.fixturenames:
36continue
37
38if not hasattr(item, "callspec"):
39continue
40
41async_client_param = item.callspec.params.get("async_client")
42if is_dict(async_client_param) and async_client_param.get("http_client") == "aiohttp":
43item.add_marker(pytest.mark.skip(reason="aiohttp client is not compatible with respx_mock"))
44
98d779fbStainless Bot2 years ago45
46base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
47
48api_key = "My API Key"
ae9ca46fstainless-app[bot]2 months ago49admin_api_key = "My Admin API Key"
98d779fbStainless Bot2 years ago50
51
52@pytest.fixture(scope="session")
53def client(request: FixtureRequest) -> Iterator[OpenAI]:
54strict = getattr(request, "param", True)
55if not isinstance(strict, bool):
56raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
57
ae9ca46fstainless-app[bot]2 months ago58with OpenAI(
59base_url=base_url, api_key=api_key, admin_api_key=admin_api_key, _strict_response_validation=strict
60) as client:
98d779fbStainless Bot2 years ago61yield client
62
63
64@pytest.fixture(scope="session")
65async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]:
c62e9907stainless-app[bot]1 years ago66param = getattr(request, "param", True)
67
68# defaults
69strict = True
70http_client: None | httpx.AsyncClient = None
71
72if isinstance(param, bool):
73strict = param
74elif is_dict(param):
75strict = param.get("strict", True)
76assert isinstance(strict, bool)
77
78http_client_type = param.get("http_client", "httpx")
79if http_client_type == "aiohttp":
80http_client = DefaultAioHttpClient()
81else:
82raise TypeError(f"Unexpected fixture parameter type {type(param)}, expected bool or dict")
83
84async with AsyncOpenAI(
ae9ca46fstainless-app[bot]2 months ago85base_url=base_url,
86api_key=api_key,
87admin_api_key=admin_api_key,
88_strict_response_validation=strict,
89http_client=http_client,
c62e9907stainless-app[bot]1 years ago90) as client:
98d779fbStainless Bot2 years ago91yield client