openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_speech.py
150lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
baa9f07fRobert Craigie2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
baa9f07fRobert Craigie2 years ago | 7 | |
| 8 | import httpx | |
| 9 | import pytest | |
| 10 | from respx import MockRouter | |
| 11 | | |
86379b44Stainless Bot2 years ago | 12 | import openai._legacy_response as _legacy_response |
baa9f07fRobert Craigie2 years ago | 13 | from openai import OpenAI, AsyncOpenAI |
86379b44Stainless Bot2 years ago | 14 | from tests.utils import assert_matches_type |
baa9f07fRobert Craigie2 years ago | 15 | |
86379b44Stainless Bot2 years ago | 16 | # pyright: reportDeprecated=false |
| 17 | | |
baa9f07fRobert Craigie2 years ago | 18 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 19 | | |
| 20 | | |
| 21 | class TestSpeech: | |
98d779fbStainless Bot2 years ago | 22 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
baa9f07fRobert Craigie2 years ago | 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", | |
156d13e1Stainless Bot2 years ago | 30 | model="string", |
2e73b529stainless-app[bot]1 years ago | 31 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 32 | ) |
86379b44Stainless Bot2 years ago | 33 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
b56cf723Stainless Bot2 years ago | 40 | speech = client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 41 | input="string", |
156d13e1Stainless Bot2 years ago | 42 | model="string", |
2e73b529stainless-app[bot]1 years ago | 43 | voice="ash", |
2b4bc759stainless-app[bot]1 years ago | 44 | instructions="instructions", |
baa9f07fRobert Craigie2 years ago | 45 | response_format="mp3", |
| 46 | speed=0.25, | |
0673da62stainless-app[bot]1 years ago | 47 | stream_format="sse", |
baa9f07fRobert Craigie2 years ago | 48 | ) |
86379b44Stainless Bot2 years ago | 49 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
86379b44Stainless Bot2 years ago | 56 | |
baa9f07fRobert Craigie2 years ago | 57 | response = client.audio.speech.with_raw_response.create( |
| 58 | input="string", | |
156d13e1Stainless Bot2 years ago | 59 | model="string", |
2e73b529stainless-app[bot]1 years ago | 60 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 61 | ) |
86379b44Stainless Bot2 years ago | 62 | |
| 63 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 64 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 65 | speech = response.parse() | |
86379b44Stainless Bot2 years ago | 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", | |
156d13e1Stainless Bot2 years ago | 74 | model="string", |
2e73b529stainless-app[bot]1 years ago | 75 | voice="ash", |
86379b44Stainless Bot2 years ago | 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 | |
baa9f07fRobert Craigie2 years ago | 84 | |
| 85 | | |
| 86 | class TestAsyncSpeech: | |
c62e9907stainless-app[bot]1 years ago | 87 | parametrize = pytest.mark.parametrize( |
| 88 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] | |
| 89 | ) | |
baa9f07fRobert Craigie2 years ago | 90 | |
| 91 | @parametrize | |
| 92 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 93 | async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 94 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 95 | speech = await async_client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 96 | input="string", |
156d13e1Stainless Bot2 years ago | 97 | model="string", |
2e73b529stainless-app[bot]1 years ago | 98 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 99 | ) |
86379b44Stainless Bot2 years ago | 100 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 101 | assert speech.json() == {"foo": "bar"} |
| 102 | | |
| 103 | @parametrize | |
| 104 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 105 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 106 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 107 | speech = await async_client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 108 | input="string", |
156d13e1Stainless Bot2 years ago | 109 | model="string", |
2e73b529stainless-app[bot]1 years ago | 110 | voice="ash", |
2b4bc759stainless-app[bot]1 years ago | 111 | instructions="instructions", |
baa9f07fRobert Craigie2 years ago | 112 | response_format="mp3", |
| 113 | speed=0.25, | |
0673da62stainless-app[bot]1 years ago | 114 | stream_format="sse", |
baa9f07fRobert Craigie2 years ago | 115 | ) |
86379b44Stainless Bot2 years ago | 116 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 117 | assert speech.json() == {"foo": "bar"} |
| 118 | | |
| 119 | @parametrize | |
| 120 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 121 | async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 122 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
86379b44Stainless Bot2 years ago | 123 | |
98d779fbStainless Bot2 years ago | 124 | response = await async_client.audio.speech.with_raw_response.create( |
baa9f07fRobert Craigie2 years ago | 125 | input="string", |
156d13e1Stainless Bot2 years ago | 126 | model="string", |
2e73b529stainless-app[bot]1 years ago | 127 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 128 | ) |
86379b44Stainless Bot2 years ago | 129 | |
| 130 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 131 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 132 | speech = response.parse() | |
86379b44Stainless Bot2 years ago | 133 | assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"]) |
| 134 | | |
| 135 | @parametrize | |
| 136 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 137 | async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
86379b44Stainless Bot2 years ago | 138 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 139 | async with async_client.audio.speech.with_streaming_response.create( |
86379b44Stainless Bot2 years ago | 140 | input="string", |
156d13e1Stainless Bot2 years ago | 141 | model="string", |
2e73b529stainless-app[bot]1 years ago | 142 | voice="ash", |
86379b44Stainless Bot2 years ago | 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 |