openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/audio/test_translations.py
112lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | from tests.utils import assert_matches_type |
| 12 | from openai.types.audio import TranslationCreateResponse |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class 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 | |
| 66 | class TestAsyncTranslations: |
| 67 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 68 | |
| 69 | @parametrize |
| 70 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 71 | translation = await async_client.audio.translations.create( |
| 72 | file=b"raw file contents", |
| 73 | model="whisper-1", |
| 74 | ) |
| 75 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
| 76 | |
| 77 | @parametrize |
| 78 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 79 | translation = await async_client.audio.translations.create( |
| 80 | file=b"raw file contents", |
| 81 | model="whisper-1", |
| 82 | prompt="prompt", |
| 83 | response_format="json", |
| 84 | temperature=0, |
| 85 | ) |
| 86 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
| 87 | |
| 88 | @parametrize |
| 89 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 90 | response = await async_client.audio.translations.with_raw_response.create( |
| 91 | file=b"raw file contents", |
| 92 | model="whisper-1", |
| 93 | ) |
| 94 | |
| 95 | assert response.is_closed is True |
| 96 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 97 | translation = response.parse() |
| 98 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
| 99 | |
| 100 | @parametrize |
| 101 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 102 | async with async_client.audio.translations.with_streaming_response.create( |
| 103 | file=b"raw file contents", |
| 104 | model="whisper-1", |
| 105 | ) as response: |
| 106 | assert not response.is_closed |
| 107 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 108 | |
| 109 | translation = await response.parse() |
| 110 | assert_matches_type(TranslationCreateResponse, translation, path=["response"]) |
| 111 | |
| 112 | assert cast(Any, response.is_closed) is True |
| 113 | |