Skip to content

Commit 05cd753

Browse files
chore(internal): share client instances between all tests (#1088)
1 parent d307127 commit 05cd753

19 files changed

+536
-611
lines changed

Diff for: tests/api_resources/audio/test_speech.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
import openai._legacy_response as _legacy_response
1313
from openai import OpenAI, AsyncOpenAI
1414
from tests.utils import assert_matches_type
15-
from openai._client import OpenAI, AsyncOpenAI
1615

1716
# pyright: reportDeprecated=false
1817

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

2220

2321
class TestSpeech:
24-
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
25-
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
26-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
22+
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
2723

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

8783

8884
class TestAsyncSpeech:
89-
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
90-
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
91-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
85+
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
9286

9387
@parametrize
9488
@pytest.mark.respx(base_url=base_url)
95-
async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
89+
async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
9690
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
97-
speech = await client.audio.speech.create(
91+
speech = await async_client.audio.speech.create(
9892
input="string",
9993
model="string",
10094
voice="alloy",
@@ -104,9 +98,9 @@ async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter)
10498

10599
@parametrize
106100
@pytest.mark.respx(base_url=base_url)
107-
async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
101+
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
108102
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
109-
speech = await client.audio.speech.create(
103+
speech = await async_client.audio.speech.create(
110104
input="string",
111105
model="string",
112106
voice="alloy",
@@ -118,10 +112,10 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mo
118112

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

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

135129
@parametrize
136130
@pytest.mark.respx(base_url=base_url)
137-
async def test_streaming_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
131+
async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
138132
respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
139-
async with client.audio.speech.with_streaming_response.create(
133+
async with async_client.audio.speech.with_streaming_response.create(
140134
input="string",
141135
model="string",
142136
voice="alloy",

Diff for: tests/api_resources/audio/test_transcriptions.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99

1010
from openai import OpenAI, AsyncOpenAI
1111
from tests.utils import assert_matches_type
12-
from openai._client import OpenAI, AsyncOpenAI
1312
from openai.types.audio import Transcription
1413

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

1816

1917
class TestTranscriptions:
20-
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21-
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
18+
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
2319

2420
@parametrize
2521
def test_method_create(self, client: OpenAI) -> None:
@@ -69,21 +65,19 @@ def test_streaming_response_create(self, client: OpenAI) -> None:
6965

7066

7167
class TestAsyncTranscriptions:
72-
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
73-
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
74-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
68+
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
7569

7670
@parametrize
77-
async def test_method_create(self, client: AsyncOpenAI) -> None:
78-
transcription = await client.audio.transcriptions.create(
71+
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
72+
transcription = await async_client.audio.transcriptions.create(
7973
file=b"raw file contents",
8074
model="whisper-1",
8175
)
8276
assert_matches_type(Transcription, transcription, path=["response"])
8377

8478
@parametrize
85-
async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
86-
transcription = await client.audio.transcriptions.create(
79+
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
80+
transcription = await async_client.audio.transcriptions.create(
8781
file=b"raw file contents",
8882
model="whisper-1",
8983
language="string",
@@ -94,8 +88,8 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
9488
assert_matches_type(Transcription, transcription, path=["response"])
9589

9690
@parametrize
97-
async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
98-
response = await client.audio.transcriptions.with_raw_response.create(
91+
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
92+
response = await async_client.audio.transcriptions.with_raw_response.create(
9993
file=b"raw file contents",
10094
model="whisper-1",
10195
)
@@ -106,8 +100,8 @@ async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
106100
assert_matches_type(Transcription, transcription, path=["response"])
107101

108102
@parametrize
109-
async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
110-
async with client.audio.transcriptions.with_streaming_response.create(
103+
async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
104+
async with async_client.audio.transcriptions.with_streaming_response.create(
111105
file=b"raw file contents",
112106
model="whisper-1",
113107
) as response:

Diff for: tests/api_resources/audio/test_translations.py

+10-16
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99

1010
from openai import OpenAI, AsyncOpenAI
1111
from tests.utils import assert_matches_type
12-
from openai._client import OpenAI, AsyncOpenAI
1312
from openai.types.audio import Translation
1413

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

1816

1917
class TestTranslations:
20-
strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21-
loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
18+
parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
2319

2420
@parametrize
2521
def test_method_create(self, client: OpenAI) -> None:
@@ -68,21 +64,19 @@ def test_streaming_response_create(self, client: OpenAI) -> None:
6864

6965

7066
class TestAsyncTranslations:
71-
strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
72-
loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
73-
parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
67+
parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
7468

7569
@parametrize
76-
async def test_method_create(self, client: AsyncOpenAI) -> None:
77-
translation = await client.audio.translations.create(
70+
async def test_method_create(self, async_client: AsyncOpenAI) -> None:
71+
translation = await async_client.audio.translations.create(
7872
file=b"raw file contents",
7973
model="whisper-1",
8074
)
8175
assert_matches_type(Translation, translation, path=["response"])
8276

8377
@parametrize
84-
async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
85-
translation = await client.audio.translations.create(
78+
async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
79+
translation = await async_client.audio.translations.create(
8680
file=b"raw file contents",
8781
model="whisper-1",
8882
prompt="string",
@@ -92,8 +86,8 @@ async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
9286
assert_matches_type(Translation, translation, path=["response"])
9387

9488
@parametrize
95-
async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
96-
response = await client.audio.translations.with_raw_response.create(
89+
async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
90+
response = await async_client.audio.translations.with_raw_response.create(
9791
file=b"raw file contents",
9892
model="whisper-1",
9993
)
@@ -104,8 +98,8 @@ async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
10498
assert_matches_type(Translation, translation, path=["response"])
10599

106100
@parametrize
107-
async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
108-
async with client.audio.translations.with_streaming_response.create(
101+
async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
102+
async with async_client.audio.translations.with_streaming_response.create(
109103
file=b"raw file contents",
110104
model="whisper-1",
111105
) as response:

0 commit comments

Comments
 (0)