main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import httpx
9import pytest
10from respx import MockRouter
11
12import openai._legacy_response as _legacy_response
13from openai import OpenAI, AsyncOpenAI
14from tests.utils import assert_matches_type
15
16# pyright: reportDeprecated=false
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestSpeech:
22 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
23
24 @parametrize
25 @pytest.mark.respx(base_url=base_url)
26 def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
27 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
28 speech = client.audio.speech.create(
29 input="string",
30 model="string",
31 voice="ash",
32 )
33 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
34 assert speech.json() == {"foo": "bar"}
35
36 @parametrize
37 @pytest.mark.respx(base_url=base_url)
38 def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None:
39 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
40 speech = client.audio.speech.create(
41 input="string",
42 model="string",
43 voice="ash",
44 instructions="instructions",
45 response_format="mp3",
46 speed=0.25,
47 stream_format="sse",
48 )
49 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
50 assert speech.json() == {"foo": "bar"}
51
52 @parametrize
53 @pytest.mark.respx(base_url=base_url)
54 def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
55 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
56
57 response = client.audio.speech.with_raw_response.create(
58 input="string",
59 model="string",
60 voice="ash",
61 )
62
63 assert response.is_closed is True
64 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
65 speech = response.parse()
66 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
67
68 @parametrize
69 @pytest.mark.respx(base_url=base_url)
70 def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
71 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
72 with client.audio.speech.with_streaming_response.create(
73 input="string",
74 model="string",
75 voice="ash",
76 ) as response:
77 assert not response.is_closed
78 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79
80 speech = response.parse()
81 assert_matches_type(bytes, speech, path=["response"])
82
83 assert cast(Any, response.is_closed) is True
84
85
86class TestAsyncSpeech:
87 parametrize = pytest.mark.parametrize(
88 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
89 )
90
91 @parametrize
92 @pytest.mark.respx(base_url=base_url)
93 async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
94 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
95 speech = await async_client.audio.speech.create(
96 input="string",
97 model="string",
98 voice="ash",
99 )
100 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
101 assert speech.json() == {"foo": "bar"}
102
103 @parametrize
104 @pytest.mark.respx(base_url=base_url)
105 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
106 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
107 speech = await async_client.audio.speech.create(
108 input="string",
109 model="string",
110 voice="ash",
111 instructions="instructions",
112 response_format="mp3",
113 speed=0.25,
114 stream_format="sse",
115 )
116 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
117 assert speech.json() == {"foo": "bar"}
118
119 @parametrize
120 @pytest.mark.respx(base_url=base_url)
121 async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
122 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
123
124 response = await async_client.audio.speech.with_raw_response.create(
125 input="string",
126 model="string",
127 voice="ash",
128 )
129
130 assert response.is_closed is True
131 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132 speech = response.parse()
133 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
134
135 @parametrize
136 @pytest.mark.respx(base_url=base_url)
137 async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
138 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
139 async with async_client.audio.speech.with_streaming_response.create(
140 input="string",
141 model="string",
142 voice="ash",
143 ) as response:
144 assert not response.is_closed
145 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
146
147 speech = await response.parse()
148 assert_matches_type(bytes, speech, path=["response"])
149
150 assert cast(Any, response.is_closed) is True