-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_governance_primitives.py
More file actions
100 lines (86 loc) · 3.17 KB
/
Copy pathtest_governance_primitives.py
File metadata and controls
100 lines (86 loc) · 3.17 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
"""Negative tests for shared governed-contract parsing primitives."""
import re
from collections.abc import Callable, Iterator
from contextlib import contextmanager
import pytest
from contextsafe.contract_validation import (
array_value,
boolean_value,
bounded_string,
date_value,
enum_string,
exact_keys,
host_value,
nullable_date_value,
object_value,
relative_path_value,
unique_strings,
)
from contextsafe.errors import ContextSafeError
@contextmanager
def _passthrough_context() -> Iterator[None]:
yield
@pytest.mark.parametrize(
("operation", "expected_code"),
[
(lambda: object_value([], "$"), "invalid_type"),
(lambda: object_value({1: "value"}, "$"), "invalid_type"),
(lambda: array_value({}, "$"), "invalid_type"),
(
lambda: exact_keys({"unexpected": True}, frozenset(), "$"),
"unknown_field",
),
(
lambda: exact_keys({}, frozenset({"required"}), "$"),
"missing_field",
),
(lambda: bounded_string(None, "$"), "invalid_string"),
(lambda: bounded_string("\ud800", "$"), "invalid_unicode"),
(
lambda: bounded_string("lower", "$", pattern=re.compile(r"^[A-Z]+$")),
"invalid_format",
),
(lambda: boolean_value(1, "$"), "invalid_type"),
(lambda: date_value("2026-02-30", "$"), "invalid_date"),
(
lambda: enum_string("unsupported", "$", frozenset({"supported"})),
"invalid_enum",
),
(lambda: relative_path_value(".", "$"), "invalid_path"),
(lambda: host_value("127.0.0.1", "$"), "invalid_host"),
(lambda: host_value("2130706433", "$"), "invalid_host"),
(lambda: host_value("127.1", "$"), "invalid_host"),
(lambda: host_value("0177.0.0.1", "$"), "invalid_host"),
(lambda: host_value("0x7f.0.0.1", "$"), "invalid_host"),
(lambda: host_value("::1", "$"), "invalid_host"),
(lambda: host_value("0:0:0:0:0:0:0:1", "$"), "invalid_host"),
(lambda: host_value("::ffff:127.0.0.1", "$"), "invalid_host"),
(
lambda: unique_strings(("same", "same"), "$", code="duplicate"),
"duplicate",
),
],
)
def test_contract_primitive_rejections(
operation: Callable[[], object], expected_code: str
) -> None:
with pytest.raises(ContextSafeError) as raised:
operation()
assert raised.value.code == expected_code
def test_optional_keys_nullable_dates_and_dns_hosts_are_accepted() -> None:
exact_keys(
{"required": True, "optional": True},
frozenset({"required"}),
"$",
optional=frozenset({"optional"}),
)
assert nullable_date_value(None, "$") is None
assert nullable_date_value("2026-07-13", "$").isoformat() == "2026-07-13"
assert host_value("staging.contextsafe.invalid", "$") == (
"staging.contextsafe.invalid"
)
def test_structured_error_preserves_itself_across_context_manager() -> None:
error = ContextSafeError("test_code", "$", "safe message")
with pytest.raises(ContextSafeError) as raised, _passthrough_context():
raise error
assert raised.value is error