openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.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

baa9f07fRobert Craigie2 years ago1# File generated from our OpenAPI spec by Stainless.
2
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 ago15from openai._client import OpenAI, AsyncOpenAI
16
86379b44Stainless Bot2 years ago17# pyright: reportDeprecated=false
18
baa9f07fRobert Craigie2 years ago19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
20api_key = "My API Key"
21
22
23class TestSpeech:
24strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
25loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
26parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
27
28@parametrize
29@pytest.mark.respx(base_url=base_url)
30def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
31respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
32speech = client.audio.speech.create(
33input="string",
34model="string",
35voice="alloy",
36)
86379b44Stainless Bot2 years ago37assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago38assert speech.json() == {"foo": "bar"}
39
40@parametrize
41@pytest.mark.respx(base_url=base_url)
42def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None:
43respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
b56cf723Stainless Bot2 years ago44speech = client.audio.speech.create(
baa9f07fRobert Craigie2 years ago45input="string",
46model="string",
47voice="alloy",
48response_format="mp3",
49speed=0.25,
50)
86379b44Stainless Bot2 years ago51assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago52assert speech.json() == {"foo": "bar"}
53
54@parametrize
55@pytest.mark.respx(base_url=base_url)
56def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
57respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago58
baa9f07fRobert Craigie2 years ago59response = client.audio.speech.with_raw_response.create(
60input="string",
61model="string",
62voice="alloy",
63)
86379b44Stainless Bot2 years ago64
65assert response.is_closed is True
baa9f07fRobert Craigie2 years ago66assert response.http_request.headers.get("X-Stainless-Lang") == "python"
67speech = response.parse()
86379b44Stainless Bot2 years ago68assert_matches_type(_legacy_response.HttpxBinaryResponseContent, speech, path=["response"])
69
70@parametrize
71@pytest.mark.respx(base_url=base_url)
72def test_streaming_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
73respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
74with client.audio.speech.with_streaming_response.create(
75input="string",
76model="string",
77voice="alloy",
78) as response:
79assert not response.is_closed
80assert response.http_request.headers.get("X-Stainless-Lang") == "python"
81
82speech = response.parse()
83assert_matches_type(bytes, speech, path=["response"])
84
85assert cast(Any, response.is_closed) is True
baa9f07fRobert Craigie2 years ago86
87
88class TestAsyncSpeech:
89strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
90loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
91parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
92
93@parametrize
94@pytest.mark.respx(base_url=base_url)
95async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
96respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
97speech = await client.audio.speech.create(
98input="string",
99model="string",
100voice="alloy",
101)
86379b44Stainless Bot2 years ago102assert isinstance(speech, _legacy_response.HttpxBinaryResponseContent)
baa9f07fRobert Craigie2 years ago103assert speech.json() == {"foo": "bar"}
104
105@parametrize
106@pytest.mark.respx(base_url=base_url)
107async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
108respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
b56cf723Stainless Bot2 years ago109speech = await client.audio.speech.create(
baa9f07fRobert Craigie2 years ago110input="string",
111model="string",
112voice="alloy",
113response_format="mp3",
114speed=0.25,
115)
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)
121async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
122respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago123
baa9f07fRobert Craigie2 years ago124response = await client.audio.speech.with_raw_response.create(
125input="string",
126model="string",
127voice="alloy",
128)
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)
137async def test_streaming_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
138respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
139async with client.audio.speech.with_streaming_response.create(
140input="string",
141model="string",
142voice="alloy",
143) 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