openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

498lines · modeblame

08b8179aDavid Schnurr2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
08b8179aDavid Schnurr2 years ago7
aa681899Stainless Bot2 years ago8import httpx
08b8179aDavid Schnurr2 years ago9import pytest
aa681899Stainless Bot2 years ago10from respx import MockRouter
08b8179aDavid Schnurr2 years ago11
86379b44Stainless Bot2 years ago12import openai._legacy_response as _legacy_response
08b8179aDavid Schnurr2 years ago13from openai import OpenAI, AsyncOpenAI
14from tests.utils import assert_matches_type
15from openai.types import FileObject, FileDeleted
16from openai._client import OpenAI, AsyncOpenAI
17from openai.pagination import SyncPage, AsyncPage
18
aa681899Stainless Bot2 years ago19# pyright: reportDeprecated=false
20
08b8179aDavid Schnurr2 years ago21base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
22api_key = "My API Key"
23
24
25class TestFiles:
26strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
27loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
28parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
29
30@parametrize
31def test_method_create(self, client: OpenAI) -> None:
32file = client.files.create(
33file=b"raw file contents",
baa9f07fRobert Craigie2 years ago34purpose="fine-tune",
08b8179aDavid Schnurr2 years ago35)
36assert_matches_type(FileObject, file, path=["response"])
37
38@parametrize
39def test_raw_response_create(self, client: OpenAI) -> None:
40response = client.files.with_raw_response.create(
41file=b"raw file contents",
baa9f07fRobert Craigie2 years ago42purpose="fine-tune",
08b8179aDavid Schnurr2 years ago43)
86379b44Stainless Bot2 years ago44
45assert response.is_closed is True
08b8179aDavid Schnurr2 years ago46assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47file = response.parse()
48assert_matches_type(FileObject, file, path=["response"])
49
86379b44Stainless Bot2 years ago50@parametrize
51def test_streaming_response_create(self, client: OpenAI) -> None:
52with client.files.with_streaming_response.create(
53file=b"raw file contents",
54purpose="fine-tune",
55) as response:
56assert not response.is_closed
57assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58
59file = response.parse()
60assert_matches_type(FileObject, file, path=["response"])
61
62assert cast(Any, response.is_closed) is True
63
08b8179aDavid Schnurr2 years ago64@parametrize
65def test_method_retrieve(self, client: OpenAI) -> None:
66file = client.files.retrieve(
67"string",
68)
69assert_matches_type(FileObject, file, path=["response"])
70
71@parametrize
72def test_raw_response_retrieve(self, client: OpenAI) -> None:
73response = client.files.with_raw_response.retrieve(
74"string",
75)
86379b44Stainless Bot2 years ago76
77assert response.is_closed is True
08b8179aDavid Schnurr2 years ago78assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79file = response.parse()
80assert_matches_type(FileObject, file, path=["response"])
81
86379b44Stainless Bot2 years ago82@parametrize
83def test_streaming_response_retrieve(self, client: OpenAI) -> None:
84with client.files.with_streaming_response.retrieve(
85"string",
86) as response:
87assert not response.is_closed
88assert response.http_request.headers.get("X-Stainless-Lang") == "python"
89
90file = response.parse()
91assert_matches_type(FileObject, file, path=["response"])
92
93assert cast(Any, response.is_closed) is True
94
023a4e66Stainless Bot2 years ago95@parametrize
96def test_path_params_retrieve(self, client: OpenAI) -> None:
97with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
98client.files.with_raw_response.retrieve(
99"",
100)
101
08b8179aDavid Schnurr2 years ago102@parametrize
103def test_method_list(self, client: OpenAI) -> None:
104file = client.files.list()
105assert_matches_type(SyncPage[FileObject], file, path=["response"])
106
baa9f07fRobert Craigie2 years ago107@parametrize
108def test_method_list_with_all_params(self, client: OpenAI) -> None:
109file = client.files.list(
110purpose="string",
111)
112assert_matches_type(SyncPage[FileObject], file, path=["response"])
113
08b8179aDavid Schnurr2 years ago114@parametrize
115def test_raw_response_list(self, client: OpenAI) -> None:
116response = client.files.with_raw_response.list()
86379b44Stainless Bot2 years ago117
118assert response.is_closed is True
08b8179aDavid Schnurr2 years ago119assert response.http_request.headers.get("X-Stainless-Lang") == "python"
120file = response.parse()
121assert_matches_type(SyncPage[FileObject], file, path=["response"])
122
86379b44Stainless Bot2 years ago123@parametrize
124def test_streaming_response_list(self, client: OpenAI) -> None:
125with client.files.with_streaming_response.list() as response:
126assert not response.is_closed
127assert response.http_request.headers.get("X-Stainless-Lang") == "python"
128
129file = response.parse()
130assert_matches_type(SyncPage[FileObject], file, path=["response"])
131
132assert cast(Any, response.is_closed) is True
133
08b8179aDavid Schnurr2 years ago134@parametrize
135def test_method_delete(self, client: OpenAI) -> None:
136file = client.files.delete(
137"string",
138)
139assert_matches_type(FileDeleted, file, path=["response"])
140
141@parametrize
142def test_raw_response_delete(self, client: OpenAI) -> None:
143response = client.files.with_raw_response.delete(
144"string",
145)
86379b44Stainless Bot2 years ago146
147assert response.is_closed is True
08b8179aDavid Schnurr2 years ago148assert response.http_request.headers.get("X-Stainless-Lang") == "python"
149file = response.parse()
150assert_matches_type(FileDeleted, file, path=["response"])
151
86379b44Stainless Bot2 years ago152@parametrize
153def test_streaming_response_delete(self, client: OpenAI) -> None:
154with client.files.with_streaming_response.delete(
155"string",
156) as response:
157assert not response.is_closed
158assert response.http_request.headers.get("X-Stainless-Lang") == "python"
159
160file = response.parse()
161assert_matches_type(FileDeleted, file, path=["response"])
162
163assert cast(Any, response.is_closed) is True
164
023a4e66Stainless Bot2 years ago165@parametrize
166def test_path_params_delete(self, client: OpenAI) -> None:
167with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
168client.files.with_raw_response.delete(
169"",
170)
171
08b8179aDavid Schnurr2 years ago172@parametrize
aa681899Stainless Bot2 years ago173@pytest.mark.respx(base_url=base_url)
174def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago175respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago176file = client.files.content(
08b8179aDavid Schnurr2 years ago177"string",
178)
86379b44Stainless Bot2 years ago179assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
aa681899Stainless Bot2 years ago180assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago181
182@parametrize
aa681899Stainless Bot2 years ago183@pytest.mark.respx(base_url=base_url)
184def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago185respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago186
aa681899Stainless Bot2 years ago187response = client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago188"string",
189)
86379b44Stainless Bot2 years ago190
191assert response.is_closed is True
08b8179aDavid Schnurr2 years ago192assert response.http_request.headers.get("X-Stainless-Lang") == "python"
193file = response.parse()
86379b44Stainless Bot2 years ago194assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
195
196@parametrize
197@pytest.mark.respx(base_url=base_url)
198def test_streaming_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
199respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
200with client.files.with_streaming_response.content(
201"string",
202) as response:
203assert not response.is_closed
204assert response.http_request.headers.get("X-Stainless-Lang") == "python"
205
206file = response.parse()
207assert_matches_type(bytes, file, path=["response"])
208
209assert cast(Any, response.is_closed) is True
aa681899Stainless Bot2 years ago210
023a4e66Stainless Bot2 years ago211@parametrize
212@pytest.mark.respx(base_url=base_url)
213def test_path_params_content(self, client: OpenAI) -> None:
214with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
215client.files.with_raw_response.content(
216"",
217)
218
aa681899Stainless Bot2 years ago219@parametrize
220def test_method_retrieve_content(self, client: OpenAI) -> None:
221with pytest.warns(DeprecationWarning):
222file = client.files.retrieve_content(
223"string",
224)
86379b44Stainless Bot2 years ago225
aa681899Stainless Bot2 years ago226assert_matches_type(str, file, path=["response"])
227
228@parametrize
229def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
230with pytest.warns(DeprecationWarning):
231response = client.files.with_raw_response.retrieve_content(
232"string",
233)
86379b44Stainless Bot2 years ago234
235assert response.is_closed is True
aa681899Stainless Bot2 years ago236assert response.http_request.headers.get("X-Stainless-Lang") == "python"
237file = response.parse()
08b8179aDavid Schnurr2 years ago238assert_matches_type(str, file, path=["response"])
239
86379b44Stainless Bot2 years ago240@parametrize
241def test_streaming_response_retrieve_content(self, client: OpenAI) -> None:
242with pytest.warns(DeprecationWarning):
243with client.files.with_streaming_response.retrieve_content(
244"string",
245) as response:
246assert not response.is_closed
247assert response.http_request.headers.get("X-Stainless-Lang") == "python"
248
249file = response.parse()
250assert_matches_type(str, file, path=["response"])
251
252assert cast(Any, response.is_closed) is True
253
023a4e66Stainless Bot2 years ago254@parametrize
255def test_path_params_retrieve_content(self, client: OpenAI) -> None:
256with pytest.warns(DeprecationWarning):
257with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
258client.files.with_raw_response.retrieve_content(
259"",
260)
261
08b8179aDavid Schnurr2 years ago262
263class TestAsyncFiles:
264strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
265loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
266parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
267
268@parametrize
269async def test_method_create(self, client: AsyncOpenAI) -> None:
270file = await client.files.create(
271file=b"raw file contents",
baa9f07fRobert Craigie2 years ago272purpose="fine-tune",
08b8179aDavid Schnurr2 years ago273)
274assert_matches_type(FileObject, file, path=["response"])
275
276@parametrize
277async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
278response = await client.files.with_raw_response.create(
279file=b"raw file contents",
baa9f07fRobert Craigie2 years ago280purpose="fine-tune",
08b8179aDavid Schnurr2 years ago281)
86379b44Stainless Bot2 years ago282
283assert response.is_closed is True
08b8179aDavid Schnurr2 years ago284assert response.http_request.headers.get("X-Stainless-Lang") == "python"
285file = response.parse()
286assert_matches_type(FileObject, file, path=["response"])
287
86379b44Stainless Bot2 years ago288@parametrize
289async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
290async with client.files.with_streaming_response.create(
291file=b"raw file contents",
292purpose="fine-tune",
293) as response:
294assert not response.is_closed
295assert response.http_request.headers.get("X-Stainless-Lang") == "python"
296
297file = await response.parse()
298assert_matches_type(FileObject, file, path=["response"])
299
300assert cast(Any, response.is_closed) is True
301
08b8179aDavid Schnurr2 years ago302@parametrize
303async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
304file = await client.files.retrieve(
305"string",
306)
307assert_matches_type(FileObject, file, path=["response"])
308
309@parametrize
310async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
311response = await client.files.with_raw_response.retrieve(
312"string",
313)
86379b44Stainless Bot2 years ago314
315assert response.is_closed is True
08b8179aDavid Schnurr2 years ago316assert response.http_request.headers.get("X-Stainless-Lang") == "python"
317file = response.parse()
318assert_matches_type(FileObject, file, path=["response"])
319
86379b44Stainless Bot2 years ago320@parametrize
321async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None:
322async with client.files.with_streaming_response.retrieve(
323"string",
324) as response:
325assert not response.is_closed
326assert response.http_request.headers.get("X-Stainless-Lang") == "python"
327
328file = await response.parse()
329assert_matches_type(FileObject, file, path=["response"])
330
331assert cast(Any, response.is_closed) is True
332
023a4e66Stainless Bot2 years ago333@parametrize
334async def test_path_params_retrieve(self, client: AsyncOpenAI) -> None:
335with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
336await client.files.with_raw_response.retrieve(
337"",
338)
339
08b8179aDavid Schnurr2 years ago340@parametrize
341async def test_method_list(self, client: AsyncOpenAI) -> None:
342file = await client.files.list()
343assert_matches_type(AsyncPage[FileObject], file, path=["response"])
344
baa9f07fRobert Craigie2 years ago345@parametrize
346async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None:
347file = await client.files.list(
348purpose="string",
349)
350assert_matches_type(AsyncPage[FileObject], file, path=["response"])
351
08b8179aDavid Schnurr2 years ago352@parametrize
353async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
354response = await client.files.with_raw_response.list()
86379b44Stainless Bot2 years ago355
356assert response.is_closed is True
08b8179aDavid Schnurr2 years ago357assert response.http_request.headers.get("X-Stainless-Lang") == "python"
358file = response.parse()
359assert_matches_type(AsyncPage[FileObject], file, path=["response"])
360
86379b44Stainless Bot2 years ago361@parametrize
362async def test_streaming_response_list(self, client: AsyncOpenAI) -> None:
363async with client.files.with_streaming_response.list() as response:
364assert not response.is_closed
365assert response.http_request.headers.get("X-Stainless-Lang") == "python"
366
367file = await response.parse()
368assert_matches_type(AsyncPage[FileObject], file, path=["response"])
369
370assert cast(Any, response.is_closed) is True
371
08b8179aDavid Schnurr2 years ago372@parametrize
373async def test_method_delete(self, client: AsyncOpenAI) -> None:
374file = await client.files.delete(
375"string",
376)
377assert_matches_type(FileDeleted, file, path=["response"])
378
379@parametrize
380async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
381response = await client.files.with_raw_response.delete(
382"string",
383)
86379b44Stainless Bot2 years ago384
385assert response.is_closed is True
08b8179aDavid Schnurr2 years ago386assert response.http_request.headers.get("X-Stainless-Lang") == "python"
387file = response.parse()
388assert_matches_type(FileDeleted, file, path=["response"])
389
86379b44Stainless Bot2 years ago390@parametrize
391async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None:
392async with client.files.with_streaming_response.delete(
393"string",
394) as response:
395assert not response.is_closed
396assert response.http_request.headers.get("X-Stainless-Lang") == "python"
397
398file = await response.parse()
399assert_matches_type(FileDeleted, file, path=["response"])
400
401assert cast(Any, response.is_closed) is True
402
023a4e66Stainless Bot2 years ago403@parametrize
404async def test_path_params_delete(self, client: AsyncOpenAI) -> None:
405with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
406await client.files.with_raw_response.delete(
407"",
408)
409
08b8179aDavid Schnurr2 years ago410@parametrize
aa681899Stainless Bot2 years ago411@pytest.mark.respx(base_url=base_url)
412async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago413respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago414file = await client.files.content(
08b8179aDavid Schnurr2 years ago415"string",
416)
86379b44Stainless Bot2 years ago417assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
aa681899Stainless Bot2 years ago418assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago419
420@parametrize
aa681899Stainless Bot2 years ago421@pytest.mark.respx(base_url=base_url)
422async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago423respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago424
aa681899Stainless Bot2 years ago425response = await client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago426"string",
427)
86379b44Stainless Bot2 years ago428
429assert response.is_closed is True
08b8179aDavid Schnurr2 years ago430assert response.http_request.headers.get("X-Stainless-Lang") == "python"
431file = response.parse()
86379b44Stainless Bot2 years ago432assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
433
434@parametrize
435@pytest.mark.respx(base_url=base_url)
436async def test_streaming_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
437respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
438async with client.files.with_streaming_response.content(
439"string",
440) as response:
441assert not response.is_closed
442assert response.http_request.headers.get("X-Stainless-Lang") == "python"
443
444file = await response.parse()
445assert_matches_type(bytes, file, path=["response"])
446
447assert cast(Any, response.is_closed) is True
aa681899Stainless Bot2 years ago448
023a4e66Stainless Bot2 years ago449@parametrize
450@pytest.mark.respx(base_url=base_url)
451async def test_path_params_content(self, client: AsyncOpenAI) -> None:
452with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
453await client.files.with_raw_response.content(
454"",
455)
456
aa681899Stainless Bot2 years ago457@parametrize
458async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None:
459with pytest.warns(DeprecationWarning):
460file = await client.files.retrieve_content(
461"string",
462)
86379b44Stainless Bot2 years ago463
aa681899Stainless Bot2 years ago464assert_matches_type(str, file, path=["response"])
465
466@parametrize
467async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None:
468with pytest.warns(DeprecationWarning):
469response = await client.files.with_raw_response.retrieve_content(
470"string",
471)
86379b44Stainless Bot2 years ago472
473assert response.is_closed is True
aa681899Stainless Bot2 years ago474assert response.http_request.headers.get("X-Stainless-Lang") == "python"
475file = response.parse()
08b8179aDavid Schnurr2 years ago476assert_matches_type(str, file, path=["response"])
86379b44Stainless Bot2 years ago477
478@parametrize
479async def test_streaming_response_retrieve_content(self, client: AsyncOpenAI) -> None:
480with pytest.warns(DeprecationWarning):
481async with client.files.with_streaming_response.retrieve_content(
482"string",
483) as response:
484assert not response.is_closed
485assert response.http_request.headers.get("X-Stainless-Lang") == "python"
486
487file = await response.parse()
488assert_matches_type(str, file, path=["response"])
489
490assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago491
492@parametrize
493async def test_path_params_retrieve_content(self, client: AsyncOpenAI) -> None:
494with pytest.warns(DeprecationWarning):
495with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
496await client.files.with_raw_response.retrieve_content(
497"",
498)