openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.107.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/conftest.py

84lines · 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"
49
50
51@pytest.fixture(scope="session")
52def client(request: FixtureRequest) -> Iterator[OpenAI]:
53strict = getattr(request, "param", True)
54if not isinstance(strict, bool):
55raise TypeError(f"Unexpected fixture parameter type {type(strict)}, expected {bool}")
56
57with OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=strict) as client:
58yield client
59
60
61@pytest.fixture(scope="session")
62async def async_client(request: FixtureRequest) -> AsyncIterator[AsyncOpenAI]:
c62e9907stainless-app[bot]1 years ago63param = getattr(request, "param", True)
64
65# defaults
66strict = True
67http_client: None | httpx.AsyncClient = None
68
69if isinstance(param, bool):
70strict = param
71elif is_dict(param):
72strict = param.get("strict", True)
73assert isinstance(strict, bool)
74
75http_client_type = param.get("http_client", "httpx")
76if http_client_type == "aiohttp":
77http_client = DefaultAioHttpClient()
78else:
79raise TypeError(f"Unexpected fixture parameter type {type(param)}, expected bool or dict")
80
81async with AsyncOpenAI(
82base_url=base_url, api_key=api_key, _strict_response_validation=strict, http_client=http_client
83) as client:
98d779fbStainless Bot2 years ago84yield client