-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_files.py
More file actions
148 lines (118 loc) · 5.1 KB
/
test_files.py
File metadata and controls
148 lines (118 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from pathlib import Path
import anyio
import pytest
from dirty_equals import IsDict, IsList, IsBytes, IsTuple
from kernel._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files
from kernel._utils import extract_files
readme_path = Path(__file__).parent.parent.joinpath("README.md")
def test_pathlib_includes_file_name() -> None:
result = to_httpx_files({"file": readme_path})
print(result)
assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
def test_tuple_input() -> None:
result = to_httpx_files([("file", readme_path)])
print(result)
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
@pytest.mark.asyncio
async def test_async_pathlib_includes_file_name() -> None:
result = await async_to_httpx_files({"file": readme_path})
print(result)
assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
@pytest.mark.asyncio
async def test_async_supports_anyio_path() -> None:
result = await async_to_httpx_files({"file": anyio.Path(readme_path)})
print(result)
assert result == IsDict({"file": IsTuple("README.md", IsBytes())})
@pytest.mark.asyncio
async def test_async_tuple_input() -> None:
result = await async_to_httpx_files([("file", readme_path)])
print(result)
assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes())))
def test_string_not_allowed() -> None:
with pytest.raises(TypeError, match="Expected file types input to be a FileContent type or to be a tuple"):
to_httpx_files(
{
"file": "foo", # type: ignore
}
)
def assert_different_identities(obj1: object, obj2: object) -> None:
assert obj1 == obj2
assert obj1 is not obj2
class TestDeepcopyWithPaths:
def test_copies_top_level_dict(self) -> None:
original = {"file": b"data", "other": "value"}
result = deepcopy_with_paths(original, [["file"]])
assert_different_identities(result, original)
def test_file_value_is_same_reference(self) -> None:
file_bytes = b"contents"
original = {"file": file_bytes}
result = deepcopy_with_paths(original, [["file"]])
assert_different_identities(result, original)
assert result["file"] is file_bytes
def test_list_popped_wholesale(self) -> None:
files = [b"f1", b"f2"]
original = {"files": files, "title": "t"}
result = deepcopy_with_paths(original, [["files", "<array>"]])
assert_different_identities(result, original)
result_files = result["files"]
assert isinstance(result_files, list)
assert_different_identities(result_files, files)
def test_nested_array_path_copies_list_and_elements(self) -> None:
elem1 = {"file": b"f1", "extra": 1}
elem2 = {"file": b"f2", "extra": 2}
original = {"items": [elem1, elem2]}
result = deepcopy_with_paths(original, [["items", "<array>", "file"]])
assert_different_identities(result, original)
result_items = result["items"]
assert isinstance(result_items, list)
assert_different_identities(result_items, original["items"])
assert_different_identities(result_items[0], elem1)
assert_different_identities(result_items[1], elem2)
def test_empty_paths_returns_same_object(self) -> None:
original = {"foo": "bar"}
result = deepcopy_with_paths(original, [])
assert result is original
def test_multiple_paths(self) -> None:
f1 = b"file1"
f2 = b"file2"
original = {"a": f1, "b": f2, "c": "unchanged"}
result = deepcopy_with_paths(original, [["a"], ["b"]])
assert_different_identities(result, original)
assert result["a"] is f1
assert result["b"] is f2
assert result["c"] is original["c"]
def test_extract_files_does_not_mutate_original_top_level(self) -> None:
file_bytes = b"contents"
original = {"file": file_bytes, "other": "value"}
copied = deepcopy_with_paths(original, [["file"]])
extracted = extract_files(copied, paths=[["file"]])
assert extracted == [("file", file_bytes)]
assert original == {"file": file_bytes, "other": "value"}
assert copied == {"other": "value"}
def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None:
file1 = b"f1"
file2 = b"f2"
original = {
"items": [
{"file": file1, "extra": 1},
{"file": file2, "extra": 2},
],
"title": "example",
}
copied = deepcopy_with_paths(original, [["items", "<array>", "file"]])
extracted = extract_files(copied, paths=[["items", "<array>", "file"]])
assert [entry for _, entry in extracted] == [file1, file2]
assert original == {
"items": [
{"file": file1, "extra": 1},
{"file": file2, "extra": 2},
],
"title": "example",
}
assert copied == {
"items": [
{"extra": 1},
{"extra": 2},
],
"title": "example",
}