openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_translations.py
114lines · modeblame
5cfb125aStainless Bot2 years ago | 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
08b8179aDavid Schnurr2 years ago | 2 | |
| 3 | from __future__ import annotations | |
| 4 | | |
| 5 | import os | |
86379b44Stainless Bot2 years ago | 6 | from typing import Any, cast |
08b8179aDavid Schnurr2 years ago | 7 | |
| 8 | import pytest | |
| 9 | | |
| 10 | from openai import OpenAI, AsyncOpenAI | |
| 11 | from tests.utils import assert_matches_type | |
1f3f4784stainless-app[bot]1 years ago | 12 | from openai.types.audio import TranslationCreateResponse |
08b8179aDavid Schnurr2 years ago | 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") | |
| 15 | | |
| 16 | | |
| 17 | class TestTranslations: | |
98d779fbStainless Bot2 years ago | 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
08b8179aDavid Schnurr2 years ago | 19 | |
| 20 | @parametrize | |
| 21 | def test_method_create(self, client: OpenAI) -> None: | |
| 22 | translation = client.audio.translations.create( | |
aa2634c2stainless-app[bot]3 months ago | 23 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 24 | model="whisper-1", |
| 25 | ) | |
1f3f4784stainless-app[bot]1 years ago | 26 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 27 | |
| 28 | @parametrize | |
| 29 | def test_method_create_with_all_params(self, client: OpenAI) -> None: | |
| 30 | translation = client.audio.translations.create( | |
aa2634c2stainless-app[bot]3 months ago | 31 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 32 | model="whisper-1", |
d1691a17stainless-app[bot]1 years ago | 33 | prompt="prompt", |
c118d98astainless-app[bot]1 years ago | 34 | response_format="json", |
08b8179aDavid Schnurr2 years ago | 35 | temperature=0, |
| 36 | ) | |
1f3f4784stainless-app[bot]1 years ago | 37 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 38 | |
| 39 | @parametrize | |
| 40 | def test_raw_response_create(self, client: OpenAI) -> None: | |
| 41 | response = client.audio.translations.with_raw_response.create( | |
aa2634c2stainless-app[bot]3 months ago | 42 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 43 | model="whisper-1", |
| 44 | ) | |
86379b44Stainless Bot2 years ago | 45 | |
| 46 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 47 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 48 | translation = response.parse() | |
1f3f4784stainless-app[bot]1 years ago | 49 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 50 | |
86379b44Stainless Bot2 years ago | 51 | @parametrize |
| 52 | def test_streaming_response_create(self, client: OpenAI) -> None: | |
| 53 | with client.audio.translations.with_streaming_response.create( | |
aa2634c2stainless-app[bot]3 months ago | 54 | file=b"Example data", |
86379b44Stainless Bot2 years ago | 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() | |
1f3f4784stainless-app[bot]1 years ago | 61 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
86379b44Stainless Bot2 years ago | 62 | |
| 63 | assert cast(Any, response.is_closed) is True | |
| 64 | | |
08b8179aDavid Schnurr2 years ago | 65 | |
| 66 | class TestAsyncTranslations: | |
d7700afdstainless-app[bot]1 years ago | 67 | parametrize = pytest.mark.parametrize( |
| 68 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] | |
| 69 | ) | |
08b8179aDavid Schnurr2 years ago | 70 | |
| 71 | @parametrize | |
98d779fbStainless Bot2 years ago | 72 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 73 | translation = await async_client.audio.translations.create( | |
aa2634c2stainless-app[bot]3 months ago | 74 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 75 | model="whisper-1", |
| 76 | ) | |
1f3f4784stainless-app[bot]1 years ago | 77 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 78 | |
| 79 | @parametrize | |
98d779fbStainless Bot2 years ago | 80 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 81 | translation = await async_client.audio.translations.create( | |
aa2634c2stainless-app[bot]3 months ago | 82 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 83 | model="whisper-1", |
d1691a17stainless-app[bot]1 years ago | 84 | prompt="prompt", |
c118d98astainless-app[bot]1 years ago | 85 | response_format="json", |
08b8179aDavid Schnurr2 years ago | 86 | temperature=0, |
| 87 | ) | |
1f3f4784stainless-app[bot]1 years ago | 88 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
08b8179aDavid Schnurr2 years ago | 89 | |
| 90 | @parametrize | |
98d779fbStainless Bot2 years ago | 91 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 92 | response = await async_client.audio.translations.with_raw_response.create( | |
aa2634c2stainless-app[bot]3 months ago | 93 | file=b"Example data", |
08b8179aDavid Schnurr2 years ago | 94 | model="whisper-1", |
| 95 | ) | |
86379b44Stainless Bot2 years ago | 96 | |
| 97 | assert response.is_closed is True | |
08b8179aDavid Schnurr2 years ago | 98 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 99 | translation = response.parse() | |
1f3f4784stainless-app[bot]1 years ago | 100 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
86379b44Stainless Bot2 years ago | 101 | |
| 102 | @parametrize | |
98d779fbStainless Bot2 years ago | 103 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 104 | async with async_client.audio.translations.with_streaming_response.create( | |
aa2634c2stainless-app[bot]3 months ago | 105 | file=b"Example data", |
86379b44Stainless Bot2 years ago | 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() | |
1f3f4784stainless-app[bot]1 years ago | 112 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
86379b44Stainless Bot2 years ago | 113 | |
| 114 | assert cast(Any, response.is_closed) is True |