openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.102.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

150lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
baa9f07fRobert Craigie2 years ago2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
baa9f07fRobert Craigie2 years ago7
8import httpx
9import pytest
10from respx import MockRouter
11
86379b44Stainless Bot2 years ago12import openai._legacy_response as _legacy_response
baa9f07fRobert Craigie2 years ago13from openai import OpenAI, AsyncOpenAI
86379b44Stainless Bot2 years ago14from tests.utils import assert_matches_type
baa9f07fRobert Craigie2 years ago15
86379b44Stainless Bot2 years ago16# pyright: reportDeprecated=false
17
baa9f07fRobert Craigie2 years ago18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestSpeech:
98d779fbStainless Bot2 years ago22parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago23
24@parametrize
25@pytest.mark.respx(base_url=base_url)
26def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
27respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
28speech = client.audio.speech.create(
29input="string",
156d13e1Stainless Bot2 years ago30model="string",
2e73b529stainless-app[bot]1 years ago31voice="ash",
baa9f07fRobert Craigie2 years ago32)
86379b44Stainless Bot2 years ago33assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago34assert speech.json() == {"foo": "bar"}
35
36@parametrize
37@pytest.mark.respx(base_url=base_url)
38def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None:
39respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
b56cf723Stainless Bot2 years ago40speech = client.audio.speech.create(
baa9f07fRobert Craigie2 years ago41input="string",
156d13e1Stainless Bot2 years ago42model="string",
2e73b529stainless-app[bot]1 years ago43voice="ash",
2b4bc759stainless-app[bot]1 years ago44instructions="instructions",
baa9f07fRobert Craigie2 years ago45response_format="mp3",
46speed=0.25,
0673da62stainless-app[bot]1 years ago47stream_format="sse",
baa9f07fRobert Craigie2 years ago48)
86379b44Stainless Bot2 years ago49assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago50assert speech.json() == {"foo": "bar"}
51
52@parametrize
53@pytest.mark.respx(base_url=base_url)
54def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
55respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago56
baa9f07fRobert Craigie2 years ago57response = client.audio.speech.with_raw_response.create(
58input="string",
156d13e1Stainless Bot2 years ago59model="string",
2e73b529stainless-app[bot]1 years ago60voice="ash",
baa9f07fRobert Craigie2 years ago61)
86379b44Stainless Bot2 years ago62
63assert response.is_closed is True
baa9f07fRobert Craigie2 years ago64assert response.http_request.headers.get("X-Stainless-Lang") == "python"
65speech = response.parse()
86379b44Stainless Bot2 years ago66assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
67
68@parametrize
69@pytest.mark.respx(base_url=base_url)
70def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
71respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
72with client.audio.speech.with_streaming_response.create(
73input="string",
156d13e1Stainless Bot2 years ago74model="string",
2e73b529stainless-app[bot]1 years ago75voice="ash",
86379b44Stainless Bot2 years ago76) as response:
77assert not response.is_closed
78assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79
80speech = response.parse()
81assert_matches_type(bytes, speech, path=["response"])
82
83assert cast(Any, response.is_closed) is True
baa9f07fRobert Craigie2 years ago84
85
86class TestAsyncSpeech:
c62e9907stainless-app[bot]1 years ago87parametrize = pytest.mark.parametrize(
88"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
89)
baa9f07fRobert Craigie2 years ago90
91@parametrize
92@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago93async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago94respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago95speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago96input="string",
156d13e1Stainless Bot2 years ago97model="string",
2e73b529stainless-app[bot]1 years ago98voice="ash",
baa9f07fRobert Craigie2 years ago99)
86379b44Stainless Bot2 years ago100assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago101assert speech.json() == {"foo": "bar"}
102
103@parametrize
104@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago105async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago106respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago107speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago108input="string",
156d13e1Stainless Bot2 years ago109model="string",
2e73b529stainless-app[bot]1 years ago110voice="ash",
2b4bc759stainless-app[bot]1 years ago111instructions="instructions",
baa9f07fRobert Craigie2 years ago112response_format="mp3",
113speed=0.25,
0673da62stainless-app[bot]1 years ago114stream_format="sse",
baa9f07fRobert Craigie2 years ago115)
86379b44Stainless Bot2 years ago116assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago117assert speech.json() == {"foo": "bar"}
118
119@parametrize
120@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago121async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago122respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago123
98d779fbStainless Bot2 years ago124response = await async_client.audio.speech.with_raw_response.create(
baa9f07fRobert Craigie2 years ago125input="string",
156d13e1Stainless Bot2 years ago126model="string",
2e73b529stainless-app[bot]1 years ago127voice="ash",
baa9f07fRobert Craigie2 years ago128)
86379b44Stainless Bot2 years ago129
130assert response.is_closed is True
baa9f07fRobert Craigie2 years ago131assert response.http_request.headers.get("X-Stainless-Lang") == "python"
132speech = response.parse()
86379b44Stainless Bot2 years ago133assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
134
135@parametrize
136@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago137async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
86379b44Stainless Bot2 years ago138respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago139async with async_client.audio.speech.with_streaming_response.create(
86379b44Stainless Bot2 years ago140input="string",
156d13e1Stainless Bot2 years ago141model="string",
2e73b529stainless-app[bot]1 years ago142voice="ash",
86379b44Stainless Bot2 years ago143) as response:
144assert not response.is_closed
145assert response.http_request.headers.get("X-Stainless-Lang") == "python"
146
147speech = await response.parse()
148assert_matches_type(bytes, speech, path=["response"])
149
150assert cast(Any, response.is_closed) is True