openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.90.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

148lines · 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:
c62e9907stainless-app[bot]1 years ago86parametrize = pytest.mark.parametrize(
87"async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
88)
baa9f07fRobert Craigie2 years ago89
90@parametrize
91@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago92async def test_method_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago93respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago94speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago95input="string",
156d13e1Stainless Bot2 years ago96model="string",
2e73b529stainless-app[bot]1 years ago97voice="ash",
baa9f07fRobert Craigie2 years ago98)
86379b44Stainless Bot2 years ago99assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago100assert speech.json() == {"foo": "bar"}
101
102@parametrize
103@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago104async def test_method_create_with_all_params(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago105respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago106speech = await async_client.audio.speech.create(
baa9f07fRobert Craigie2 years ago107input="string",
156d13e1Stainless Bot2 years ago108model="string",
2e73b529stainless-app[bot]1 years ago109voice="ash",
2b4bc759stainless-app[bot]1 years ago110instructions="instructions",
baa9f07fRobert Craigie2 years ago111response_format="mp3",
112speed=0.25,
113)
86379b44Stainless Bot2 years ago114assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago115assert speech.json() == {"foo": "bar"}
116
117@parametrize
118@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago119async def test_raw_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
baa9f07fRobert Craigie2 years ago120respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago121
98d779fbStainless Bot2 years ago122response = await async_client.audio.speech.with_raw_response.create(
baa9f07fRobert Craigie2 years ago123input="string",
156d13e1Stainless Bot2 years ago124model="string",
2e73b529stainless-app[bot]1 years ago125voice="ash",
baa9f07fRobert Craigie2 years ago126)
86379b44Stainless Bot2 years ago127
128assert response.is_closed is True
baa9f07fRobert Craigie2 years ago129assert response.http_request.headers.get("X-Stainless-Lang") == "python"
130speech = response.parse()
86379b44Stainless Bot2 years ago131assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
132
133@parametrize
134@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago135async def test_streaming_response_create(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
86379b44Stainless Bot2 years ago136respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago137async with async_client.audio.speech.with_streaming_response.create(
86379b44Stainless Bot2 years ago138input="string",
156d13e1Stainless Bot2 years ago139model="string",
2e73b529stainless-app[bot]1 years ago140voice="ash",
86379b44Stainless Bot2 years ago141) as response:
142assert not response.is_closed
143assert response.http_request.headers.get("X-Stainless-Lang") == "python"
144
145speech = await response.parse()
146assert_matches_type(bytes, speech, path=["response"])
147
148assert cast(Any, response.is_closed) is True