Skip to content

Commit

Permalink
chore(internal): share client instances between all tests (openai#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 21, 2024
1 parent c484e0c commit 98d779f
Show file tree
Hide file tree
Showing 19 changed files with 536 additions and 611 deletions.
26 changes: 10 additions & 16 deletions tests/api_resources/audio/test_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@
import openai._legacy_response as _legacy_response
from openai import OpenAI, AsyncOpenAI
from tests.utils import assert_matches_type
from openai._client import OpenAI, AsyncOpenAI

# pyright: reportDeprecated=false

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
api_key = "My API Key"


class TestSpeech:
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
@pytest.mark.respx(base_url=base_url)
Expand Down Expand Up @@ -86,15 +82,13 @@ def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter)


class TestAsyncSpeech:
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
@pytest.mark.respx(base_url=base_url)
async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
speech = await client.audio.speech.create(
speech = await async_client.audio.speech.create(
input="string",
model="string",
voice="alloy",
Expand All @@ -104,9 +98,9 @@ async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter)

@parametrize
@pytest.mark.respx(base_url=base_url)
async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
speech = await client.audio.speech.create(
speech = await async_client.audio.speech.create(
input="string",
model="string",
voice="alloy",
Expand All @@ -118,10 +112,10 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mo

@parametrize
@pytest.mark.respx(base_url=base_url)
async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))

response = await client.audio.speech.with_raw_response.create(
response = await async_client.audio.speech.with_raw_response.create(
input="string",
model="string",
voice="alloy",
Expand All @@ -134,9 +128,9 @@ async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRo

@parametrize
@pytest.mark.respx(base_url=base_url)
async def test_streaming_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
async with client.audio.speech.with_streaming_response.create(
async with async_client.audio.speech.with_streaming_response.create(
input="string",
model="string",
voice="alloy",
Expand Down
26 changes: 10 additions & 16 deletions tests/api_resources/audio/test_transcriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@

from openai import OpenAI, AsyncOpenAI
from tests.utils import assert_matches_type
from openai._client import OpenAI, AsyncOpenAI
from openai.types.audio import Transcription

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
api_key = "My API Key"


class TestTranscriptions:
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
def test_method_create(self, client: OpenAI) -> None:
Expand Down Expand Up @@ -69,21 +65,19 @@ def test_streaming_response_create(self, client: OpenAI) -> None:


class TestAsyncTranscriptions:
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
async def test_method_create(self, client: AsyncOpenAI) -> None:
transcription = await client.audio.transcriptions.create(
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
transcription = await async_client.audio.transcriptions.create(
file=b"raw file contents",
model="whisper-1",
)
assert_matches_type(Transcription, transcription, path=["response"])

@parametrize
async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
transcription = await client.audio.transcriptions.create(
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
transcription = await async_client.audio.transcriptions.create(
file=b"raw file contents",
model="whisper-1",
language="string",
Expand All @@ -94,8 +88,8 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
assert_matches_type(Transcription, transcription, path=["response"])

@parametrize
async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
response = await client.audio.transcriptions.with_raw_response.create(
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
response = await async_client.audio.transcriptions.with_raw_response.create(
file=b"raw file contents",
model="whisper-1",
)
Expand All @@ -106,8 +100,8 @@ async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
assert_matches_type(Transcription, transcription, path=["response"])

@parametrize
async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
async with client.audio.transcriptions.with_streaming_response.create(
async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
async with async_client.audio.transcriptions.with_streaming_response.create(
file=b"raw file contents",
model="whisper-1",
) as response:
Expand Down
26 changes: 10 additions & 16 deletions tests/api_resources/audio/test_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@

from openai import OpenAI, AsyncOpenAI
from tests.utils import assert_matches_type
from openai._client import OpenAI, AsyncOpenAI
from openai.types.audio import Translation

base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
api_key = "My API Key"


class TestTranslations:
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
def test_method_create(self, client: OpenAI) -> None:
Expand Down Expand Up @@ -68,21 +64,19 @@ def test_streaming_response_create(self, client: OpenAI) -> None:


class TestAsyncTranslations:
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])

@parametrize
async def test_method_create(self, client: AsyncOpenAI) -> None:
translation = await client.audio.translations.create(
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
translation = await async_client.audio.translations.create(
file=b"raw file contents",
model="whisper-1",
)
assert_matches_type(Translation, translation, path=["response"])

@parametrize
async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
translation = await client.audio.translations.create(
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
translation = await async_client.audio.translations.create(
file=b"raw file contents",
model="whisper-1",
prompt="string",
Expand All @@ -92,8 +86,8 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
assert_matches_type(Translation, translation, path=["response"])

@parametrize
async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
response = await client.audio.translations.with_raw_response.create(
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
response = await async_client.audio.translations.with_raw_response.create(
file=b"raw file contents",
model="whisper-1",
)
Expand All @@ -104,8 +98,8 @@ async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
assert_matches_type(Translation, translation, path=["response"])

@parametrize
async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
async with client.audio.translations.with_streaming_response.create(
async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
async with async_client.audio.translations.with_streaming_response.create(
file=b"raw file contents",
model="whisper-1",
) as response:
Expand Down
Loading

0 comments on commit 98d779f

Please sign in to comment.