openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
86379b4471d67a9d2e85f0b0c098787fb99aa4e0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/test_files.py

438lines · modecode

1# File generated from our OpenAPI spec by Stainless.
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._client import OpenAI, AsyncOpenAI
17from openai.pagination import SyncPage, AsyncPage
18
19# pyright: reportDeprecated=false
20
21base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
22api_key = "My API Key"
23
24
25class TestFiles:
26 strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
27 loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
28 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
29
30 @parametrize
31 def test_method_create(self, client: OpenAI) -> None:
32 file = client.files.create(
33 file=b"raw file contents",
34 purpose="fine-tune",
35 )
36 assert_matches_type(FileObject, file, path=["response"])
37
38 @parametrize
39 def test_raw_response_create(self, client: OpenAI) -> None:
40 response = client.files.with_raw_response.create(
41 file=b"raw file contents",
42 purpose="fine-tune",
43 )
44
45 assert response.is_closed is True
46 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
47 file = response.parse()
48 assert_matches_type(FileObject, file, path=["response"])
49
50 @parametrize
51 def test_streaming_response_create(self, client: OpenAI) -> None:
52 with client.files.with_streaming_response.create(
53 file=b"raw file contents",
54 purpose="fine-tune",
55 ) as response:
56 assert not response.is_closed
57 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
58
59 file = response.parse()
60 assert_matches_type(FileObject, file, path=["response"])
61
62 assert cast(Any, response.is_closed) is True
63
64 @parametrize
65 def test_method_retrieve(self, client: OpenAI) -> None:
66 file = client.files.retrieve(
67 "string",
68 )
69 assert_matches_type(FileObject, file, path=["response"])
70
71 @parametrize
72 def test_raw_response_retrieve(self, client: OpenAI) -> None:
73 response = client.files.with_raw_response.retrieve(
74 "string",
75 )
76
77 assert response.is_closed is True
78 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
79 file = response.parse()
80 assert_matches_type(FileObject, file, path=["response"])
81
82 @parametrize
83 def test_streaming_response_retrieve(self, client: OpenAI) -> None:
84 with client.files.with_streaming_response.retrieve(
85 "string",
86 ) as response:
87 assert not response.is_closed
88 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
89
90 file = response.parse()
91 assert_matches_type(FileObject, file, path=["response"])
92
93 assert cast(Any, response.is_closed) is True
94
95 @parametrize
96 def test_method_list(self, client: OpenAI) -> None:
97 file = client.files.list()
98 assert_matches_type(SyncPage[FileObject], file, path=["response"])
99
100 @parametrize
101 def test_method_list_with_all_params(self, client: OpenAI) -> None:
102 file = client.files.list(
103 purpose="string",
104 )
105 assert_matches_type(SyncPage[FileObject], file, path=["response"])
106
107 @parametrize
108 def test_raw_response_list(self, client: OpenAI) -> None:
109 response = client.files.with_raw_response.list()
110
111 assert response.is_closed is True
112 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
113 file = response.parse()
114 assert_matches_type(SyncPage[FileObject], file, path=["response"])
115
116 @parametrize
117 def test_streaming_response_list(self, client: OpenAI) -> None:
118 with client.files.with_streaming_response.list() as response:
119 assert not response.is_closed
120 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
121
122 file = response.parse()
123 assert_matches_type(SyncPage[FileObject], file, path=["response"])
124
125 assert cast(Any, response.is_closed) is True
126
127 @parametrize
128 def test_method_delete(self, client: OpenAI) -> None:
129 file = client.files.delete(
130 "string",
131 )
132 assert_matches_type(FileDeleted, file, path=["response"])
133
134 @parametrize
135 def test_raw_response_delete(self, client: OpenAI) -> None:
136 response = client.files.with_raw_response.delete(
137 "string",
138 )
139
140 assert response.is_closed is True
141 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
142 file = response.parse()
143 assert_matches_type(FileDeleted, file, path=["response"])
144
145 @parametrize
146 def test_streaming_response_delete(self, client: OpenAI) -> None:
147 with client.files.with_streaming_response.delete(
148 "string",
149 ) as response:
150 assert not response.is_closed
151 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
152
153 file = response.parse()
154 assert_matches_type(FileDeleted, file, path=["response"])
155
156 assert cast(Any, response.is_closed) is True
157
158 @parametrize
159 @pytest.mark.respx(base_url=base_url)
160 def test_method_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
161 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
162 file = client.files.content(
163 "string",
164 )
165 assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
166 assert file.json() == {"foo": "bar"}
167
168 @parametrize
169 @pytest.mark.respx(base_url=base_url)
170 def test_raw_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
171 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
172
173 response = client.files.with_raw_response.content(
174 "string",
175 )
176
177 assert response.is_closed is True
178 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
179 file = response.parse()
180 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
181
182 @parametrize
183 @pytest.mark.respx(base_url=base_url)
184 def test_streaming_response_content(self, client: OpenAI, respx_mock: MockRouter) -> None:
185 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
186 with client.files.with_streaming_response.content(
187 "string",
188 ) as response:
189 assert not response.is_closed
190 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
191
192 file = response.parse()
193 assert_matches_type(bytes, file, path=["response"])
194
195 assert cast(Any, response.is_closed) is True
196
197 @parametrize
198 def test_method_retrieve_content(self, client: OpenAI) -> None:
199 with pytest.warns(DeprecationWarning):
200 file = client.files.retrieve_content(
201 "string",
202 )
203
204 assert_matches_type(str, file, path=["response"])
205
206 @parametrize
207 def test_raw_response_retrieve_content(self, client: OpenAI) -> None:
208 with pytest.warns(DeprecationWarning):
209 response = client.files.with_raw_response.retrieve_content(
210 "string",
211 )
212
213 assert response.is_closed is True
214 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
215 file = response.parse()
216 assert_matches_type(str, file, path=["response"])
217
218 @parametrize
219 def test_streaming_response_retrieve_content(self, client: OpenAI) -> None:
220 with pytest.warns(DeprecationWarning):
221 with client.files.with_streaming_response.retrieve_content(
222 "string",
223 ) as response:
224 assert not response.is_closed
225 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
226
227 file = response.parse()
228 assert_matches_type(str, file, path=["response"])
229
230 assert cast(Any, response.is_closed) is True
231
232
233class TestAsyncFiles:
234 strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
235 loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
236 parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
237
238 @parametrize
239 async def test_method_create(self, client: AsyncOpenAI) -> None:
240 file = await client.files.create(
241 file=b"raw file contents",
242 purpose="fine-tune",
243 )
244 assert_matches_type(FileObject, file, path=["response"])
245
246 @parametrize
247 async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
248 response = await client.files.with_raw_response.create(
249 file=b"raw file contents",
250 purpose="fine-tune",
251 )
252
253 assert response.is_closed is True
254 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
255 file = response.parse()
256 assert_matches_type(FileObject, file, path=["response"])
257
258 @parametrize
259 async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
260 async with client.files.with_streaming_response.create(
261 file=b"raw file contents",
262 purpose="fine-tune",
263 ) as response:
264 assert not response.is_closed
265 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
266
267 file = await response.parse()
268 assert_matches_type(FileObject, file, path=["response"])
269
270 assert cast(Any, response.is_closed) is True
271
272 @parametrize
273 async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
274 file = await client.files.retrieve(
275 "string",
276 )
277 assert_matches_type(FileObject, file, path=["response"])
278
279 @parametrize
280 async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
281 response = await client.files.with_raw_response.retrieve(
282 "string",
283 )
284
285 assert response.is_closed is True
286 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
287 file = response.parse()
288 assert_matches_type(FileObject, file, path=["response"])
289
290 @parametrize
291 async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None:
292 async with client.files.with_streaming_response.retrieve(
293 "string",
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(FileObject, file, path=["response"])
300
301 assert cast(Any, response.is_closed) is True
302
303 @parametrize
304 async def test_method_list(self, client: AsyncOpenAI) -> None:
305 file = await client.files.list()
306 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
307
308 @parametrize
309 async def test_method_list_with_all_params(self, client: AsyncOpenAI) -> None:
310 file = await client.files.list(
311 purpose="string",
312 )
313 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
314
315 @parametrize
316 async def test_raw_response_list(self, client: AsyncOpenAI) -> None:
317 response = await client.files.with_raw_response.list()
318
319 assert response.is_closed is True
320 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
321 file = response.parse()
322 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
323
324 @parametrize
325 async def test_streaming_response_list(self, client: AsyncOpenAI) -> None:
326 async with client.files.with_streaming_response.list() as response:
327 assert not response.is_closed
328 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
329
330 file = await response.parse()
331 assert_matches_type(AsyncPage[FileObject], file, path=["response"])
332
333 assert cast(Any, response.is_closed) is True
334
335 @parametrize
336 async def test_method_delete(self, client: AsyncOpenAI) -> None:
337 file = await client.files.delete(
338 "string",
339 )
340 assert_matches_type(FileDeleted, file, path=["response"])
341
342 @parametrize
343 async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
344 response = await client.files.with_raw_response.delete(
345 "string",
346 )
347
348 assert response.is_closed is True
349 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
350 file = response.parse()
351 assert_matches_type(FileDeleted, file, path=["response"])
352
353 @parametrize
354 async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None:
355 async with client.files.with_streaming_response.delete(
356 "string",
357 ) as response:
358 assert not response.is_closed
359 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
360
361 file = await response.parse()
362 assert_matches_type(FileDeleted, file, path=["response"])
363
364 assert cast(Any, response.is_closed) is True
365
366 @parametrize
367 @pytest.mark.respx(base_url=base_url)
368 async def test_method_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
369 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
370 file = await client.files.content(
371 "string",
372 )
373 assert isinstance(file, _legacy_response.HttpxBinaryResponseContent)
374 assert file.json() == {"foo": "bar"}
375
376 @parametrize
377 @pytest.mark.respx(base_url=base_url)
378 async def test_raw_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
379 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
380
381 response = await client.files.with_raw_response.content(
382 "string",
383 )
384
385 assert response.is_closed is True
386 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
387 file = response.parse()
388 assert_matches_type(_legacy_response.HttpxBinaryResponseContent, file, path=["response"])
389
390 @parametrize
391 @pytest.mark.respx(base_url=base_url)
392 async def test_streaming_response_content(self, client: AsyncOpenAI, respx_mock: MockRouter) -> None:
393 respx_mock.get("/files/string/content").mock(return_value=httpx.Response(200, json={"foo": "bar"}))
394 async with client.files.with_streaming_response.content(
395 "string",
396 ) as response:
397 assert not response.is_closed
398 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
399
400 file = await response.parse()
401 assert_matches_type(bytes, file, path=["response"])
402
403 assert cast(Any, response.is_closed) is True
404
405 @parametrize
406 async def test_method_retrieve_content(self, client: AsyncOpenAI) -> None:
407 with pytest.warns(DeprecationWarning):
408 file = await client.files.retrieve_content(
409 "string",
410 )
411
412 assert_matches_type(str, file, path=["response"])
413
414 @parametrize
415 async def test_raw_response_retrieve_content(self, client: AsyncOpenAI) -> None:
416 with pytest.warns(DeprecationWarning):
417 response = await client.files.with_raw_response.retrieve_content(
418 "string",
419 )
420
421 assert response.is_closed is True
422 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
423 file = response.parse()
424 assert_matches_type(str, file, path=["response"])
425
426 @parametrize
427 async def test_streaming_response_retrieve_content(self, client: AsyncOpenAI) -> None:
428 with pytest.warns(DeprecationWarning):
429 async with client.files.with_streaming_response.retrieve_content(
430 "string",
431 ) as response:
432 assert not response.is_closed
433 assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435 file = await response.parse()
436 assert_matches_type(str, file, path=["response"])
437
438 assert cast(Any, response.is_closed) is True
439