openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_speech.py
150lines · modeblame
baa9f07fRobert Craigie2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. |
| 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 | from openai._client import OpenAI, AsyncOpenAI |
| 16 | | |
86379b44Stainless Bot2 years ago | 17 | # pyright: reportDeprecated=false |
| 18 | | |
baa9f07fRobert Craigie2 years ago | 19 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 20 | api_key = "My API Key" | |
| 21 | | |
| 22 | | |
| 23 | class 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 | ) | |
86379b44Stainless Bot2 years ago | 37 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
b56cf723Stainless Bot2 years ago | 44 | speech = client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 45 | input="string", |
| 46 | model="string", | |
| 47 | voice="alloy", | |
| 48 | response_format="mp3", | |
| 49 | speed=0.25, | |
| 50 | ) | |
86379b44Stainless Bot2 years ago | 51 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
86379b44Stainless Bot2 years ago | 58 | |
baa9f07fRobert Craigie2 years ago | 59 | response = client.audio.speech.with_raw_response.create( |
| 60 | input="string", | |
| 61 | model="string", | |
| 62 | voice="alloy", | |
| 63 | ) | |
86379b44Stainless Bot2 years ago | 64 | |
| 65 | assert response.is_closed is True | |
baa9f07fRobert Craigie2 years ago | 66 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 67 | speech = response.parse() | |
86379b44Stainless Bot2 years ago | 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 | |
baa9f07fRobert Craigie2 years ago | 86 | |
| 87 | | |
| 88 | class 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 | ) | |
86379b44Stainless Bot2 years ago | 102 | assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent) |
baa9f07fRobert Craigie2 years ago | 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"})) | |
b56cf723Stainless Bot2 years ago | 109 | speech = await client.audio.speech.create( |
baa9f07fRobert Craigie2 years ago | 110 | input="string", |
| 111 | model="string", | |
| 112 | voice="alloy", | |
| 113 | response_format="mp3", | |
| 114 | speed=0.25, | |
| 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) | |
| 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"})) | |
86379b44Stainless Bot2 years ago | 123 | |
baa9f07fRobert Craigie2 years ago | 124 | response = await client.audio.speech.with_raw_response.create( |
| 125 | input="string", | |
| 126 | model="string", | |
| 127 | voice="alloy", | |
| 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) | |
| 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 |