main
1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3from __future__ import annotations
4
5import os
6from typing import Any, cast
7
8import httpx
9import pytest
10from respx import MockRouter
11
12import openai._legacy_response as _legacy_response
13from openai import OpenAI, AsyncOpenAI
14from tests.utils import assert_matches_type
15from openai.types import FileObject, FileDeleted
16from openai.pagination import SyncCursorPage, AsyncCursorPage
17
18# pyright: reportDeprecated=false
19
20base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
21
22
23class TestFiles:
24 parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
25
26 @parametrize
27 def test_method_create(self, client: OpenAI) -> None:
28 file = client.files.create(
29 file=b"raw file contents",
30 purpose="assistants",
31 )
32 assert_matches_type(FileObject, file, path=["response"])
33
34 @parametrize
35 def test_method_create_with_all_params(self, client: OpenAI) -> None:
36 file = client.files.create(
37 file=b"raw file contents",
38 purpose="assistants",
39 expires_after={
40 "anchor": "created_at",
41 "seconds": 3600,
42 },
43 )
44 assert_matches_type(FileObject, file, path=["response"])
45
46 @parametrize
47 def test_raw_response_create(self, client: OpenAI) -> None:
48 response = client.files.with_raw_response.create(
49 file=b"raw file contents",
50 purpose="assistants",
51 )
52
53 assert response.is_closed is True
54 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
55 file = response.parse()
56 assert_matches_type(FileObject, file, path=["response"])
57
58 @parametrize
59 def test_streaming_response_create(self, client: OpenAI) -> None:
60 with client.files.with_streaming_response.create(
61 file=b"raw file contents",
62 purpose="assistants",
63 ) as response:
64 assert not response.is_closed
65 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
66
67 file = response.parse()
68 assert_matches_type(FileObject, file, path=["response"])
69
70 assert cast(Any, response.is_closed) is True
71
72 @parametrize
73 def test_method_retrieve(self, client: OpenAI) -> None:
74 file = client.files.retrieve(
75 "string",
76 )
77 assert_matches_type(FileObject, file, path=["response"])
78
79 @parametrize
80 def test_raw_response_retrieve(self, client: OpenAI) -> None:
81 response = client.files.with_raw_response.retrieve(
82 "string",
83 )
84
85 assert response.is_closed is True
86 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
87 file = response.parse()
88 assert_matches_type(FileObject, file, path=["response"])
89
90 @parametrize
91 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
92 with client.files.with_streaming_response.retrieve(
93 "string",
94 ) as response:
95 assert not response.is_closed
96 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
97
98 file = response.parse()
99 assert_matches_type(FileObject, file, path=["response"])
100
101 assert cast(Any, response.is_closed) is True
102
103 @parametrize
104 def test_path_params_retrieve(self, client: OpenAI) -> None:
105 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
106 client.files.with_raw_response.retrieve(
107 "",
108 )
109
110 @parametrize
111 def test_method_list(self, client: OpenAI) -> None:
112 file = client.files.list()
113 assert_matches_type(SyncCursorPage[FileObject], file, path=["response"])
114
115 @parametrize
116 def test_method_list_with_all_params(self, client: OpenAI) -> None:
117 file = client.files.list(
118 after="after",
119 limit=0,
120 order="asc",
121 purpose="purpose",
122 )
123 assert_matches_type(SyncCursorPage[FileObject], file, path=["response"])
124
125 @parametrize
126 def test_raw_response_list(self, client: OpenAI) -> None:
127 response = client.files.with_raw_response.list()
128
129 assert response.is_closed is True
130 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
131 file = response.parse()
132 assert_matches_type(SyncCursorPage[FileObject], file, path=["response"])
133
134 @parametrize
135 def test_streaming_response_list(self, client: OpenAI) -> None:
136 with client.files.with_streaming_response.list() as response:
137 assert not response.is_closed
138 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
139
140 file = response.parse()
141 assert_matches_type(SyncCursorPage[FileObject], file, path=["response"])
142
143 assert cast(Any, response.is_closed) is True
144
145 @parametrize
146 def test_method_delete(self, client: OpenAI) -> None:
147 file = client.files.delete(
148 "string",
149 )
150 assert_matches_type(FileDeleted, file, path=["response"])
151
152 @parametrize
153 def test_raw_response_delete(self, client: OpenAI) -> None:
154 response = client.files.with_raw_response.delete(
155 "string",
156 )
157
158 assert response.is_closed is True
159 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
160 file = response.parse()
161 assert_matches_type(FileDeleted, file, path=["response"])
162
163 @parametrize
164 def test_streaming_response_delete(self, client: OpenAI) -> None:
165 with client.files.with_streaming_response.delete(
166 "string",
167 ) as response:
168 assert not response.is_closed
169 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
170
171 file = response.parse()
172 assert_matches_type(FileDeleted, file, path=["response"])
173
174 assert cast(Any, response.is_closed) is True
175
176 @parametrize
177 def test_path_params_delete(self, client: OpenAI) -> None:
178 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
179 client.files.with_raw_response.delete(
180 "",
181 )
182
183 @parametrize
184 @pytest.mark.respx(base_url=base_url)
185 def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
186 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
187 file = client.files.content(
188 "string",
189 )
190 assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
191 assert file.json() == {"foo": "bar"}
192
193 @parametrize
194 @pytest.mark.respx(base_url=base_url)
195 def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
196 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
197
198 response = client.files.with_raw_response.content(
199 "string",
200 )
201
202 assert response.is_closed is True
203 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
204 file = response.parse()
205 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
206
207 @parametrize
208 @pytest.mark.respx(base_url=base_url)
209 def test_streaming_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
210 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
211 with client.files.with_streaming_response.content(
212 "string",
213 ) as response:
214 assert not response.is_closed
215 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
216
217 file = response.parse()
218 assert_matches_type(bytes, file, path=["response"])
219
220 assert cast(Any, response.is_closed) is True
221
222 @parametrize
223 @pytest.mark.respx(base_url=base_url)
224 def test_path_params_content(self, client: OpenAI) -> None:
225 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
226 client.files.with_raw_response.content(
227 "",
228 )
229
230 @parametrize
231 def test_method_retrieve_content(self, client: OpenAI) -> None:
232 with pytest.warns(DeprecationWarning):
233 file = client.files.retrieve_content(
234 "string",
235 )
236
237 assert_matches_type(str, file, path=["response"])
238
239 @parametrize
240 def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
241 with pytest.warns(DeprecationWarning):
242 response = client.files.with_raw_response.retrieve_content(
243 "string",
244 )
245
246 assert response.is_closed is True
247 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
248 file = response.parse()
249 assert_matches_type(str, file, path=["response"])
250
251 @parametrize
252 def test_streaming_response_retrieve_content(self, client: OpenAI) -> None:
253 with pytest.warns(DeprecationWarning):
254 with client.files.with_streaming_response.retrieve_content(
255 "string",
256 ) as response:
257 assert not response.is_closed
258 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
259
260 file = response.parse()
261 assert_matches_type(str, file, path=["response"])
262
263 assert cast(Any, response.is_closed) is True
264
265 @parametrize
266 def test_path_params_retrieve_content(self, client: OpenAI) -> None:
267 with pytest.warns(DeprecationWarning):
268 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
269 client.files.with_raw_response.retrieve_content(
270 "",
271 )
272
273
274class TestAsyncFiles:
275 parametrize = pytest.mark.parametrize(
276 "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"]
277 )
278
279 @parametrize
280 async def test_method_create(self, async_client: AsyncOpenAI) -> None:
281 file = await async_client.files.create(
282 file=b"raw file contents",
283 purpose="assistants",
284 )
285 assert_matches_type(FileObject, file, path=["response"])
286
287 @parametrize
288 async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
289 file = await async_client.files.create(
290 file=b"raw file contents",
291 purpose="assistants",
292 expires_after={
293 "anchor": "created_at",
294 "seconds": 3600,
295 },
296 )
297 assert_matches_type(FileObject, file, path=["response"])
298
299 @parametrize
300 async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
301 response = await async_client.files.with_raw_response.create(
302 file=b"raw file contents",
303 purpose="assistants",
304 )
305
306 assert response.is_closed is True
307 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
308 file = response.parse()
309 assert_matches_type(FileObject, file, path=["response"])
310
311 @parametrize
312 async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
313 async with async_client.files.with_streaming_response.create(
314 file=b"raw file contents",
315 purpose="assistants",
316 ) as response:
317 assert not response.is_closed
318 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
319
320 file = await response.parse()
321 assert_matches_type(FileObject, file, path=["response"])
322
323 assert cast(Any, response.is_closed) is True
324
325 @parametrize
326 async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
327 file = await async_client.files.retrieve(
328 "string",
329 )
330 assert_matches_type(FileObject, file, path=["response"])
331
332 @parametrize
333 async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
334 response = await async_client.files.with_raw_response.retrieve(
335 "string",
336 )
337
338 assert response.is_closed is True
339 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
340 file = response.parse()
341 assert_matches_type(FileObject, file, path=["response"])
342
343 @parametrize
344 async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
345 async with async_client.files.with_streaming_response.retrieve(
346 "string",
347 ) as response:
348 assert not response.is_closed
349 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
350
351 file = await response.parse()
352 assert_matches_type(FileObject, file, path=["response"])
353
354 assert cast(Any, response.is_closed) is True
355
356 @parametrize
357 async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
358 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
359 await async_client.files.with_raw_response.retrieve(
360 "",
361 )
362
363 @parametrize
364 async def test_method_list(self, async_client: AsyncOpenAI) -> None:
365 file = await async_client.files.list()
366 assert_matches_type(AsyncCursorPage[FileObject], file, path=["response"])
367
368 @parametrize
369 async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
370 file = await async_client.files.list(
371 after="after",
372 limit=0,
373 order="asc",
374 purpose="purpose",
375 )
376 assert_matches_type(AsyncCursorPage[FileObject], file, path=["response"])
377
378 @parametrize
379 async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
380 response = await async_client.files.with_raw_response.list()
381
382 assert response.is_closed is True
383 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
384 file = response.parse()
385 assert_matches_type(AsyncCursorPage[FileObject], file, path=["response"])
386
387 @parametrize
388 async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
389 async with async_client.files.with_streaming_response.list() as response:
390 assert not response.is_closed
391 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
392
393 file = await response.parse()
394 assert_matches_type(AsyncCursorPage[FileObject], file, path=["response"])
395
396 assert cast(Any, response.is_closed) is True
397
398 @parametrize
399 async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
400 file = await async_client.files.delete(
401 "string",
402 )
403 assert_matches_type(FileDeleted, file, path=["response"])
404
405 @parametrize
406 async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
407 response = await async_client.files.with_raw_response.delete(
408 "string",
409 )
410
411 assert response.is_closed is True
412 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
413 file = response.parse()
414 assert_matches_type(FileDeleted, file, path=["response"])
415
416 @parametrize
417 async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
418 async with async_client.files.with_streaming_response.delete(
419 "string",
420 ) as response:
421 assert not response.is_closed
422 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
423
424 file = await response.parse()
425 assert_matches_type(FileDeleted, file, path=["response"])
426
427 assert cast(Any, response.is_closed) is True
428
429 @parametrize
430 async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
431 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
432 await async_client.files.with_raw_response.delete(
433 "",
434 )
435
436 @parametrize
437 @pytest.mark.respx(base_url=base_url)
438 async def test_method_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
439 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
440 file = await async_client.files.content(
441 "string",
442 )
443 assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
444 assert file.json() == {"foo": "bar"}
445
446 @parametrize
447 @pytest.mark.respx(base_url=base_url)
448 async def test_raw_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
449 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
450
451 response = await async_client.files.with_raw_response.content(
452 "string",
453 )
454
455 assert response.is_closed is True
456 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
457 file = response.parse()
458 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
459
460 @parametrize
461 @pytest.mark.respx(base_url=base_url)
462 async def test_streaming_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
463 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
464 async with async_client.files.with_streaming_response.content(
465 "string",
466 ) as response:
467 assert not response.is_closed
468 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
469
470 file = await response.parse()
471 assert_matches_type(bytes, file, path=["response"])
472
473 assert cast(Any, response.is_closed) is True
474
475 @parametrize
476 @pytest.mark.respx(base_url=base_url)
477 async def test_path_params_content(self, async_client: AsyncOpenAI) -> None:
478 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
479 await async_client.files.with_raw_response.content(
480 "",
481 )
482
483 @parametrize
484 async def test_method_retrieve_content(self, async_client: AsyncOpenAI) -> None:
485 with pytest.warns(DeprecationWarning):
486 file = await async_client.files.retrieve_content(
487 "string",
488 )
489
490 assert_matches_type(str, file, path=["response"])
491
492 @parametrize
493 async def test_raw_response_retrieve_content(self, async_client: AsyncOpenAI) -> None:
494 with pytest.warns(DeprecationWarning):
495 response = await async_client.files.with_raw_response.retrieve_content(
496 "string",
497 )
498
499 assert response.is_closed is True
500 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
501 file = response.parse()
502 assert_matches_type(str, file, path=["response"])
503
504 @parametrize
505 async def test_streaming_response_retrieve_content(self, async_client: AsyncOpenAI) -> None:
506 with pytest.warns(DeprecationWarning):
507 async with async_client.files.with_streaming_response.retrieve_content(
508 "string",
509 ) as response:
510 assert not response.is_closed
511 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
512
513 file = await response.parse()
514 assert_matches_type(str, file, path=["response"])
515
516 assert cast(Any, response.is_closed) is True
517
518 @parametrize
519 async def test_path_params_retrieve_content(self, async_client: AsyncOpenAI) -> None:
520 with pytest.warns(DeprecationWarning):
521 with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
522 await async_client.files.with_raw_response.retrieve_content(
523 "",
524 )