forked from ChelseaKR/exitdrill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_loader.py
More file actions
236 lines (205 loc) · 7.68 KB
/
Copy pathtest_loader.py
File metadata and controls
236 lines (205 loc) · 7.68 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import json
from collections.abc import Callable
from pathlib import Path
import pytest
from exitdrill.loader import PackageError, load_baseline, load_export
from exitdrill.models import Coverage, Dimension
def _json(path: Path) -> dict[str, object]:
raw = json.loads(path.read_text(encoding="utf-8"))
assert isinstance(raw, dict)
return raw
def _write(path: Path, raw: object) -> None:
path.write_text(json.dumps(raw), encoding="utf-8")
def test_loads_strict_baseline_and_export(example_root: Path) -> None:
baseline = load_baseline(example_root / "baseline.json")
package = load_export(example_root / "export.json")
assert baseline.drill_id == package.drill_id
assert baseline.coverage[Dimension.ENTITIES] is Coverage.COMPLETE
assert baseline.entities[0].required_fields[0].expected_value == "Synthetic Person"
assert len(package.entities) == 2
@pytest.mark.parametrize(
("document", "mutation", "message"),
[
("baseline", lambda raw: raw.update({"unexpected": True}), "unknown field"),
("baseline", lambda raw: raw.pop("entities"), "missing field"),
(
"baseline",
lambda raw: raw.update({"schema_version": "wrong"}),
"unsupported baseline",
),
(
"export",
lambda raw: raw.update({"schema_version": "wrong"}),
"unsupported export",
),
(
"baseline",
lambda raw: raw["coverage"].update({"entities": "optimistic"}),
"coverage value",
),
(
"export",
lambda raw: raw["entities"][0]["fields"].update({"nested": {}}),
"JSON scalar",
),
(
"baseline",
lambda raw: raw["entities"][0]["required_fields"][0].update({"type": "object"}),
"unsupported",
),
(
"baseline",
lambda raw: raw["entities"][0]["required_fields"][0].update({"expected_value": 42}),
"does not match",
),
(
"baseline",
lambda raw: raw["entities"][0]["required_fields"][0].pop("expected_value"),
"missing field",
),
(
"baseline",
lambda raw: raw["entities"][0]["required_fields"][0].update({"comparison": "casefold"}),
"unknown field",
),
(
"export",
lambda raw: raw["attachments"][0].update({"content_sha256": "bad"}),
"SHA-256",
),
],
)
def test_rejects_invalid_documents(
copied_example: Path,
document: str,
mutation: Callable[[dict[str, object]], None],
message: str,
) -> None:
path = copied_example / f"{document}.json"
raw = _json(path)
mutation(raw)
_write(path, raw)
loader = load_baseline if document == "baseline" else load_export
with pytest.raises(PackageError, match=message):
loader(path)
def test_rejects_duplicate_keys(copied_example: Path) -> None:
path = copied_example / "export.json"
raw = _json(path)
entities = raw["entities"]
assert isinstance(entities, list)
entities.append(entities[0])
_write(path, raw)
with pytest.raises(PackageError, match="unique"):
load_export(path)
@pytest.mark.parametrize(
("entity_index", "field_index", "expected_value"),
[
(0, 0, " "),
(0, 1, 1),
(1, 1, True),
(1, 1, None),
(1, 1, {"nested": "value"}),
],
)
def test_rejects_expected_values_outside_declared_scalar_type(
copied_example: Path,
entity_index: int,
field_index: int,
expected_value: object,
) -> None:
path = copied_example / "baseline.json"
raw = _json(path)
raw["entities"][entity_index]["required_fields"][field_index][ # type: ignore[index]
"expected_value"
] = expected_value
_write(path, raw)
with pytest.raises(PackageError, match="expected_value does not match"):
load_baseline(path)
@pytest.mark.parametrize(
("document", "collection"), [("baseline", "attachments"), ("export", "audit_events")]
)
def test_rejects_duplicate_primary_identity_with_changed_semantics(
copied_example: Path,
document: str,
collection: str,
) -> None:
path = copied_example / f"{document}.json"
raw = _json(path)
items = raw[collection]
assert isinstance(items, list)
duplicate = dict(items[0])
if collection == "attachments":
duplicate["owner_type"] = "person"
duplicate["owner_id"] = "person-001"
else:
duplicate["action"] = "different_synthetic_action"
items.append(duplicate)
_write(path, raw)
loader = load_baseline if document == "baseline" else load_export
with pytest.raises(PackageError, match="keys must be unique"):
loader(path)
@pytest.mark.parametrize("document", ["baseline", "export"])
def test_rejects_duplicate_json_object_keys(copied_example: Path, document: str) -> None:
path = copied_example / f"{document}.json"
content = path.read_text(encoding="utf-8")
duplicate = '"drill_id": "synthetic-crm-exit-001",\n "drill_id": "synthetic-crm-exit-001",'
path.write_text(
content.replace('"drill_id": "synthetic-crm-exit-001",', duplicate, 1),
encoding="utf-8",
)
loader = load_baseline if document == "baseline" else load_export
with pytest.raises(PackageError, match="duplicate object key"):
loader(path)
@pytest.mark.parametrize("document", ["baseline", "export"])
@pytest.mark.parametrize("token", ["NaN", "Infinity", "-Infinity"])
def test_rejects_non_finite_json_numbers(
copied_example: Path,
document: str,
token: str,
) -> None:
path = copied_example / f"{document}.json"
content = path.read_text(encoding="utf-8")
path.write_text(
content.replace(
'"source_system": "Invented CommunityCase CRM"', f'"source_system": {token}'
),
encoding="utf-8",
)
loader = load_baseline if document == "baseline" else load_export
with pytest.raises(PackageError, match="non-finite"):
loader(path)
def test_rejects_float_overflow_to_infinity(copied_example: Path) -> None:
path = copied_example / "export.json"
content = path.read_text(encoding="utf-8")
path.write_text(content.replace('"priority": 2', '"priority": 1e400'), encoding="utf-8")
with pytest.raises(PackageError, match="non-finite"):
load_export(path)
@pytest.mark.parametrize("timestamp", ["not-a-time", "2026-07-22T18:00:00"])
def test_rejects_invalid_or_naive_timestamp(copied_example: Path, timestamp: str) -> None:
path = copied_example / "baseline.json"
raw = _json(path)
raw["captured_at"] = timestamp
_write(path, raw)
with pytest.raises(PackageError, match=r"timestamp|UTC offset"):
load_baseline(path)
def test_rejects_excessive_json_nesting(copied_example: Path) -> None:
path = copied_example / "export.json"
raw = _json(path)
nested: object = "leaf"
for _index in range(65):
nested = [nested]
raw["entities"][0]["fields"]["deep"] = nested # type: ignore[index]
_write(path, raw)
with pytest.raises(PackageError, match="depth limit"):
load_export(path)
@pytest.mark.parametrize(("content", "message"), [("[]", "JSON object"), ("{", "valid JSON")])
def test_rejects_non_object_or_malformed_json(tmp_path: Path, content: str, message: str) -> None:
path = tmp_path / "bad.json"
path.write_text(content, encoding="utf-8")
with pytest.raises(PackageError, match=message):
load_export(path)
def test_rejects_oversized_document(tmp_path: Path) -> None:
path = tmp_path / "large.json"
path.write_bytes(b" " * (4 * 1024 * 1024 + 1))
with pytest.raises(PackageError, match="4 MiB"):
load_export(path)