openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_speech.py
146lines · 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, | |
| 47 | ) | |
86379b44Stainless Bot2 years ago | 48 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
86379b44Stainless Bot2 years ago | 55 | |
baa9f07fRobert Craigie2 years ago | 56 | response = client.audio.speech.with_raw_response.create( |
| 57 | input="string", | |
156d13e1Stainless Bot2 years ago | 58 | model="string", |
2e73b529stainless-app[bot]1 years ago | 59 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 60 | ) |
86379b44Stainless Bot2 years ago | 61 | |
| 62 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 63 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 64 | speech = response.parse() | |
86379b44Stainless Bot2 years ago | 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", | |
156d13e1Stainless Bot2 years ago | 73 | model="string", |
2e73b529stainless-app[bot]1 years ago | 74 | voice="ash", |
86379b44Stainless Bot2 years ago | 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 | |
baa9f07fRobert Craigie2 years ago | 83 | |
| 84 | | |
| 85 | class TestAsyncSpeech: | |
98d779fbStainless Bot2 years ago | 86 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
baa9f07fRobert Craigie2 years ago | 87 | |
| 88 | @parametrize | |
| 89 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 90 | async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 91 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 92 | speech = await async_client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 93 | input="string", |
156d13e1Stainless Bot2 years ago | 94 | model="string", |
2e73b529stainless-app[bot]1 years ago | 95 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 96 | ) |
86379b44Stainless Bot2 years ago | 97 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 98 | assert speech.json() == {"foo": "bar"} |
| 99 | | |
| 100 | @parametrize | |
| 101 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 102 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 103 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 104 | speech = await async_client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 105 | input="string", |
156d13e1Stainless Bot2 years ago | 106 | model="string", |
2e73b529stainless-app[bot]1 years ago | 107 | voice="ash", |
2b4bc759stainless-app[bot]1 years ago | 108 | instructions="instructions", |
baa9f07fRobert Craigie2 years ago | 109 | response_format="mp3", |
| 110 | speed=0.25, | |
| 111 | ) | |
86379b44Stainless Bot2 years ago | 112 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 113 | assert speech.json() == {"foo": "bar"} |
| 114 | | |
| 115 | @parametrize | |
| 116 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 117 | async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
baa9f07fRobert Craigie2 years ago | 118 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
86379b44Stainless Bot2 years ago | 119 | |
98d779fbStainless Bot2 years ago | 120 | response = await async_client.audio.speech.with_raw_response.create( |
baa9f07fRobert Craigie2 years ago | 121 | input="string", |
156d13e1Stainless Bot2 years ago | 122 | model="string", |
2e73b529stainless-app[bot]1 years ago | 123 | voice="ash", |
baa9f07fRobert Craigie2 years ago | 124 | ) |
86379b44Stainless Bot2 years ago | 125 | |
| 126 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 127 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 128 | speech = response.parse() | |
86379b44Stainless Bot2 years ago | 129 | assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"]) |
| 130 | | |
| 131 | @parametrize | |
| 132 | @pytest.mark.respx(base_url=base_url) | |
98d779fbStainless Bot2 years ago | 133 | async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
86379b44Stainless Bot2 years ago | 134 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
98d779fbStainless Bot2 years ago | 135 | async with async_client.audio.speech.with_streaming_response.create( |
86379b44Stainless Bot2 years ago | 136 | input="string", |
156d13e1Stainless Bot2 years ago | 137 | model="string", |
2e73b529stainless-app[bot]1 years ago | 138 | voice="ash", |
86379b44Stainless Bot2 years ago | 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 |