Debian Codegraph @

tests/test_files.py

raw
1 import io
2 from pathlib import Path
3
4 import anyio
5 import pytest
6 from dirty_equals import IsDict, IsList, IsBytes, IsTuple
7
8 from anthropic._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files
9 from anthropic._utils import extract_files
10
11 readme_path = Path(__file__).parent.parent.joinpath("README.md")
12
13
14 def test_pathlib_includes_file_name() -> None:
15 result = to_httpx_files({"file": readme_path})
16 print(result)
17 assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
18
19
20 def test_tuple_input() -> None:
21 result = to_httpx_files([("file", readme_path)])
22 print(result)
23 assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
24
25
26 @pytest.mark.asyncio
27 async def test_async_pathlib_includes_file_name() -> None:
28 result = await async_to_httpx_files({"file": readme_path})
29 print(result)
30 assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
31
32
33 @pytest.mark.asyncio
34 async def test_async_supports_anyio_path() -> None:
35 result = await async_to_httpx_files({"file": anyio.Path(readme_path)})
36 print(result)
37 assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
38
39
40 @pytest.mark.asyncio
41 async def test_async_tuple_input() -> None:
42 result = await async_to_httpx_files([("file", readme_path)])
43 print(result)
44 assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
45
46
47 def test_pathlib_in_file_tuple() -> None:
48 result = to_httpx_files({"file": ("custom.md", readme_path)})
49 assert result == IsDict({"file": IsTuple("custom.md", readme_path.read_bytes())})
50
51 result = to_httpx_files({"file": ("custom.md", readme_path, "text/markdown", {"X-Custom": "1"})})
52 assert result == IsDict({"file": IsTuple("custom.md", IsBytes(), "text/markdown", {"X-Custom": "1"})})
53
54
55 @pytest.mark.asyncio
56 async def test_async_pathlib_in_file_tuple() -> None:
57 result = await async_to_httpx_files({"file": ("custom.md", readme_path)})
58 assert result == IsDict({"file": IsTuple("custom.md", readme_path.read_bytes())})
59
60 result = await async_to_httpx_files(
61 {"file": ("custom.md", anyio.Path(readme_path), "text/markdown", {"X-Custom": "1"})}
62 )
63 assert result == IsDict({"file": IsTuple("custom.md", IsBytes(), "text/markdown", {"X-Custom": "1"})})
64
65
66 def test_unnamed_file_tuple_uses_base_name() -> None:
67 with open(readme_path, "rb") as f:
68 result = to_httpx_files({"file": (None, f, "text/markdown")})
69 assert result == IsDict({"file": IsTuple("README.md", f, "text/markdown")})
70
71 result = to_httpx_files({"file": (None, readme_path)})
72 assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
73
74 result = to_httpx_files({"file": (None, b"content")})
75 assert result == IsDict({"file": IsTuple(None, b"content")})
76
77
78 @pytest.mark.asyncio
79 async def test_async_unnamed_file_tuple_uses_base_name() -> None:
80 with open(readme_path, "rb") as f:
81 result = await async_to_httpx_files({"file": (None, f, "text/markdown")})
82 assert result == IsDict({"file": IsTuple("README.md", f, "text/markdown")})
83
84 result = await async_to_httpx_files({"file": (None, anyio.Path(readme_path))})
85 assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
86
87
88 def test_file_without_a_name_is_sent_unnamed() -> None:
89 for content in (b"content", io.BytesIO(b"content")):
90 result = to_httpx_files({"file": content})
91 assert result == IsDict({"file": IsTuple(None, content, "application/octet-stream")})
92
93
94 @pytest.mark.asyncio
95 async def test_async_file_without_a_name_is_sent_unnamed() -> None:
96 for content in (b"content", io.BytesIO(b"content")):
97 result = await async_to_httpx_files({"file": content})
98 assert result == IsDict({"file": IsTuple(None, content, "application/octet-stream")})
99
100
101 def test_string_not_allowed() -> None:
102 with pytest.raises(TypeError, match="Expected file types input to be a FileContent type or to be a tuple"):
103 to_httpx_files(
104 {
105 "file": "foo", # type: ignore
106 }
107 )
108
109
110 def assert_different_identities(obj1: object, obj2: object) -> None:
111 assert obj1 == obj2
112 assert obj1 is not obj2
113
114
115 class TestDeepcopyWithPaths:
116 def test_copies_top_level_dict(self) -> None:
117 original = {"file": b"data", "other": "value"}
118 result = deepcopy_with_paths(original, [["file"]])
119 assert_different_identities(result, original)
120
121 def test_file_value_is_same_reference(self) -> None:
122 file_bytes = b"contents"
123 original = {"file": file_bytes}
124 result = deepcopy_with_paths(original, [["file"]])
125 assert_different_identities(result, original)
126 assert result["file"] is file_bytes
127
128 def test_list_popped_wholesale(self) -> None:
129 files = [b"f1", b"f2"]
130 original = {"files": files, "title": "t"}
131 result = deepcopy_with_paths(original, [["files", "<array>"]])
132 assert_different_identities(result, original)
133 result_files = result["files"]
134 assert isinstance(result_files, list)
135 assert_different_identities(result_files, files)
136
137 def test_nested_array_path_copies_list_and_elements(self) -> None:
138 elem1 = {"file": b"f1", "extra": 1}
139 elem2 = {"file": b"f2", "extra": 2}
140 original = {"items": [elem1, elem2]}
141 result = deepcopy_with_paths(original, [["items", "<array>", "file"]])
142 assert_different_identities(result, original)
143 result_items = result["items"]
144 assert isinstance(result_items, list)
145 assert_different_identities(result_items, original["items"])
146 assert_different_identities(result_items[0], elem1)
147 assert_different_identities(result_items[1], elem2)
148
149 def test_empty_paths_returns_same_object(self) -> None:
150 original = {"foo": "bar"}
151 result = deepcopy_with_paths(original, [])
152 assert result is original
153
154 def test_multiple_paths(self) -> None:
155 f1 = b"file1"
156 f2 = b"file2"
157 original = {"a": f1, "b": f2, "c": "unchanged"}
158 result = deepcopy_with_paths(original, [["a"], ["b"]])
159 assert_different_identities(result, original)
160 assert result["a"] is f1
161 assert result["b"] is f2
162 assert result["c"] is original["c"]
163
164 def test_extract_files_does_not_mutate_original_top_level(self) -> None:
165 file_bytes = b"contents"
166 original = {"file": file_bytes, "other": "value"}
167
168 copied = deepcopy_with_paths(original, [["file"]])
169 extracted = extract_files(copied, paths=[["file"]])
170
171 assert extracted == [("file", file_bytes)]
172 assert original == {"file": file_bytes, "other": "value"}
173 assert copied == {"other": "value"}
174
175 def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None:
176 file1 = b"f1"
177 file2 = b"f2"
178 original = {
179 "items": [
180 {"file": file1, "extra": 1},
181 {"file": file2, "extra": 2},
182 ],
183 "title": "example",
184 }
185
186 copied = deepcopy_with_paths(original, [["items", "<array>", "file"]])
187 extracted = extract_files(copied, paths=[["items", "<array>", "file"]])
188
189 assert [entry for _, entry in extracted] == [file1, file2]
190 assert original == {
191 "items": [
192 {"file": file1, "extra": 1},
193 {"file": file2, "extra": 2},
194 ],
195 "title": "example",
196 }
197 assert copied == {
198 "items": [
199 {"extra": 1},
200 {"extra": 2},
201 ],
202 "title": "example",
203 }