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