openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.90.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

148lines · modecode

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 )
48 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
49 assert speech.json() == {"foo": "bar"}
50
51 @parametrize
52 @pytest.mark.respx(base_url=base_url)
53 def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
54 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
55
56 response = client.audio.speech.with_raw_response.create(
57 input="string",
58 model="string",
59 voice="ash",
60 )
61
62 assert response.is_closed is True
63 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
64 speech = response.parse()
65 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
66
67 @parametrize
68 @pytest.mark.respx(base_url=base_url)
69 def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
70 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
71 with client.audio.speech.with_streaming_response.create(
72 input="string",
73 model="string",
74 voice="ash",
75 ) as response:
76 assert not response.is_closed
77 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
78
79 speech = response.parse()
80 assert_matches_type(bytes, speech, path=["response"])
81
82 assert cast(Any, response.is_closed) is True
83
84
85class TestAsyncSpeech:
86 parametrize = pytest.mark.parametrize(
87 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
88 )
89
90 @parametrize
91 @pytest.mark.respx(base_url=base_url)
92 async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
93 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
94 speech = await async_client.audio.speech.create(
95 input="string",
96 model="string",
97 voice="ash",
98 )
99 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
100 assert speech.json() == {"foo": "bar"}
101
102 @parametrize
103 @pytest.mark.respx(base_url=base_url)
104 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
105 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
106 speech = await async_client.audio.speech.create(
107 input="string",
108 model="string",
109 voice="ash",
110 instructions="instructions",
111 response_format="mp3",
112 speed=0.25,
113 )
114 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
115 assert speech.json() == {"foo": "bar"}
116
117 @parametrize
118 @pytest.mark.respx(base_url=base_url)
119 async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
120 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
121
122 response = await async_client.audio.speech.with_raw_response.create(
123 input="string",
124 model="string",
125 voice="ash",
126 )
127
128 assert response.is_closed is True
129 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
130 speech = response.parse()
131 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
132
133 @parametrize
134 @pytest.mark.respx(base_url=base_url)
135 async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
136 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
137 async with async_client.audio.speech.with_streaming_response.create(
138 input="string",
139 model="string",
140 voice="ash",
141 ) as response:
142 assert not response.is_closed
143 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
144
145 speech = await response.parse()
146 assert_matches_type(bytes, speech, path=["response"])
147
148 assert cast(Any, response.is_closed) is True