openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.20.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

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