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