Debian Codegraph @

tests/api_resources/beta/test_files.py

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