openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_speech.py
116lines · 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 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 25 | @parametrize |
| 26 | @pytest.mark.respx(base_url=base_url) |
| 27 | def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 28 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 29 | speech = client.audio.speech.create( |
| 30 | input="string", |
| 31 | model="string", |
| 32 | voice="alloy", |
| 33 | ) |
| 34 | assert isinstance(speech, BinaryResponseContent) |
| 35 | assert speech.json() == {"foo": "bar"} |
| 36 | |
| 37 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 38 | @parametrize |
| 39 | @pytest.mark.respx(base_url=base_url) |
| 40 | def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None: |
| 41 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 42 | speech = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 43 | client.audio.speech.create( |
| 44 | input="string", |
| 45 | model="string", |
| 46 | voice="alloy", |
| 47 | response_format="mp3", |
| 48 | speed=0.25, |
| 49 | ) |
| 50 | assert isinstance(speech, BinaryResponseContent) |
| 51 | assert speech.json() == {"foo": "bar"} |
| 52 | |
| 53 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 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 | response = client.audio.speech.with_raw_response.create( |
| 59 | input="string", |
| 60 | model="string", |
| 61 | voice="alloy", |
| 62 | ) |
| 63 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 64 | speech = response.parse() |
| 65 | assert isinstance(speech, BinaryResponseContent) |
| 66 | assert speech.json() == {"foo": "bar"} |
| 67 | |
| 68 | |
| 69 | class TestAsyncSpeech: |
| 70 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 71 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 72 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 73 | |
| 74 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 75 | @parametrize |
| 76 | @pytest.mark.respx(base_url=base_url) |
| 77 | async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 78 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 79 | speech = await client.audio.speech.create( |
| 80 | input="string", |
| 81 | model="string", |
| 82 | voice="alloy", |
| 83 | ) |
| 84 | assert isinstance(speech, BinaryResponseContent) |
| 85 | assert speech.json() == {"foo": "bar"} |
| 86 | |
| 87 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 88 | @parametrize |
| 89 | @pytest.mark.respx(base_url=base_url) |
| 90 | async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 91 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 92 | speech = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 93 | await client.audio.speech.create( |
| 94 | input="string", |
| 95 | model="string", |
| 96 | voice="alloy", |
| 97 | response_format="mp3", |
| 98 | speed=0.25, |
| 99 | ) |
| 100 | assert isinstance(speech, BinaryResponseContent) |
| 101 | assert speech.json() == {"foo": "bar"} |
| 102 | |
| 103 | @pytest.mark.skip(reason="Mocked tests are currently broken") |
| 104 | @parametrize |
| 105 | @pytest.mark.respx(base_url=base_url) |
| 106 | async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None: |
| 107 | respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"})) |
| 108 | response = await client.audio.speech.with_raw_response.create( |
| 109 | input="string", |
| 110 | model="string", |
| 111 | voice="alloy", |
| 112 | ) |
| 113 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 114 | speech = response.parse() |
| 115 | assert isinstance(speech, BinaryResponseContent) |
| 116 | assert speech.json() == {"foo": "bar"} |
| 117 | |