Debian Codegraph @

tests/test_impl_sync.py

raw symbols SCIP
1 from inspect import getdoc, signature
2 import os
3 import random
4
5 import pytest
6
7 import ulid_transform
8
9 try:
10 import ulid_transform._ulid_impl as _c_impl
11 except ImportError: # pragma: no cover - exercised when C ext is unavailable
12 _c_impl = None
13 import ulid_transform._py_ulid_impl as _py_impl
14
15 _BOTH_IMPLS_AVAILABLE = _c_impl is not None
16
17
18 def get_signature_string(func):
19 """Get a comparable signature string from a function."""
20 sig = signature(func)
21 # Strip type annotations and positional-only markers for comparison
22 # since C extensions cannot express them via __text_signature__.
23 params = [
24 p.replace(annotation=p.empty, kind=p.POSITIONAL_OR_KEYWORD)
25 for p in sig.parameters.values()
26 ]
27 sig = sig.replace(parameters=params, return_annotation=sig.empty)
28 return repr(sig).replace("'", "")
29
30
31 def test_impl_exports_required_keys(impl):
32 """Test all implementations export the public API exposed by ulid_transform."""
33 assert set(ulid_transform.__all__) <= set(dir(impl)), (
34 f"{impl} does not match ulid_transform.__all__"
35 )
36
37
38 def test_impls_in_sync(impl):
39 """Test implementations are in sync with the python implementation (docstrings and signatures)."""
40 import ulid_transform._py_ulid_impl as python_impl # noqa: PLC0415
41
42 for key in ulid_transform.__all__:
43 py_func = getattr(python_impl, key)
44 impl_func = getattr(impl, key)
45 assert getdoc(py_func) == getdoc(impl_func)
46 assert get_signature_string(py_func) == get_signature_string(impl_func)
47
48
49 # --- Behavioral parity: C and Python must produce byte-identical results ---
50
51
52 pytestmark_parity = pytest.mark.skipif(
53 not _BOTH_IMPLS_AVAILABLE,
54 reason="C extension not available; cross-impl parity cannot be checked",
55 )
56
57
58 @pytestmark_parity
59 def test_parity_bytes_to_ulid_random():
60 rng = random.Random(0xC0DE)
61 for _ in range(200):
62 b = bytes(rng.getrandbits(8) for _ in range(16))
63 assert _c_impl.bytes_to_ulid(b) == _py_impl.bytes_to_ulid(b)
64
65
66 @pytestmark_parity
67 def test_parity_ulid_to_bytes_round_trip():
68 """Encoding then decoding through either impl yields the same bytes."""
69 for _ in range(200):
70 b = os.urandom(16)
71 s = _py_impl.bytes_to_ulid(b)
72 assert _c_impl.ulid_to_bytes(s) == _py_impl.ulid_to_bytes(s) == b
73
74
75 @pytestmark_parity
76 def test_parity_ulid_to_bytes_mixed_case():
77 """C and Python decode mixed-case ULIDs identically (Crockford is case-insensitive)."""
78 rng = random.Random(0xBEEF)
79 for _ in range(100):
80 b = os.urandom(16)
81 s = _py_impl.bytes_to_ulid(b)
82 mixed = "".join(ch.lower() if rng.random() < 0.5 else ch for ch in s)
83 assert _c_impl.ulid_to_bytes(mixed) == _py_impl.ulid_to_bytes(mixed)
84
85
86 @pytestmark_parity
87 @pytest.mark.parametrize(
88 ("alias_char", "canonical"),
89 [
90 ("I", "1"),
91 ("i", "1"),
92 ("L", "1"),
93 ("l", "1"),
94 ("O", "0"),
95 ("o", "0"),
96 ],
97 )
98 def test_parity_crockford_aliases(alias_char, canonical):
99 """I/L (any case) -> 1, O (any case) -> 0; both impls must agree."""
100 base = "01GTCKZT7K26YEVVW6AMQ3J0VT"
101 aliased = alias_char + base[1:]
102 assert _c_impl.ulid_to_bytes(aliased) == _py_impl.ulid_to_bytes(aliased)
103
104
105 @pytestmark_parity
106 def test_parity_ulid_at_time_timestamp_portion():
107 """First 10 chars (timestamp) of ulid_at_time must match across impls."""
108 for ts in (0.0, 1.0, 1677627631.2127638, 9999999999.999):
109 assert _c_impl.ulid_at_time(ts)[:10] == _py_impl.ulid_at_time(ts)[:10]
110 assert _c_impl.ulid_at_time_bytes(ts)[:6] == _py_impl.ulid_at_time_bytes(ts)[:6]
111
112
113 @pytestmark_parity
114 def test_parity_ulid_hex_format():
115 """ulid_hex output format (length, hex chars) matches across impls."""
116 for _ in range(10):
117 for h in (_c_impl.ulid_hex(), _py_impl.ulid_hex()):
118 assert len(h) == 32
119 assert all(ch in "0123456789abcdef" for ch in h)
120
121
122 @pytestmark_parity
123 def test_parity_ulid_to_timestamp_valid_string():
124 for s in ("01GTCKZT7K26YEVVW6AMQ3J0VT", "00000000000000000000000000"):
125 assert _c_impl.ulid_to_timestamp(s) == _py_impl.ulid_to_timestamp(s)
126
127
128 @pytestmark_parity
129 def test_parity_or_none_valid_inputs():
130 s = "01GTCKZT7K26YEVVW6AMQ3J0VT"
131 b = bytes(range(16))
132 assert _c_impl.ulid_to_bytes_or_none(s) == _py_impl.ulid_to_bytes_or_none(s)
133 assert _c_impl.ulid_to_bytes_or_none(None) == _py_impl.ulid_to_bytes_or_none(None)
134 assert _c_impl.bytes_to_ulid_or_none(b) == _py_impl.bytes_to_ulid_or_none(b)
135 assert _c_impl.bytes_to_ulid_or_none(None) == _py_impl.bytes_to_ulid_or_none(None)
136
137
138 @pytestmark_parity
139 def test_parity_or_none_invalid_lengths():
140 """_or_none variants return None for length-invalid (but type-correct) input."""
141 assert _c_impl.ulid_to_bytes_or_none("short") == _py_impl.ulid_to_bytes_or_none(
142 "short"
143 )
144 assert _c_impl.bytes_to_ulid_or_none(b"short") == _py_impl.bytes_to_ulid_or_none(
145 b"short"
146 )