openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.76.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

146lines · 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("async_client", [False, True], indirect=True, ids=["loose", "strict"])
87
88 @parametrize
89 @pytest.mark.respx(base_url=base_url)
90 async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
91 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
92 speech = await async_client.audio.speech.create(
93 input="string",
94 model="string",
95 voice="ash",
96 )
97 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
98 assert speech.json() == {"foo": "bar"}
99
100 @parametrize
101 @pytest.mark.respx(base_url=base_url)
102 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
103 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
104 speech = await async_client.audio.speech.create(
105 input="string",
106 model="string",
107 voice="ash",
108 instructions="instructions",
109 response_format="mp3",
110 speed=0.25,
111 )
112 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
113 assert speech.json() == {"foo": "bar"}
114
115 @parametrize
116 @pytest.mark.respx(base_url=base_url)
117 async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
118 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
119
120 response = await async_client.audio.speech.with_raw_response.create(
121 input="string",
122 model="string",
123 voice="ash",
124 )
125
126 assert response.is_closed is True
127 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
128 speech = response.parse()
129 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
130
131 @parametrize
132 @pytest.mark.respx(base_url=base_url)
133 async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
134 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
135 async with async_client.audio.speech.with_streaming_response.create(
136 input="string",
137 model="string",
138 voice="ash",
139 ) as response:
140 assert not response.is_closed
141 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142
143 speech = await response.parse()
144 assert_matches_type(bytes, speech, path=["response"])
145
146 assert cast(Any, response.is_closed) is True
147