openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/containers/test_files.py
411lines · 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.pagination import SyncCursorPage, AsyncCursorPage |
| 13 | from openai.types.containers import ( |
| 14 | FileListResponse, |
| 15 | FileCreateResponse, |
| 16 | FileRetrieveResponse, |
| 17 | ) |
| 18 | |
| 19 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 20 | |
| 21 | |
| 22 | class TestFiles: |
| 23 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 24 | |
| 25 | @parametrize |
| 26 | def test_method_create(self, client: OpenAI) -> None: |
| 27 | file = client.containers.files.create( |
| 28 | container_id="container_id", |
| 29 | ) |
| 30 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 31 | |
| 32 | @parametrize |
| 33 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 34 | file = client.containers.files.create( |
| 35 | container_id="container_id", |
| 36 | file=b"Example data", |
| 37 | file_id="file_id", |
| 38 | ) |
| 39 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 40 | |
| 41 | @parametrize |
| 42 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 43 | response = client.containers.files.with_raw_response.create( |
| 44 | container_id="container_id", |
| 45 | ) |
| 46 | |
| 47 | assert response.is_closed is True |
| 48 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 49 | file = response.parse() |
| 50 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 51 | |
| 52 | @parametrize |
| 53 | def test_streaming_response_create(self, client: OpenAI) -> None: |
| 54 | with client.containers.files.with_streaming_response.create( |
| 55 | container_id="container_id", |
| 56 | ) as response: |
| 57 | assert not response.is_closed |
| 58 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 59 | |
| 60 | file = response.parse() |
| 61 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 62 | |
| 63 | assert cast(Any, response.is_closed) is True |
| 64 | |
| 65 | @parametrize |
| 66 | def test_path_params_create(self, client: OpenAI) -> None: |
| 67 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 68 | client.containers.files.with_raw_response.create( |
| 69 | container_id="", |
| 70 | ) |
| 71 | |
| 72 | @parametrize |
| 73 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 74 | file = client.containers.files.retrieve( |
| 75 | file_id="file_id", |
| 76 | container_id="container_id", |
| 77 | ) |
| 78 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 79 | |
| 80 | @parametrize |
| 81 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 82 | response = client.containers.files.with_raw_response.retrieve( |
| 83 | file_id="file_id", |
| 84 | container_id="container_id", |
| 85 | ) |
| 86 | |
| 87 | assert response.is_closed is True |
| 88 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 89 | file = response.parse() |
| 90 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 91 | |
| 92 | @parametrize |
| 93 | def test_streaming_response_retrieve(self, client: OpenAI) -> None: |
| 94 | with client.containers.files.with_streaming_response.retrieve( |
| 95 | file_id="file_id", |
| 96 | container_id="container_id", |
| 97 | ) as response: |
| 98 | assert not response.is_closed |
| 99 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 100 | |
| 101 | file = response.parse() |
| 102 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 103 | |
| 104 | assert cast(Any, response.is_closed) is True |
| 105 | |
| 106 | @parametrize |
| 107 | def test_path_params_retrieve(self, client: OpenAI) -> None: |
| 108 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 109 | client.containers.files.with_raw_response.retrieve( |
| 110 | file_id="file_id", |
| 111 | container_id="", |
| 112 | ) |
| 113 | |
| 114 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 115 | client.containers.files.with_raw_response.retrieve( |
| 116 | file_id="", |
| 117 | container_id="container_id", |
| 118 | ) |
| 119 | |
| 120 | @parametrize |
| 121 | def test_method_list(self, client: OpenAI) -> None: |
| 122 | file = client.containers.files.list( |
| 123 | container_id="container_id", |
| 124 | ) |
| 125 | assert_matches_type(SyncCursorPage[FileListResponse], file, path=["response"]) |
| 126 | |
| 127 | @parametrize |
| 128 | def test_method_list_with_all_params(self, client: OpenAI) -> None: |
| 129 | file = client.containers.files.list( |
| 130 | container_id="container_id", |
| 131 | after="after", |
| 132 | limit=0, |
| 133 | order="asc", |
| 134 | ) |
| 135 | assert_matches_type(SyncCursorPage[FileListResponse], file, path=["response"]) |
| 136 | |
| 137 | @parametrize |
| 138 | def test_raw_response_list(self, client: OpenAI) -> None: |
| 139 | response = client.containers.files.with_raw_response.list( |
| 140 | container_id="container_id", |
| 141 | ) |
| 142 | |
| 143 | assert response.is_closed is True |
| 144 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 145 | file = response.parse() |
| 146 | assert_matches_type(SyncCursorPage[FileListResponse], file, path=["response"]) |
| 147 | |
| 148 | @parametrize |
| 149 | def test_streaming_response_list(self, client: OpenAI) -> None: |
| 150 | with client.containers.files.with_streaming_response.list( |
| 151 | container_id="container_id", |
| 152 | ) as response: |
| 153 | assert not response.is_closed |
| 154 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 155 | |
| 156 | file = response.parse() |
| 157 | assert_matches_type(SyncCursorPage[FileListResponse], file, path=["response"]) |
| 158 | |
| 159 | assert cast(Any, response.is_closed) is True |
| 160 | |
| 161 | @parametrize |
| 162 | def test_path_params_list(self, client: OpenAI) -> None: |
| 163 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 164 | client.containers.files.with_raw_response.list( |
| 165 | container_id="", |
| 166 | ) |
| 167 | |
| 168 | @parametrize |
| 169 | def test_method_delete(self, client: OpenAI) -> None: |
| 170 | file = client.containers.files.delete( |
| 171 | file_id="file_id", |
| 172 | container_id="container_id", |
| 173 | ) |
| 174 | assert file is None |
| 175 | |
| 176 | @parametrize |
| 177 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 178 | response = client.containers.files.with_raw_response.delete( |
| 179 | file_id="file_id", |
| 180 | container_id="container_id", |
| 181 | ) |
| 182 | |
| 183 | assert response.is_closed is True |
| 184 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 185 | file = response.parse() |
| 186 | assert file is None |
| 187 | |
| 188 | @parametrize |
| 189 | def test_streaming_response_delete(self, client: OpenAI) -> None: |
| 190 | with client.containers.files.with_streaming_response.delete( |
| 191 | file_id="file_id", |
| 192 | container_id="container_id", |
| 193 | ) as response: |
| 194 | assert not response.is_closed |
| 195 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 196 | |
| 197 | file = response.parse() |
| 198 | assert file is None |
| 199 | |
| 200 | assert cast(Any, response.is_closed) is True |
| 201 | |
| 202 | @parametrize |
| 203 | def test_path_params_delete(self, client: OpenAI) -> None: |
| 204 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 205 | client.containers.files.with_raw_response.delete( |
| 206 | file_id="file_id", |
| 207 | container_id="", |
| 208 | ) |
| 209 | |
| 210 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 211 | client.containers.files.with_raw_response.delete( |
| 212 | file_id="", |
| 213 | container_id="container_id", |
| 214 | ) |
| 215 | |
| 216 | |
| 217 | class TestAsyncFiles: |
| 218 | parametrize = pytest.mark.parametrize( |
| 219 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] |
| 220 | ) |
| 221 | |
| 222 | @parametrize |
| 223 | async def test_method_create(self, async_client: AsyncOpenAI) -> None: |
| 224 | file = await async_client.containers.files.create( |
| 225 | container_id="container_id", |
| 226 | ) |
| 227 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 228 | |
| 229 | @parametrize |
| 230 | async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 231 | file = await async_client.containers.files.create( |
| 232 | container_id="container_id", |
| 233 | file=b"Example data", |
| 234 | file_id="file_id", |
| 235 | ) |
| 236 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 237 | |
| 238 | @parametrize |
| 239 | async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None: |
| 240 | response = await async_client.containers.files.with_raw_response.create( |
| 241 | container_id="container_id", |
| 242 | ) |
| 243 | |
| 244 | assert response.is_closed is True |
| 245 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 246 | file = response.parse() |
| 247 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 248 | |
| 249 | @parametrize |
| 250 | async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None: |
| 251 | async with async_client.containers.files.with_streaming_response.create( |
| 252 | container_id="container_id", |
| 253 | ) as response: |
| 254 | assert not response.is_closed |
| 255 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 256 | |
| 257 | file = await response.parse() |
| 258 | assert_matches_type(FileCreateResponse, file, path=["response"]) |
| 259 | |
| 260 | assert cast(Any, response.is_closed) is True |
| 261 | |
| 262 | @parametrize |
| 263 | async def test_path_params_create(self, async_client: AsyncOpenAI) -> None: |
| 264 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 265 | await async_client.containers.files.with_raw_response.create( |
| 266 | container_id="", |
| 267 | ) |
| 268 | |
| 269 | @parametrize |
| 270 | async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 271 | file = await async_client.containers.files.retrieve( |
| 272 | file_id="file_id", |
| 273 | container_id="container_id", |
| 274 | ) |
| 275 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 276 | |
| 277 | @parametrize |
| 278 | async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 279 | response = await async_client.containers.files.with_raw_response.retrieve( |
| 280 | file_id="file_id", |
| 281 | container_id="container_id", |
| 282 | ) |
| 283 | |
| 284 | assert response.is_closed is True |
| 285 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 286 | file = response.parse() |
| 287 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 288 | |
| 289 | @parametrize |
| 290 | async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 291 | async with async_client.containers.files.with_streaming_response.retrieve( |
| 292 | file_id="file_id", |
| 293 | container_id="container_id", |
| 294 | ) as response: |
| 295 | assert not response.is_closed |
| 296 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 297 | |
| 298 | file = await response.parse() |
| 299 | assert_matches_type(FileRetrieveResponse, file, path=["response"]) |
| 300 | |
| 301 | assert cast(Any, response.is_closed) is True |
| 302 | |
| 303 | @parametrize |
| 304 | async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None: |
| 305 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 306 | await async_client.containers.files.with_raw_response.retrieve( |
| 307 | file_id="file_id", |
| 308 | container_id="", |
| 309 | ) |
| 310 | |
| 311 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 312 | await async_client.containers.files.with_raw_response.retrieve( |
| 313 | file_id="", |
| 314 | container_id="container_id", |
| 315 | ) |
| 316 | |
| 317 | @parametrize |
| 318 | async def test_method_list(self, async_client: AsyncOpenAI) -> None: |
| 319 | file = await async_client.containers.files.list( |
| 320 | container_id="container_id", |
| 321 | ) |
| 322 | assert_matches_type(AsyncCursorPage[FileListResponse], file, path=["response"]) |
| 323 | |
| 324 | @parametrize |
| 325 | async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 326 | file = await async_client.containers.files.list( |
| 327 | container_id="container_id", |
| 328 | after="after", |
| 329 | limit=0, |
| 330 | order="asc", |
| 331 | ) |
| 332 | assert_matches_type(AsyncCursorPage[FileListResponse], file, path=["response"]) |
| 333 | |
| 334 | @parametrize |
| 335 | async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None: |
| 336 | response = await async_client.containers.files.with_raw_response.list( |
| 337 | container_id="container_id", |
| 338 | ) |
| 339 | |
| 340 | assert response.is_closed is True |
| 341 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 342 | file = response.parse() |
| 343 | assert_matches_type(AsyncCursorPage[FileListResponse], file, path=["response"]) |
| 344 | |
| 345 | @parametrize |
| 346 | async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None: |
| 347 | async with async_client.containers.files.with_streaming_response.list( |
| 348 | container_id="container_id", |
| 349 | ) as response: |
| 350 | assert not response.is_closed |
| 351 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 352 | |
| 353 | file = await response.parse() |
| 354 | assert_matches_type(AsyncCursorPage[FileListResponse], file, path=["response"]) |
| 355 | |
| 356 | assert cast(Any, response.is_closed) is True |
| 357 | |
| 358 | @parametrize |
| 359 | async def test_path_params_list(self, async_client: AsyncOpenAI) -> None: |
| 360 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 361 | await async_client.containers.files.with_raw_response.list( |
| 362 | container_id="", |
| 363 | ) |
| 364 | |
| 365 | @parametrize |
| 366 | async def test_method_delete(self, async_client: AsyncOpenAI) -> None: |
| 367 | file = await async_client.containers.files.delete( |
| 368 | file_id="file_id", |
| 369 | container_id="container_id", |
| 370 | ) |
| 371 | assert file is None |
| 372 | |
| 373 | @parametrize |
| 374 | async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 375 | response = await async_client.containers.files.with_raw_response.delete( |
| 376 | file_id="file_id", |
| 377 | container_id="container_id", |
| 378 | ) |
| 379 | |
| 380 | assert response.is_closed is True |
| 381 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 382 | file = response.parse() |
| 383 | assert file is None |
| 384 | |
| 385 | @parametrize |
| 386 | async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None: |
| 387 | async with async_client.containers.files.with_streaming_response.delete( |
| 388 | file_id="file_id", |
| 389 | container_id="container_id", |
| 390 | ) as response: |
| 391 | assert not response.is_closed |
| 392 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 393 | |
| 394 | file = await response.parse() |
| 395 | assert file is None |
| 396 | |
| 397 | assert cast(Any, response.is_closed) is True |
| 398 | |
| 399 | @parametrize |
| 400 | async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None: |
| 401 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `container_id` but received ''"): |
| 402 | await async_client.containers.files.with_raw_response.delete( |
| 403 | file_id="file_id", |
| 404 | container_id="", |
| 405 | ) |
| 406 | |
| 407 | with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"): |
| 408 | await async_client.containers.files.with_raw_response.delete( |
| 409 | file_id="", |
| 410 | container_id="container_id", |
| 411 | ) |
| 412 | |