forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exact_input_bytes.py
More file actions
174 lines (143 loc) · 5.58 KB
/
Copy pathtest_exact_input_bytes.py
File metadata and controls
174 lines (143 loc) · 5.58 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from __future__ import annotations
import json
from pathlib import Path
import pytest
from nearmiss.config import load_config, load_config_bytes
from nearmiss.errors import ConfigError, NearmissError
from nearmiss.loaders import load_streets, load_streets_bytes
def _config_payload() -> bytes:
return (
b'city = "Byte City"\n'
b'streets = "streets.geojson"\n'
b'reports = "r.json"\n'
b'exposure = "e.json"\n'
)
def _street_payload(segment_id: str) -> bytes:
return json.dumps(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"segment_id": segment_id},
"geometry": {
"type": "LineString",
"coordinates": [[-121.75, 38.53], [-121.74, 38.54]],
},
}
],
}
).encode()
def test_load_config_bytes_parses_the_supplied_snapshot_without_reopening(tmp_path: Path) -> None:
path = tmp_path / "city.toml"
original = _config_payload()
path.write_bytes(original)
expected = load_config(path)
path.write_text("invalid = [", encoding="utf-8")
actual = load_config_bytes(path, original)
assert actual == expected
assert actual.streets_path == (tmp_path / "streets.geojson").resolve()
def test_load_streets_bytes_parses_the_supplied_snapshot_without_reopening(
tmp_path: Path,
) -> None:
path = tmp_path / "streets.geojson"
original = _street_payload("original")
path.write_bytes(original)
expected = load_streets(path)
path.write_bytes(_street_payload("replacement"))
actual = load_streets_bytes(path, original)
assert actual == expected
assert actual[0].id == "original"
def test_load_streets_bytes_preserves_strict_utf8_input_contract(tmp_path: Path) -> None:
path = tmp_path / "streets.geojson"
with pytest.raises(NearmissError, match="invalid JSON"):
load_streets_bytes(path, _street_payload("utf16").decode().encode("utf-16"))
def test_load_config_bytes_rejects_non_object_json_root(tmp_path: Path) -> None:
with pytest.raises(ConfigError, match="top-level object"):
load_config_bytes(tmp_path / "city.json", b"[]")
@pytest.mark.parametrize(
"replacement",
[
{"type": "FeatureCollection", "features": {}},
{"type": "FeatureCollection", "features": [None]},
{
"type": "FeatureCollection",
"features": [{"type": "Feature", "properties": [], "geometry": {"type": "LineString"}}],
},
{
"type": "FeatureCollection",
"features": [{"type": "Feature", "properties": {}, "geometry": []}],
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {"type": "LineString", "coordinates": {}},
}
],
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {"type": "LineString", "coordinates": [[1], [2, 3]]},
}
],
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [["west", 1], [2, 3]],
},
}
],
},
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [[10**1000, 1], [2, 3]],
},
}
],
},
],
)
def test_load_streets_bytes_rejects_malformed_nested_shapes(
tmp_path: Path, replacement: object
) -> None:
with pytest.raises(NearmissError):
load_streets_bytes(tmp_path / "streets.geojson", json.dumps(replacement).encode())
@pytest.mark.parametrize("field", ["segment_id", "name"])
def test_load_streets_bytes_rejects_escaped_lone_surrogate(tmp_path: Path, field: str) -> None:
payload = _street_payload("valid")
decoded = json.loads(payload)
decoded["features"][0]["properties"][field] = "\ud800"
escaped = json.dumps(decoded).encode("ascii")
with pytest.raises(NearmissError, match="invalid Unicode scalar"):
load_streets_bytes(tmp_path / "streets.geojson", escaped)
def test_load_config_bytes_rejects_escaped_lone_surrogate(tmp_path: Path) -> None:
payload = b'{"city":"\\ud800","streets":"s.geojson","reports":"r.json","exposure":"e.json"}'
with pytest.raises(ConfigError, match="invalid Unicode scalar"):
load_config_bytes(tmp_path / "city.json", payload)
def test_load_config_bytes_translates_deep_object_recursion(tmp_path: Path) -> None:
payload = b'{"nested":' * 2_000 + b"null" + b"}" * 2_000
with pytest.raises(ConfigError, match="invalid config"):
load_config_bytes(tmp_path / "city.json", payload)
def test_load_streets_bytes_translates_deep_list_recursion(tmp_path: Path) -> None:
payload = b"[" * 2_000 + b"null" + b"]" * 2_000
with pytest.raises(NearmissError, match="invalid JSON"):
load_streets_bytes(tmp_path / "streets.geojson", payload)