openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
robert/fix-streaming-parsing

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/containers/test_files.py

409lines · modecode

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