openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.6.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/audio/test_speech.py

114lines · modecode

1# 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:
20 strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
21 loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
22 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
23
24 @pytest.mark.skip(reason="Mocked tests are currently broken")
25 @parametrize
26 @pytest.mark.respx(base_url=base_url)
27 def test_method_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
28 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
29 speech = client.audio.speech.create(
30 input="string",
31 model="string",
32 voice="alloy",
33 )
34 assert isinstance(speech, BinaryResponseContent)
35 assert speech.json() == {"foo": "bar"}
36
37 @pytest.mark.skip(reason="Mocked tests are currently broken")
38 @parametrize
39 @pytest.mark.respx(base_url=base_url)
40 def test_method_create_with_all_params(self, client: OpenAI, respx_mock: MockRouter) -> None:
41 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
42 speech = client.audio.speech.create(
43 input="string",
44 model="string",
45 voice="alloy",
46 response_format="mp3",
47 speed=0.25,
48 )
49 assert isinstance(speech, BinaryResponseContent)
50 assert speech.json() == {"foo": "bar"}
51
52 @pytest.mark.skip(reason="Mocked tests are currently broken")
53 @parametrize
54 @pytest.mark.respx(base_url=base_url)
55 def test_raw_response_create(self, client: OpenAI, respx_mock: MockRouter) -> None:
56 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
57 response = client.audio.speech.with_raw_response.create(
58 input="string",
59 model="string",
60 voice="alloy",
61 )
62 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
63 speech = response.parse()
64 assert isinstance(speech, BinaryResponseContent)
65 assert speech.json() == {"foo": "bar"}
66
67
68class TestAsyncSpeech:
69 strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
70 loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
71 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
72
73 @pytest.mark.skip(reason="Mocked tests are currently broken")
74 @parametrize
75 @pytest.mark.respx(base_url=base_url)
76 async def test_method_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
77 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
78 speech = await client.audio.speech.create(
79 input="string",
80 model="string",
81 voice="alloy",
82 )
83 assert isinstance(speech, BinaryResponseContent)
84 assert speech.json() == {"foo": "bar"}
85
86 @pytest.mark.skip(reason="Mocked tests are currently broken")
87 @parametrize
88 @pytest.mark.respx(base_url=base_url)
89 async def test_method_create_with_all_params(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
90 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
91 speech = await client.audio.speech.create(
92 input="string",
93 model="string",
94 voice="alloy",
95 response_format="mp3",
96 speed=0.25,
97 )
98 assert isinstance(speech, BinaryResponseContent)
99 assert speech.json() == {"foo": "bar"}
100
101 @pytest.mark.skip(reason="Mocked tests are currently broken")
102 @parametrize
103 @pytest.mark.respx(base_url=base_url)
104 async def test_raw_response_create(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
105 respx_mock.post("/audio/speech").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
106 response = await client.audio.speech.with_raw_response.create(
107 input="string",
108 model="string",
109 voice="alloy",
110 )
111 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
112 speech = response.parse()
113 assert isinstance(speech, BinaryResponseContent)
114 assert speech.json() == {"foo": "bar"}
115