main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai.types.audio import TranslationCreateResponse
13
14base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
15
16
17class TestTranslations:
18 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
19
20 @parametrize
21 def test_method_create(self, client: OpenAI) -> None:
22 translation = client.audio.translations.create(
23 file=b"raw file contents",
24 model="whisper-1",
25 )
26 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
27
28 @parametrize
29 def test_method_create_with_all_params(self, client: OpenAI) -> None:
30 translation = client.audio.translations.create(
31 file=b"raw file contents",
32 model="whisper-1",
33 prompt="prompt",
34 response_format="json",
35 temperature=0,
36 )
37 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
38
39 @parametrize
40 def test_raw_response_create(self, client: OpenAI) -> None:
41 response = client.audio.translations.with_raw_response.create(
42 file=b"raw file contents",
43 model="whisper-1",
44 )
45
46 assert response.is_closed is True
47 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
48 translation = response.parse()
49 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
50
51 @parametrize
52 def test_streaming_response_create(self, client: OpenAI) -> None:
53 with client.audio.translations.with_streaming_response.create(
54 file=b"raw file contents",
55 model="whisper-1",
56 ) as response:
57 assert not response.is_closed
58 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
59
60 translation = response.parse()
61 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
62
63 assert cast(Any, response.is_closed) is True
64
65
66class TestAsyncTranslations:
67 parametrize = pytest.mark.parametrize(
68 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
69 )
70
71 @parametrize
72 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
73 translation = await async_client.audio.translations.create(
74 file=b"raw file contents",
75 model="whisper-1",
76 )
77 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
78
79 @parametrize
80 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
81 translation = await async_client.audio.translations.create(
82 file=b"raw file contents",
83 model="whisper-1",
84 prompt="prompt",
85 response_format="json",
86 temperature=0,
87 )
88 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
89
90 @parametrize
91 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
92 response = await async_client.audio.translations.with_raw_response.create(
93 file=b"raw file contents",
94 model="whisper-1",
95 )
96
97 assert response.is_closed is True
98 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
99 translation = response.parse()
100 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
101
102 @parametrize
103 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
104 async with async_client.audio.translations.with_streaming_response.create(
105 file=b"raw file contents",
106 model="whisper-1",
107 ) as response:
108 assert not response.is_closed
109 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
110
111 translation = await response.parse()
112 assert_matches_type(TranslationCreateResponse, translation, path=["response"])
113
114 assert cast(Any, response.is_closed) is True