openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.85.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

146lines · 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,
47)
86379b44Stainless Bot2 years ago48assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago49assert speech.json() == {"foo": "bar"}
50
51@parametrize
52@pytest.mark.respx(base_url=base_url)
53def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
54respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago55
baa9f07fRobert Craigie2 years ago56response = client.audio.speech.with_raw_response.create(
57input="string",
156d13e1Stainless Bot2 years ago58model="string",
2e73b529stainless-app[bot]1 years ago59voice="ash",
baa9f07fRobert Craigie2 years ago60)
86379b44Stainless Bot2 years ago61
62assert response.is_closed is True
baa9f07fRobert Craigie2 years ago63assert response.http_request.headers.get("X-Stainless-Lang") == "python"
64speech = response.parse()
86379b44Stainless Bot2 years ago65assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
66
67@parametrize
68@pytest.mark.respx(base_url=base_url)
69def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
70respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
71with client.audio.speech.with_streaming_response.create(
72input="string",
156d13e1Stainless Bot2 years ago73model="string",
2e73b529stainless-app[bot]1 years ago74voice="ash",
86379b44Stainless Bot2 years ago75) as response:
76assert not response.is_closed
77assert response.http_request.headers.get("X-Stainless-Lang") == "python"
78
79speech = response.parse()
80assert_matches_type(bytes, speech, path=["response"])
81
82assert cast(Any, response.is_closed) is True
baa9f07fRobert Craigie2 years ago83
84
85class TestAsyncSpeech:
98d779fbStainless Bot2 years ago86parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago87
88@parametrize
89@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago90async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago91respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago92speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago93input="string",
156d13e1Stainless Bot2 years ago94model="string",
2e73b529stainless-app[bot]1 years ago95voice="ash",
baa9f07fRobert Craigie2 years ago96)
86379b44Stainless Bot2 years ago97assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago98assert speech.json() == {"foo": "bar"}
99
100@parametrize
101@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago102async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago103respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago104speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago105input="string",
156d13e1Stainless Bot2 years ago106model="string",
2e73b529stainless-app[bot]1 years ago107voice="ash",
2b4bc759stainless-app[bot]1 years ago108instructions="instructions",
baa9f07fRobert Craigie2 years ago109response_format="mp3",
110speed=0.25,
111)
86379b44Stainless Bot2 years ago112assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago113assert speech.json() == {"foo": "bar"}
114
115@parametrize
116@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago117async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago118respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago119
98d779fbStainless Bot2 years ago120response = await async_client.audio.speech.with_raw_response.create(
baa9f07fRobert Craigie2 years ago121input="string",
156d13e1Stainless Bot2 years ago122model="string",
2e73b529stainless-app[bot]1 years ago123voice="ash",
baa9f07fRobert Craigie2 years ago124)
86379b44Stainless Bot2 years ago125
126assert response.is_closed is True
baa9f07fRobert Craigie2 years ago127assert response.http_request.headers.get("X-Stainless-Lang") == "python"
128speech = response.parse()
86379b44Stainless Bot2 years ago129assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
130
131@parametrize
132@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago133async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
86379b44Stainless Bot2 years ago134respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago135async with async_client.audio.speech.with_streaming_response.create(
86379b44Stainless Bot2 years ago136input="string",
156d13e1Stainless Bot2 years ago137model="string",
2e73b529stainless-app[bot]1 years ago138voice="ash",
86379b44Stainless Bot2 years ago139) as response:
140assert not response.is_closed
141assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142
143speech = await response.parse()
144assert_matches_type(bytes, speech, path=["response"])
145
146assert cast(Any, response.is_closed) is True