openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_speech.py
110lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import httpx |
| 8 | import pytest |
| 9 | from respx import MockRouter |
| 10 | |
| 11 | from openai import OpenAI, AsyncOpenAI |
| 12 | from openai._types import BinaryResponseContent |
| 13 | from openai._client import OpenAI, AsyncOpenAI |
| 14 | |
| 15 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 16 | api_key = "My API Key" |
| 17 | |
| 18 | |
| 19 | class TestSpeech: |
| 20 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 21 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 22 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 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, BinaryResponseContent) |
| 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 = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 41 | client.audio.speech.create( |
| 42 | input="string", |
| 43 | model="string", |
| 44 | voice="alloy", |
| 45 | response_format="mp3", |
| 46 | speed=0.25, |
| 47 | ) |
| 48 | assert isinstance(speech, BinaryResponseContent) |
| 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"})) |
| 55 | response = client.audio.speech.with_raw_response.create( |
| 56 | input="string", |
| 57 | model="string", |
| 58 | voice="alloy", |
| 59 | ) |
| 60 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 61 | speech = response.parse() |
| 62 | assert isinstance(speech, BinaryResponseContent) |
| 63 | assert speech.json() == {"foo": "bar"} |
| 64 | |
| 65 | |
| 66 | class TestAsyncSpeech: |
| 67 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 68 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 69 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 70 | |
| 71 | @parametrize |
| 72 | @pytest.mark.respx(base_url=base_url) |
| 73 | async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 74 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 75 | speech = await client.audio.speech.create( |
| 76 | input="string", |
| 77 | model="string", |
| 78 | voice="alloy", |
| 79 | ) |
| 80 | assert isinstance(speech, BinaryResponseContent) |
| 81 | assert speech.json() == {"foo": "bar"} |
| 82 | |
| 83 | @parametrize |
| 84 | @pytest.mark.respx(base_url=base_url) |
| 85 | async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 86 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 87 | speech = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 88 | await client.audio.speech.create( |
| 89 | input="string", |
| 90 | model="string", |
| 91 | voice="alloy", |
| 92 | response_format="mp3", |
| 93 | speed=0.25, |
| 94 | ) |
| 95 | assert isinstance(speech, BinaryResponseContent) |
| 96 | assert speech.json() == {"foo": "bar"} |
| 97 | |
| 98 | @parametrize |
| 99 | @pytest.mark.respx(base_url=base_url) |
| 100 | async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 101 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 102 | response = await client.audio.speech.with_raw_response.create( |
| 103 | input="string", |
| 104 | model="string", |
| 105 | voice="alloy", |
| 106 | ) |
| 107 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 108 | speech = response.parse() |
| 109 | assert isinstance(speech, BinaryResponseContent) |
| 110 | assert speech.json() == {"foo": "bar"} |
| 111 | |