Debian Codegraph @

tests/test_files.py

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