openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.2.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

110lines · modeblame

baa9f07fRobert Craigie2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
6
7import httpx
8import pytest
9from respx import MockRouter
10
11from openai import OpenAI, AsyncOpenAI
12from openai._types import BinaryResponseContent
13from openai._client import OpenAI, AsyncOpenAI
14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16api_key = "My API Key"
17
18
19class TestSpeech:
20strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
23
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",
30model="string",
31voice="alloy",
32)
33assert isinstance(speech, BinaryResponseContent)
34assert 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"}))
40speech = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
41client.audio.speech.create(
42input="string",
43model="string",
44voice="alloy",
45response_format="mp3",
46speed=0.25,
47)
48assert isinstance(speech, BinaryResponseContent)
49assert 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"}))
55response = client.audio.speech.with_raw_response.create(
56input="string",
57model="string",
58voice="alloy",
59)
60assert response.http_request.headers.get("X-Stainless-Lang") == "python"
61speech = response.parse()
62assert isinstance(speech, BinaryResponseContent)
63assert speech.json() == {"foo": "bar"}
64
65
66class TestAsyncSpeech:
67strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
68loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
69parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
70
71@parametrize
72@pytest.mark.respx(base_url=base_url)
73async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
74respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
75speech = await client.audio.speech.create(
76input="string",
77model="string",
78voice="alloy",
79)
80assert isinstance(speech, BinaryResponseContent)
81assert speech.json() == {"foo": "bar"}
82
83@parametrize
84@pytest.mark.respx(base_url=base_url)
85async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
86respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
87speech = respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
88await client.audio.speech.create(
89input="string",
90model="string",
91voice="alloy",
92response_format="mp3",
93speed=0.25,
94)
95assert isinstance(speech, BinaryResponseContent)
96assert speech.json() == {"foo": "bar"}
97
98@parametrize
99@pytest.mark.respx(base_url=base_url)
100async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
101respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
102response = await client.audio.speech.with_raw_response.create(
103input="string",
104model="string",
105voice="alloy",
106)
107assert response.http_request.headers.get("X-Stainless-Lang") == "python"
108speech = response.parse()
109assert isinstance(speech, BinaryResponseContent)
110assert speech.json() == {"foo": "bar"}