openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/test_batches.py
268lines · 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 import Batch |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestBatches: |
| 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 | batch = client.batches.create( |
| 23 | completion_window="24h", |
| 24 | endpoint="/v1/chat/completions", |
| 25 | input_file_id="string", |
| 26 | ) |
| 27 | assert_matches_type(Batch, batch, path=["response"]) |
| 28 | |
| 29 | @parametrize |
| 30 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 31 | batch = client.batches.create( |
| 32 | completion_window="24h", |
| 33 | endpoint="/v1/chat/completions", |
| 34 | input_file_id="string", |
| 35 | metadata={"foo": "string"}, |
| 36 | ) |
| 37 | assert_matches_type(Batch, batch, path=["response"]) |
| 38 | |
| 39 | @parametrize |
| 40 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 41 | response = client.batches.with_raw_response.create( |
| 42 | completion_window="24h", |
| 43 | endpoint="/v1/chat/completions", |
| 44 | input_file_id="string", |
| 45 | ) |
| 46 | |
| 47 | assert response.is_closed is True |
| 48 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 49 | batch = response.parse() |
| 50 | assert_matches_type(Batch, batch, path=["response"]) |
| 51 | |
| 52 | @parametrize |
| 53 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 54 | with client.batches.with_streaming_response.create( |
| 55 | completion_window="24h", |
| 56 | endpoint="/v1/chat/completions", |
| 57 | input_file_id="string", |
| 58 | ) as response: |
| 59 | assert not response.is_closed |
| 60 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 61 | |
| 62 | batch = response.parse() |
| 63 | assert_matches_type(Batch, batch, path=["response"]) |
| 64 | |
| 65 | assert cast(Any, response.is_closed) is True |
| 66 | |
| 67 | @parametrize |
| 68 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 69 | batch = client.batches.retrieve( |
| 70 | "string", |
| 71 | ) |
| 72 | assert_matches_type(Batch, batch, path=["response"]) |
| 73 | |
| 74 | @parametrize |
| 75 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 76 | response = client.batches.with_raw_response.retrieve( |
| 77 | "string", |
| 78 | ) |
| 79 | |
| 80 | assert response.is_closed is True |
| 81 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 82 | batch = response.parse() |
| 83 | assert_matches_type(Batch, batch, path=["response"]) |
| 84 | |
| 85 | @parametrize |
| 86 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 87 | with client.batches.with_streaming_response.retrieve( |
| 88 | "string", |
| 89 | ) as response: |
| 90 | assert not response.is_closed |
| 91 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 92 | |
| 93 | batch = response.parse() |
| 94 | assert_matches_type(Batch, batch, path=["response"]) |
| 95 | |
| 96 | assert cast(Any, response.is_closed) is True |
| 97 | |
| 98 | @parametrize |
| 99 | def test_path_params_retrieve(self, client: OpenAI) -> None: |
| 100 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"): |
| 101 | client.batches.with_raw_response.retrieve( |
| 102 | "", |
| 103 | ) |
| 104 | |
| 105 | @parametrize |
| 106 | def test_method_cancel(self, client: OpenAI) -> None: |
| 107 | batch = client.batches.cancel( |
| 108 | "string", |
| 109 | ) |
| 110 | assert_matches_type(Batch, batch, path=["response"]) |
| 111 | |
| 112 | @parametrize |
| 113 | def test_raw_response_cancel(self, client: OpenAI) -> None: |
| 114 | response = client.batches.with_raw_response.cancel( |
| 115 | "string", |
| 116 | ) |
| 117 | |
| 118 | assert response.is_closed is True |
| 119 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 120 | batch = response.parse() |
| 121 | assert_matches_type(Batch, batch, path=["response"]) |
| 122 | |
| 123 | @parametrize |
| 124 | def test_streaming_response_cancel(self, client: OpenAI) -> None: |
| 125 | with client.batches.with_streaming_response.cancel( |
| 126 | "string", |
| 127 | ) as response: |
| 128 | assert not response.is_closed |
| 129 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 130 | |
| 131 | batch = response.parse() |
| 132 | assert_matches_type(Batch, batch, path=["response"]) |
| 133 | |
| 134 | assert cast(Any, response.is_closed) is True |
| 135 | |
| 136 | @parametrize |
| 137 | def test_path_params_cancel(self, client: OpenAI) -> None: |
| 138 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"): |
| 139 | client.batches.with_raw_response.cancel( |
| 140 | "", |
| 141 | ) |
| 142 | |
| 143 | |
| 144 | class TestAsyncBatches: |
| 145 | parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 146 | |
| 147 | @parametrize |
| 148 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 149 | batch = await async_client.batches.create( |
| 150 | completion_window="24h", |
| 151 | endpoint="/v1/chat/completions", |
| 152 | input_file_id="string", |
| 153 | ) |
| 154 | assert_matches_type(Batch, batch, path=["response"]) |
| 155 | |
| 156 | @parametrize |
| 157 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 158 | batch = await async_client.batches.create( |
| 159 | completion_window="24h", |
| 160 | endpoint="/v1/chat/completions", |
| 161 | input_file_id="string", |
| 162 | metadata={"foo": "string"}, |
| 163 | ) |
| 164 | assert_matches_type(Batch, batch, path=["response"]) |
| 165 | |
| 166 | @parametrize |
| 167 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 168 | response = await async_client.batches.with_raw_response.create( |
| 169 | completion_window="24h", |
| 170 | endpoint="/v1/chat/completions", |
| 171 | input_file_id="string", |
| 172 | ) |
| 173 | |
| 174 | assert response.is_closed is True |
| 175 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 176 | batch = response.parse() |
| 177 | assert_matches_type(Batch, batch, path=["response"]) |
| 178 | |
| 179 | @parametrize |
| 180 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 181 | async with async_client.batches.with_streaming_response.create( |
| 182 | completion_window="24h", |
| 183 | endpoint="/v1/chat/completions", |
| 184 | input_file_id="string", |
| 185 | ) as response: |
| 186 | assert not response.is_closed |
| 187 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 188 | |
| 189 | batch = await response.parse() |
| 190 | assert_matches_type(Batch, batch, path=["response"]) |
| 191 | |
| 192 | assert cast(Any, response.is_closed) is True |
| 193 | |
| 194 | @parametrize |
| 195 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 196 | batch = await async_client.batches.retrieve( |
| 197 | "string", |
| 198 | ) |
| 199 | assert_matches_type(Batch, batch, path=["response"]) |
| 200 | |
| 201 | @parametrize |
| 202 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 203 | response = await async_client.batches.with_raw_response.retrieve( |
| 204 | "string", |
| 205 | ) |
| 206 | |
| 207 | assert response.is_closed is True |
| 208 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 209 | batch = response.parse() |
| 210 | assert_matches_type(Batch, batch, path=["response"]) |
| 211 | |
| 212 | @parametrize |
| 213 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 214 | async with async_client.batches.with_streaming_response.retrieve( |
| 215 | "string", |
| 216 | ) as response: |
| 217 | assert not response.is_closed |
| 218 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 219 | |
| 220 | batch = await response.parse() |
| 221 | assert_matches_type(Batch, batch, path=["response"]) |
| 222 | |
| 223 | assert cast(Any, response.is_closed) is True |
| 224 | |
| 225 | @parametrize |
| 226 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 227 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"): |
| 228 | await async_client.batches.with_raw_response.retrieve( |
| 229 | "", |
| 230 | ) |
| 231 | |
| 232 | @parametrize |
| 233 | async def test_method_cancel(self, async_client: AsyncOpenAI) -> None: |
| 234 | batch = await async_client.batches.cancel( |
| 235 | "string", |
| 236 | ) |
| 237 | assert_matches_type(Batch, batch, path=["response"]) |
| 238 | |
| 239 | @parametrize |
| 240 | async def test_raw_response_cancel(self, async_client: AsyncOpenAI) -> None: |
| 241 | response = await async_client.batches.with_raw_response.cancel( |
| 242 | "string", |
| 243 | ) |
| 244 | |
| 245 | assert response.is_closed is True |
| 246 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 247 | batch = response.parse() |
| 248 | assert_matches_type(Batch, batch, path=["response"]) |
| 249 | |
| 250 | @parametrize |
| 251 | async def test_streaming_response_cancel(self, async_client: AsyncOpenAI) -> None: |
| 252 | async with async_client.batches.with_streaming_response.cancel( |
| 253 | "string", |
| 254 | ) as response: |
| 255 | assert not response.is_closed |
| 256 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 257 | |
| 258 | batch = await response.parse() |
| 259 | assert_matches_type(Batch, batch, path=["response"]) |
| 260 | |
| 261 | assert cast(Any, response.is_closed) is True |
| 262 | |
| 263 | @parametrize |
| 264 | async def test_path_params_cancel(self, async_client: AsyncOpenAI) -> None: |
| 265 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `batch_id` but received ''"): |
| 266 | await async_client.batches.with_raw_response.cancel( |
| 267 | "", |
| 268 | ) |
| 269 | |