openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

150lines · modecode

1# File generated from our OpenAPI spec by Stainless.
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
15from openai._client import OpenAI, AsyncOpenAI
16
17# pyright: reportDeprecated=false
18
19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
20api_key = "My API Key"
21
22
23class 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"])
27
28 @parametrize
29 @pytest.mark.respx(base_url=base_url)
30 def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
31 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
32 speech = client.audio.speech.create(
33 input="string",
34 model="string",
35 voice="alloy",
36 )
37 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
38 assert speech.json() == {"foo": "bar"}
39
40 @parametrize
41 @pytest.mark.respx(base_url=base_url)
42 def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None:
43 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
44 speech = client.audio.speech.create(
45 input="string",
46 model="string",
47 voice="alloy",
48 response_format="mp3",
49 speed=0.25,
50 )
51 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
52 assert speech.json() == {"foo": "bar"}
53
54 @parametrize
55 @pytest.mark.respx(base_url=base_url)
56 def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
57 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
58
59 response = client.audio.speech.with_raw_response.create(
60 input="string",
61 model="string",
62 voice="alloy",
63 )
64
65 assert response.is_closed is True
66 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
67 speech = response.parse()
68 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
69
70 @parametrize
71 @pytest.mark.respx(base_url=base_url)
72 def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
73 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
74 with client.audio.speech.with_streaming_response.create(
75 input="string",
76 model="string",
77 voice="alloy",
78 ) as response:
79 assert not response.is_closed
80 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
81
82 speech = response.parse()
83 assert_matches_type(bytes, speech, path=["response"])
84
85 assert cast(Any, response.is_closed) is True
86
87
88class 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"])
92
93 @parametrize
94 @pytest.mark.respx(base_url=base_url)
95 async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
96 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
97 speech = await client.audio.speech.create(
98 input="string",
99 model="string",
100 voice="alloy",
101 )
102 assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
103 assert speech.json() == {"foo": "bar"}
104
105 @parametrize
106 @pytest.mark.respx(base_url=base_url)
107 async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
108 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
109 speech = await client.audio.speech.create(
110 input="string",
111 model="string",
112 voice="alloy",
113 response_format="mp3",
114 speed=0.25,
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, 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 client.audio.speech.with_raw_response.create(
125 input="string",
126 model="string",
127 voice="alloy",
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, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
138 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
139 async with client.audio.speech.with_streaming_response.create(
140 input="string",
141 model="string",
142 voice="alloy",
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
151