openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.24.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

492lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
08b8179aDavid Schnurr2 years ago2
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
4a0f0fa0Stainless Bot2 years ago15from openai.types import FileObject, FileDeleted
08b8179aDavid Schnurr2 years ago16from openai.pagination import SyncPage, AsyncPage
17
aa681899Stainless Bot2 years ago18# pyright: reportDeprecated=false
19
08b8179aDavid Schnurr2 years ago20base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
21
22
23class TestFiles:
98d779fbStainless Bot2 years ago24parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago25
26@parametrize
27def test_method_create(self, client: OpenAI) -> None:
28file = client.files.create(
29file=b"raw file contents",
baa9f07fRobert Craigie2 years ago30purpose="fine-tune",
08b8179aDavid Schnurr2 years ago31)
32assert_matches_type(FileObject, file, path=["response"])
33
34@parametrize
35def test_raw_response_create(self, client: OpenAI) -> None:
36response = client.files.with_raw_response.create(
37file=b"raw file contents",
baa9f07fRobert Craigie2 years ago38purpose="fine-tune",
08b8179aDavid Schnurr2 years ago39)
86379b44Stainless Bot2 years ago40
41assert response.is_closed is True
08b8179aDavid Schnurr2 years ago42assert response.http_request.headers.get("X-Stainless-Lang") == "python"
43file = response.parse()
44assert_matches_type(FileObject, file, path=["response"])
45
86379b44Stainless Bot2 years ago46@parametrize
47def test_streaming_response_create(self, client: OpenAI) -> None:
48with client.files.with_streaming_response.create(
49file=b"raw file contents",
50purpose="fine-tune",
51) as response:
52assert not response.is_closed
53assert response.http_request.headers.get("X-Stainless-Lang") == "python"
54
55file = response.parse()
56assert_matches_type(FileObject, file, path=["response"])
57
58assert cast(Any, response.is_closed) is True
59
08b8179aDavid Schnurr2 years ago60@parametrize
61def test_method_retrieve(self, client: OpenAI) -> None:
62file = client.files.retrieve(
63"string",
64)
65assert_matches_type(FileObject, file, path=["response"])
66
67@parametrize
68def test_raw_response_retrieve(self, client: OpenAI) -> None:
69response = client.files.with_raw_response.retrieve(
70"string",
71)
86379b44Stainless Bot2 years ago72
73assert response.is_closed is True
08b8179aDavid Schnurr2 years ago74assert response.http_request.headers.get("X-Stainless-Lang") == "python"
75file = response.parse()
76assert_matches_type(FileObject, file, path=["response"])
77
86379b44Stainless Bot2 years ago78@parametrize
79def test_streaming_response_retrieve(self, client: OpenAI) -> None:
80with client.files.with_streaming_response.retrieve(
81"string",
82) as response:
83assert not response.is_closed
84assert response.http_request.headers.get("X-Stainless-Lang") == "python"
85
86file = response.parse()
87assert_matches_type(FileObject, file, path=["response"])
88
89assert cast(Any, response.is_closed) is True
90
023a4e66Stainless Bot2 years ago91@parametrize
92def test_path_params_retrieve(self, client: OpenAI) -> None:
93with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
94client.files.with_raw_response.retrieve(
95"",
96)
97
08b8179aDavid Schnurr2 years ago98@parametrize
99def test_method_list(self, client: OpenAI) -> None:
100file = client.files.list()
101assert_matches_type(SyncPage[FileObject], file, path=["response"])
102
baa9f07fRobert Craigie2 years ago103@parametrize
104def test_method_list_with_all_params(self, client: OpenAI) -> None:
105file = client.files.list(
106purpose="string",
107)
108assert_matches_type(SyncPage[FileObject], file, path=["response"])
109
08b8179aDavid Schnurr2 years ago110@parametrize
111def test_raw_response_list(self, client: OpenAI) -> None:
112response = client.files.with_raw_response.list()
86379b44Stainless Bot2 years ago113
114assert response.is_closed is True
08b8179aDavid Schnurr2 years ago115assert response.http_request.headers.get("X-Stainless-Lang") == "python"
116file = response.parse()
117assert_matches_type(SyncPage[FileObject], file, path=["response"])
118
86379b44Stainless Bot2 years ago119@parametrize
120def test_streaming_response_list(self, client: OpenAI) -> None:
121with client.files.with_streaming_response.list() as response:
122assert not response.is_closed
123assert response.http_request.headers.get("X-Stainless-Lang") == "python"
124
125file = response.parse()
126assert_matches_type(SyncPage[FileObject], file, path=["response"])
127
128assert cast(Any, response.is_closed) is True
129
08b8179aDavid Schnurr2 years ago130@parametrize
131def test_method_delete(self, client: OpenAI) -> None:
132file = client.files.delete(
133"string",
134)
135assert_matches_type(FileDeleted, file, path=["response"])
136
137@parametrize
138def test_raw_response_delete(self, client: OpenAI) -> None:
139response = client.files.with_raw_response.delete(
140"string",
141)
86379b44Stainless Bot2 years ago142
143assert response.is_closed is True
08b8179aDavid Schnurr2 years ago144assert response.http_request.headers.get("X-Stainless-Lang") == "python"
145file = response.parse()
146assert_matches_type(FileDeleted, file, path=["response"])
147
86379b44Stainless Bot2 years ago148@parametrize
149def test_streaming_response_delete(self, client: OpenAI) -> None:
150with client.files.with_streaming_response.delete(
151"string",
152) as response:
153assert not response.is_closed
154assert response.http_request.headers.get("X-Stainless-Lang") == "python"
155
156file = response.parse()
157assert_matches_type(FileDeleted, file, path=["response"])
158
159assert cast(Any, response.is_closed) is True
160
023a4e66Stainless Bot2 years ago161@parametrize
162def test_path_params_delete(self, client: OpenAI) -> None:
163with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
164client.files.with_raw_response.delete(
165"",
166)
167
08b8179aDavid Schnurr2 years ago168@parametrize
aa681899Stainless Bot2 years ago169@pytest.mark.respx(base_url=base_url)
170def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago171respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
aa681899Stainless Bot2 years ago172file = client.files.content(
08b8179aDavid Schnurr2 years ago173"string",
174)
86379b44Stainless Bot2 years ago175assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
aa681899Stainless Bot2 years ago176assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago177
178@parametrize
aa681899Stainless Bot2 years ago179@pytest.mark.respx(base_url=base_url)
180def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago181respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago182
aa681899Stainless Bot2 years ago183response = client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago184"string",
185)
86379b44Stainless Bot2 years ago186
187assert response.is_closed is True
08b8179aDavid Schnurr2 years ago188assert response.http_request.headers.get("X-Stainless-Lang") == "python"
189file = response.parse()
86379b44Stainless Bot2 years ago190assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
191
192@parametrize
193@pytest.mark.respx(base_url=base_url)
194def test_streaming_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
195respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
196with client.files.with_streaming_response.content(
197"string",
198) as response:
199assert not response.is_closed
200assert response.http_request.headers.get("X-Stainless-Lang") == "python"
201
202file = response.parse()
203assert_matches_type(bytes, file, path=["response"])
204
205assert cast(Any, response.is_closed) is True
aa681899Stainless Bot2 years ago206
023a4e66Stainless Bot2 years ago207@parametrize
208@pytest.mark.respx(base_url=base_url)
209def test_path_params_content(self, client: OpenAI) -> None:
210with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
211client.files.with_raw_response.content(
212"",
213)
214
aa681899Stainless Bot2 years ago215@parametrize
216def test_method_retrieve_content(self, client: OpenAI) -> None:
217with pytest.warns(DeprecationWarning):
218file = client.files.retrieve_content(
219"string",
220)
86379b44Stainless Bot2 years ago221
aa681899Stainless Bot2 years ago222assert_matches_type(str, file, path=["response"])
223
224@parametrize
225def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
226with pytest.warns(DeprecationWarning):
227response = client.files.with_raw_response.retrieve_content(
228"string",
229)
86379b44Stainless Bot2 years ago230
231assert response.is_closed is True
aa681899Stainless Bot2 years ago232assert response.http_request.headers.get("X-Stainless-Lang") == "python"
233file = response.parse()
08b8179aDavid Schnurr2 years ago234assert_matches_type(str, file, path=["response"])
235
86379b44Stainless Bot2 years ago236@parametrize
237def test_streaming_response_retrieve_content(self, client: OpenAI) -> None:
238with pytest.warns(DeprecationWarning):
239with client.files.with_streaming_response.retrieve_content(
240"string",
241) as response:
242assert not response.is_closed
243assert response.http_request.headers.get("X-Stainless-Lang") == "python"
244
245file = response.parse()
246assert_matches_type(str, file, path=["response"])
247
248assert cast(Any, response.is_closed) is True
249
023a4e66Stainless Bot2 years ago250@parametrize
251def test_path_params_retrieve_content(self, client: OpenAI) -> None:
252with pytest.warns(DeprecationWarning):
253with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
254client.files.with_raw_response.retrieve_content(
255"",
256)
257
08b8179aDavid Schnurr2 years ago258
259class TestAsyncFiles:
98d779fbStainless Bot2 years ago260parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
08b8179aDavid Schnurr2 years ago261
262@parametrize
98d779fbStainless Bot2 years ago263async def test_method_create(self, async_client: AsyncOpenAI) -> None:
264file = await async_client.files.create(
08b8179aDavid Schnurr2 years ago265file=b"raw file contents",
baa9f07fRobert Craigie2 years ago266purpose="fine-tune",
08b8179aDavid Schnurr2 years ago267)
268assert_matches_type(FileObject, file, path=["response"])
269
270@parametrize
98d779fbStainless Bot2 years ago271async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
272response = await async_client.files.with_raw_response.create(
08b8179aDavid Schnurr2 years ago273file=b"raw file contents",
baa9f07fRobert Craigie2 years ago274purpose="fine-tune",
08b8179aDavid Schnurr2 years ago275)
86379b44Stainless Bot2 years ago276
277assert response.is_closed is True
08b8179aDavid Schnurr2 years ago278assert response.http_request.headers.get("X-Stainless-Lang") == "python"
279file = response.parse()
280assert_matches_type(FileObject, file, path=["response"])
281
86379b44Stainless Bot2 years ago282@parametrize
98d779fbStainless Bot2 years ago283async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
284async with async_client.files.with_streaming_response.create(
86379b44Stainless Bot2 years ago285file=b"raw file contents",
286purpose="fine-tune",
287) as response:
288assert not response.is_closed
289assert response.http_request.headers.get("X-Stainless-Lang") == "python"
290
291file = await response.parse()
292assert_matches_type(FileObject, file, path=["response"])
293
294assert cast(Any, response.is_closed) is True
295
08b8179aDavid Schnurr2 years ago296@parametrize
98d779fbStainless Bot2 years ago297async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
298file = await async_client.files.retrieve(
08b8179aDavid Schnurr2 years ago299"string",
300)
301assert_matches_type(FileObject, file, path=["response"])
302
303@parametrize
98d779fbStainless Bot2 years ago304async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
305response = await async_client.files.with_raw_response.retrieve(
08b8179aDavid Schnurr2 years ago306"string",
307)
86379b44Stainless Bot2 years ago308
309assert response.is_closed is True
08b8179aDavid Schnurr2 years ago310assert response.http_request.headers.get("X-Stainless-Lang") == "python"
311file = response.parse()
312assert_matches_type(FileObject, file, path=["response"])
313
86379b44Stainless Bot2 years ago314@parametrize
98d779fbStainless Bot2 years ago315async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
316async with async_client.files.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago317"string",
318) as response:
319assert not response.is_closed
320assert response.http_request.headers.get("X-Stainless-Lang") == "python"
321
322file = await response.parse()
323assert_matches_type(FileObject, file, path=["response"])
324
325assert cast(Any, response.is_closed) is True
326
023a4e66Stainless Bot2 years ago327@parametrize
98d779fbStainless Bot2 years ago328async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago329with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
98d779fbStainless Bot2 years ago330await async_client.files.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago331"",
332)
333
08b8179aDavid Schnurr2 years ago334@parametrize
98d779fbStainless Bot2 years ago335async def test_method_list(self, async_client: AsyncOpenAI) -> None:
336file = await async_client.files.list()
08b8179aDavid Schnurr2 years ago337assert_matches_type(AsyncPage[FileObject], file, path=["response"])
338
baa9f07fRobert Craigie2 years ago339@parametrize
98d779fbStainless Bot2 years ago340async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
341file = await async_client.files.list(
baa9f07fRobert Craigie2 years ago342purpose="string",
343)
344assert_matches_type(AsyncPage[FileObject], file, path=["response"])
345
08b8179aDavid Schnurr2 years ago346@parametrize
98d779fbStainless Bot2 years ago347async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
348response = await async_client.files.with_raw_response.list()
86379b44Stainless Bot2 years ago349
350assert response.is_closed is True
08b8179aDavid Schnurr2 years ago351assert response.http_request.headers.get("X-Stainless-Lang") == "python"
352file = response.parse()
353assert_matches_type(AsyncPage[FileObject], file, path=["response"])
354
86379b44Stainless Bot2 years ago355@parametrize
98d779fbStainless Bot2 years ago356async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
357async with async_client.files.with_streaming_response.list() as response:
86379b44Stainless Bot2 years ago358assert not response.is_closed
359assert response.http_request.headers.get("X-Stainless-Lang") == "python"
360
361file = await response.parse()
362assert_matches_type(AsyncPage[FileObject], file, path=["response"])
363
364assert cast(Any, response.is_closed) is True
365
08b8179aDavid Schnurr2 years ago366@parametrize
98d779fbStainless Bot2 years ago367async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
368file = await async_client.files.delete(
08b8179aDavid Schnurr2 years ago369"string",
370)
371assert_matches_type(FileDeleted, file, path=["response"])
372
373@parametrize
98d779fbStainless Bot2 years ago374async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
375response = await async_client.files.with_raw_response.delete(
08b8179aDavid Schnurr2 years ago376"string",
377)
86379b44Stainless Bot2 years ago378
379assert response.is_closed is True
08b8179aDavid Schnurr2 years ago380assert response.http_request.headers.get("X-Stainless-Lang") == "python"
381file = response.parse()
382assert_matches_type(FileDeleted, file, path=["response"])
383
86379b44Stainless Bot2 years ago384@parametrize
98d779fbStainless Bot2 years ago385async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
386async with async_client.files.with_streaming_response.delete(
86379b44Stainless Bot2 years ago387"string",
388) as response:
389assert not response.is_closed
390assert response.http_request.headers.get("X-Stainless-Lang") == "python"
391
392file = await response.parse()
393assert_matches_type(FileDeleted, file, path=["response"])
394
395assert cast(Any, response.is_closed) is True
396
023a4e66Stainless Bot2 years ago397@parametrize
98d779fbStainless Bot2 years ago398async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago399with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
98d779fbStainless Bot2 years ago400await async_client.files.with_raw_response.delete(
023a4e66Stainless Bot2 years ago401"",
402)
403
08b8179aDavid Schnurr2 years ago404@parametrize
aa681899Stainless Bot2 years ago405@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago406async def test_method_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago407respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago408file = await async_client.files.content(
08b8179aDavid Schnurr2 years ago409"string",
410)
86379b44Stainless Bot2 years ago411assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
aa681899Stainless Bot2 years ago412assert file.json() == {"foo": "bar"}
08b8179aDavid Schnurr2 years ago413
414@parametrize
aa681899Stainless Bot2 years ago415@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago416async def test_raw_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
b56cf723Stainless Bot2 years ago417respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
86379b44Stainless Bot2 years ago418
98d779fbStainless Bot2 years ago419response = await async_client.files.with_raw_response.content(
08b8179aDavid Schnurr2 years ago420"string",
421)
86379b44Stainless Bot2 years ago422
423assert response.is_closed is True
08b8179aDavid Schnurr2 years ago424assert response.http_request.headers.get("X-Stainless-Lang") == "python"
425file = response.parse()
86379b44Stainless Bot2 years ago426assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
427
428@parametrize
429@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago430async def test_streaming_response_content(self, async_client: AsyncOpenAI, respx_mock: MockRouter) -> None:
86379b44Stainless Bot2 years ago431respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
98d779fbStainless Bot2 years ago432async with async_client.files.with_streaming_response.content(
86379b44Stainless Bot2 years ago433"string",
434) as response:
435assert not response.is_closed
436assert response.http_request.headers.get("X-Stainless-Lang") == "python"
437
438file = await response.parse()
439assert_matches_type(bytes, file, path=["response"])
440
441assert cast(Any, response.is_closed) is True
aa681899Stainless Bot2 years ago442
023a4e66Stainless Bot2 years ago443@parametrize
444@pytest.mark.respx(base_url=base_url)
98d779fbStainless Bot2 years ago445async def test_path_params_content(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago446with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
98d779fbStainless Bot2 years ago447await async_client.files.with_raw_response.content(
023a4e66Stainless Bot2 years ago448"",
449)
450
aa681899Stainless Bot2 years ago451@parametrize
98d779fbStainless Bot2 years ago452async def test_method_retrieve_content(self, async_client: AsyncOpenAI) -> None:
aa681899Stainless Bot2 years ago453with pytest.warns(DeprecationWarning):
98d779fbStainless Bot2 years ago454file = await async_client.files.retrieve_content(
aa681899Stainless Bot2 years ago455"string",
456)
86379b44Stainless Bot2 years ago457
aa681899Stainless Bot2 years ago458assert_matches_type(str, file, path=["response"])
459
460@parametrize
98d779fbStainless Bot2 years ago461async def test_raw_response_retrieve_content(self, async_client: AsyncOpenAI) -> None:
aa681899Stainless Bot2 years ago462with pytest.warns(DeprecationWarning):
98d779fbStainless Bot2 years ago463response = await async_client.files.with_raw_response.retrieve_content(
aa681899Stainless Bot2 years ago464"string",
465)
86379b44Stainless Bot2 years ago466
467assert response.is_closed is True
aa681899Stainless Bot2 years ago468assert response.http_request.headers.get("X-Stainless-Lang") == "python"
469file = response.parse()
08b8179aDavid Schnurr2 years ago470assert_matches_type(str, file, path=["response"])
86379b44Stainless Bot2 years ago471
472@parametrize
98d779fbStainless Bot2 years ago473async def test_streaming_response_retrieve_content(self, async_client: AsyncOpenAI) -> None:
86379b44Stainless Bot2 years ago474with pytest.warns(DeprecationWarning):
98d779fbStainless Bot2 years ago475async with async_client.files.with_streaming_response.retrieve_content(
86379b44Stainless Bot2 years ago476"string",
477) as response:
478assert not response.is_closed
479assert response.http_request.headers.get("X-Stainless-Lang") == "python"
480
481file = await response.parse()
482assert_matches_type(str, file, path=["response"])
483
484assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago485
486@parametrize
98d779fbStainless Bot2 years ago487async def test_path_params_retrieve_content(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago488with pytest.warns(DeprecationWarning):
489with pytest.raises(ValueError, match=r"Expected a non-empty value for `file_id` but received ''"):
98d779fbStainless Bot2 years ago490await async_client.files.with_raw_response.retrieve_content(
023a4e66Stainless Bot2 years ago491"",
492)