Debian Codegraph @

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