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