-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config_properties.py
More file actions
79 lines (67 loc) · 2.15 KB
/
Copy pathtest_config_properties.py
File metadata and controls
79 lines (67 loc) · 2.15 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
"""Property tests: arbitrary input either fully validates or raises — never half-applies."""
from __future__ import annotations
import importlib.resources
import json
import jsonschema
from hypothesis import given
from hypothesis import strategies as st
from openjobradar.config.loader import normalize
json_values = st.recursive(
st.none()
| st.booleans()
| st.integers()
| st.floats(allow_nan=False)
| st.text(max_size=20),
lambda children: st.lists(children, max_size=4)
| st.dictionaries(st.text(max_size=8), children, max_size=4),
max_leaves=12,
)
profile_sections = st.sampled_from(
[
"schema_version",
"candidate",
"targeting",
"location_policy",
"hard_filters",
"values_screening",
"legacy_hard_limits",
]
)
_SETTINGS_SCHEMA: dict = json.loads(
(importlib.resources.files("openjobradar.schemas") / "settings.v2.json").read_text(
encoding="utf-8"
)
)
@given(st.dictionaries(profile_sections, json_values, max_size=7))
def test_arbitrary_documents_never_return_unvalidated(document: dict) -> None:
try:
result = normalize("profile", document)
except Exception:
return
assert result == document
@given(json_values)
def test_arbitrary_top_level_types_fail_closed(value: object) -> None:
try:
normalize("settings", value)
except Exception as exc:
assert isinstance(exc, RuntimeError)
return
raise AssertionError(f"non-mapping input {value!r} unexpectedly validated")
@given(
st.fixed_dictionaries(
{},
optional={
"alert_threshold": st.one_of(st.integers(), st.text(max_size=10)),
"digest_threshold": st.integers(min_value=-100),
"quiet_hours": st.one_of(st.none(), st.booleans()),
},
)
)
def test_threshold_section_either_valid_or_rejected(alerts: dict) -> None:
document = {"schema_version": 2, "alerts": alerts}
try:
result = normalize("settings", document)
except Exception:
return
validator_cls = jsonschema.validators.validator_for(_SETTINGS_SCHEMA)
validator_cls(_SETTINGS_SCHEMA).validate(result)