| 1 |
import time |
| 2 |
from types import ModuleType |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
|
| 7 |
def test_ulid_now(impl): |
| 8 |
ulid_str = impl.ulid_now() |
| 9 |
assert len(ulid_str) == 26 |
| 10 |
timestamp = impl.ulid_to_timestamp(ulid_str) |
| 11 |
assert timestamp == pytest.approx(int(time.time() * 1000), 1) |
| 12 |
|
| 13 |
|
| 14 |
def test_ulid_now_bytes(impl): |
| 15 |
ulid_bytes = impl.ulid_now_bytes() |
| 16 |
assert len(ulid_bytes) == 16 |
| 17 |
timestamp = impl.ulid_to_timestamp(ulid_bytes) |
| 18 |
assert timestamp == pytest.approx(int(time.time() * 1000), 1) |
| 19 |
|
| 20 |
|
| 21 |
def test_ulid_hex(impl): |
| 22 |
ulid_str = impl.ulid_hex() |
| 23 |
assert len(ulid_str) == 32 |
| 24 |
|
| 25 |
|
| 26 |
def test_ulid_to_bytes(impl): |
| 27 |
assert ( |
| 28 |
impl.ulid_to_bytes("01GTCKZT7K26YEVVW6AMQ3J0VT") |
| 29 |
== b"\x01\x86\x99?\xe8\xf3\x11\xbc\xed\xef\x86U.9\x03z" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def test_ulid_to_bytes_overflow(impl): |
| 34 |
with pytest.raises(ValueError, match="26 character"): |
| 35 |
impl.ulid_to_bytes("01GTCKZT7K26YEVVW6AMQ3J0VT0000") |
| 36 |
|
| 37 |
|
| 38 |
def test_ulid_to_bytes_under_low(impl): |
| 39 |
with pytest.raises(ValueError, match="26 character"): |
| 40 |
impl.ulid_to_bytes("01") |
| 41 |
|
| 42 |
|
| 43 |
def test_bytes_to_ulid(impl): |
| 44 |
assert ( |
| 45 |
impl.bytes_to_ulid(b"\x01\x86\x99?\xe8\xf3\x11\xbc\xed\xef\x86U.9\x03z") |
| 46 |
== "01GTCKZT7K26YEVVW6AMQ3J0VT" |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
def test_ulid_to_bytes_invalid_length(impl): |
| 51 |
with pytest.raises(ValueError, match="26 character"): |
| 52 |
assert impl.ulid_to_bytes("aa") |
| 53 |
|
| 54 |
|
| 55 |
def test_bytes_to_ulid_invalid_length(impl): |
| 56 |
with pytest.raises(ValueError, match="aa"): |
| 57 |
assert impl.bytes_to_ulid(b"aa") |
| 58 |
|
| 59 |
|
| 60 |
def test_ulid_to_bytes_2(impl): |
| 61 |
assert ( |
| 62 |
impl.ulid_to_bytes("00000000AC00GW0X476W5TVBFE") |
| 63 |
== b"\x00\x00\x00\x00\x01L\x00!\xc0t\x877\x0b\xad\xad\xee" |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
def test_timestamp_string(impl): |
| 68 |
ulid = impl.ulid_at_time(1677627631.2127638) |
| 69 |
assert ulid[:10] == "01GTD6C9KC" |
| 70 |
|
| 71 |
|
| 72 |
def test_timestamp_bytes(impl): |
| 73 |
ulid = impl.ulid_at_time_bytes(1677627631.2127638) |
| 74 |
|
| 75 |
assert ulid[:6] == b"\x01\x86\x9af&l" |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 79 |
def test_timestamp(impl, gen): |
| 80 |
gen = getattr(impl, gen) |
| 81 |
now = time.time() |
| 82 |
ulid = gen(now) |
| 83 |
|
| 84 |
assert impl.ulid_to_timestamp(ulid) == int(now * 1000) |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 88 |
def test_timestamp_fixed(impl, gen): |
| 89 |
gen = getattr(impl, gen) |
| 90 |
now = 1677627631.2127638 |
| 91 |
ulid = gen(now) |
| 92 |
|
| 93 |
assert impl.ulid_to_timestamp(ulid) == int(now * 1000) |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 97 |
def test_ulid_at_time_nan_raises(impl, gen): |
| 98 |
with pytest.raises(ValueError, match="NaN"): |
| 99 |
getattr(impl, gen)(float("nan")) |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 103 |
@pytest.mark.parametrize("value", [float("inf"), float("-inf")]) |
| 104 |
def test_ulid_at_time_infinity_raises(impl, gen, value): |
| 105 |
with pytest.raises(OverflowError, match="infinity"): |
| 106 |
getattr(impl, gen)(value) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 110 |
@pytest.mark.parametrize( |
| 111 |
"value", |
| 112 |
[ |
| 113 |
-1.0, |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
-0.0009, |
| 119 |
-0.0005, |
| 120 |
-0.0001, |
| 121 |
-1e-9, |
| 122 |
], |
| 123 |
) |
| 124 |
def test_ulid_at_time_negative_raises(impl, gen, value): |
| 125 |
with pytest.raises(OverflowError, match="negative"): |
| 126 |
getattr(impl, gen)(value) |
| 127 |
|
| 128 |
|
| 129 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 130 |
def test_ulid_at_time_too_large_raises(impl, gen): |
| 131 |
s. |
| 132 |
with pytest.raises(OverflowError, match="int too big"): |
| 133 |
getattr(impl, gen)(1e18) |
| 134 |
|
| 135 |
|
| 136 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 137 |
def test_ulid_at_time_max_valid(impl, gen): |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
ts = 281474976710.000 |
| 142 |
ulid = getattr(impl, gen)(ts) |
| 143 |
assert ulid is not None |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.parametrize("gen", ["ulid_at_time", "ulid_at_time_bytes"]) |
| 147 |
def test_ulid_at_time_wrong_type_raises(impl, gen): |
| 148 |
with pytest.raises(TypeError): |
| 149 |
getattr(impl, gen)("not-a-float") |
| 150 |
|
| 151 |
|
| 152 |
def test_non_uppercase_b32_data(impl): |
| 153 |
assert len(impl.ulid_to_bytes("not_uppercase_b32_data_:::")) == 16 |
| 154 |
|
| 155 |
|
| 156 |
def test_ulid_to_bytes_or_none(impl): |
| 157 |
"""Test ulid_to_bytes_or_none.""" |
| 158 |
assert ( |
| 159 |
impl.ulid_to_bytes_or_none("01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1") |
| 160 |
== b"\x01w\xaf\xf9w\xe5\xf8~\x1f\x87\xe1\xf8~\x1f\x87\xe1" |
| 161 |
) |
| 162 |
assert impl.ulid_to_bytes_or_none("invalid") is None |
| 163 |
assert impl.ulid_to_bytes_or_none(None) is None |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.parametrize( |
| 167 |
"value", |
| 168 |
[ |
| 169 |
123, |
| 170 |
3.14, |
| 171 |
[], |
| 172 |
(), |
| 173 |
{}, |
| 174 |
b"\x01w\xaf\xf9w\xe5\xf8~\x1f\x87\xe1\xf8~\x1f\x87\xe1", |
| 175 |
object(), |
| 176 |
], |
| 177 |
) |
| 178 |
def test_ulid_to_bytes_or_none_invalid_type(impl, value): |
| 179 |
"""Non-str inputs return None instead of raising (parity with C impl).""" |
| 180 |
assert impl.ulid_to_bytes_or_none(value) is None |
| 181 |
|
| 182 |
|
| 183 |
def test_ulid_to_bytes_or_none_non_ascii(impl): |
| 184 |
"""Non-ASCII 26-char strings return None instead of raising.""" |
| 185 |
assert impl.ulid_to_bytes_or_none("é" * 26) is None |
| 186 |
|
| 187 |
|
| 188 |
def test_entropy(impl): |
| 189 |
"""Verify generated ULIDs have non-trivial entropy in the random part.""" |
| 190 |
samples = [impl.ulid_at_time_bytes(1677627631.0)[6:] for _ in range(200)] |
| 191 |
assert len(set(samples)) == len(samples), ( |
| 192 |
"Generated ULIDs should have unique entropy" |
| 193 |
) |
| 194 |
|
| 195 |
for pos in range(10): |
| 196 |
distinct_bytes = {s[pos] for s in samples} |
| 197 |
assert len(distinct_bytes) > 1, f"Byte position {pos} has no variety" |
| 198 |
|
| 199 |
for pos in range(20): |
| 200 |
byte_idx, shift = divmod(pos, 2) |
| 201 |
distinct_nibbles = {(s[byte_idx] >> (4 * shift)) & 0xF for s in samples} |
| 202 |
assert len(distinct_nibbles) > 1, f"Nibble position {pos} has no variety" |
| 203 |
|
| 204 |
|
| 205 |
def test_ulid_to_timestamp_wrong_length_bytes(impl: ModuleType) -> None: |
| 206 |
"""Bytes input with the wrong length must raise ValueError, not silently truncate.""" |
| 207 |
with pytest.raises(ValueError, match="16 bytes"): |
| 208 |
impl.ulid_to_timestamp(b"short") |
| 209 |
with pytest.raises(ValueError, match="16 bytes"): |
| 210 |
impl.ulid_to_timestamp(b"") |
| 211 |
with pytest.raises(ValueError, match="16 bytes"): |
| 212 |
impl.ulid_to_timestamp(b"a" * 50) |
| 213 |
|
| 214 |
|
| 215 |
def test_ulid_to_timestamp_wrong_length_string(impl: ModuleType) -> None: |
| 216 |
"""String input with the wrong length must raise ValueError.""" |
| 217 |
with pytest.raises(ValueError, match="26 character"): |
| 218 |
impl.ulid_to_timestamp("too short") |
| 219 |
with pytest.raises(ValueError, match="26 character"): |
| 220 |
impl.ulid_to_timestamp("01GTCKZT7K26YEVVW6AMQ3J0VT0000") |
| 221 |
|
| 222 |
|
| 223 |
def test_ulid_to_timestamp_wrong_type(impl: ModuleType) -> None: |
| 224 |
"""Non-str / non-bytes input must raise TypeError.""" |
| 225 |
with pytest.raises(TypeError): |
| 226 |
impl.ulid_to_timestamp(123) |
| 227 |
with pytest.raises(TypeError): |
| 228 |
impl.ulid_to_timestamp(None) |
| 229 |
with pytest.raises(TypeError): |
| 230 |
impl.ulid_to_timestamp( |
| 231 |
bytearray(b"\x01\x86\x99?\xe8\xf3\x11\xbc\xed\xef\x86U.9\x03z") |
| 232 |
) |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.parametrize( |
| 236 |
"value", |
| 237 |
[ |
| 238 |
b"01GTCKZT7K26YEVVW6AMQ3J0VT", |
| 239 |
bytearray(b"01GTCKZT7K26YEVVW6AMQ3J0VT"), |
| 240 |
memoryview(b"01GTCKZT7K26YEVVW6AMQ3J0VT"), |
| 241 |
123, |
| 242 |
None, |
| 243 |
3.14, |
| 244 |
], |
| 245 |
) |
| 246 |
def test_ulid_to_bytes_wrong_type(impl, value): |
| 247 |
"""Non-str input must raise TypeError (parity with C impl).""" |
| 248 |
with pytest.raises(TypeError): |
| 249 |
impl.ulid_to_bytes(value) |
| 250 |
|
| 251 |
|
| 252 |
@pytest.mark.parametrize( |
| 253 |
"value", |
| 254 |
[ |
| 255 |
bytearray(16), |
| 256 |
memoryview(b"x" * 16), |
| 257 |
"01GTCKZT7K26YEVVW6AMQ3J0VT", |
| 258 |
123, |
| 259 |
None, |
| 260 |
], |
| 261 |
) |
| 262 |
def test_bytes_to_ulid_wrong_type(impl, value): |
| 263 |
"""Non-bytes input must raise TypeError (parity with C impl).""" |
| 264 |
with pytest.raises(TypeError): |
| 265 |
impl.bytes_to_ulid(value) |
| 266 |
|
| 267 |
|
| 268 |
def test_bytes_to_ulid_or_none(impl): |
| 269 |
"""Test bytes_to_ulid_or_none.""" |
| 270 |
assert ( |
| 271 |
impl.bytes_to_ulid_or_none( |
| 272 |
b"\x01w\xaf\xf9w\xe5\xf8~\x1f\x87\xe1\xf8~\x1f\x87\xe1" |
| 273 |
) |
| 274 |
== "01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1" |
| 275 |
) |
| 276 |
assert impl.bytes_to_ulid_or_none(b"invalid") is None |
| 277 |
assert impl.bytes_to_ulid_or_none(None) is None |
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.parametrize( |
| 281 |
"value", |
| 282 |
[ |
| 283 |
123, |
| 284 |
3.14, |
| 285 |
[], |
| 286 |
(), |
| 287 |
{}, |
| 288 |
"01EYQZJXZ5Z1Z1Z1Z1Z1Z1Z1Z1", |
| 289 |
object(), |
| 290 |
], |
| 291 |
) |
| 292 |
def test_bytes_to_ulid_or_none_invalid_type(impl, value): |
| 293 |
"""Non-bytes inputs return None instead of raising (parity with C impl).""" |
| 294 |
assert impl.bytes_to_ulid_or_none(value) is None |
| 295 |
|
| 296 |
|
| 297 |
@pytest.mark.parametrize( |
| 298 |
"value", |
| 299 |
["", "short", "01GTCKZT7K26YEVVW6AMQ3J0VT ", "01GTCKZT7K26YEVVW6AMQ3J0VT0000"], |
| 300 |
) |
| 301 |
def test_ulid_to_bytes_error_message_reprs_value(impl: ModuleType, value: str) -> None: |
| 302 |
"""The wrong-length ValueError message repr-quotes the offending value. |
| 303 |
|
| 304 |
Both impls must embed repr(value) (the C extension uses PyErr_Format's %R), |
| 305 |
so a trailing space or empty string is unambiguous in the message and the |
| 306 |
two implementations stay byte-for-byte aligned. |
| 307 |
""" |
| 308 |
with pytest.raises(ValueError, match="26 character") as exc_info: |
| 309 |
impl.ulid_to_bytes(value) |
| 310 |
assert repr(value) in str(exc_info.value) |
| 311 |
|
| 312 |
|
| 313 |
@pytest.mark.parametrize( |
| 314 |
"value", |
| 315 |
["", "short", "01GTCKZT7K26YEVVW6AMQ3J0VT ", "01GTCKZT7K26YEVVW6AMQ3J0VT0000"], |
| 316 |
) |
| 317 |
def test_ulid_to_timestamp_error_message_reprs_value( |
| 318 |
impl: ModuleType, value: str |
| 319 |
) -> None: |
| 320 |
"""ulid_to_timestamp's string path inherits the repr-quoted ValueError. |
| 321 |
|
| 322 |
It delegates to ulid_to_bytes, so the wrong-length message must embed |
| 323 |
repr(value) on both impls just like the ulid_to_bytes path. |
| 324 |
""" |
| 325 |
with pytest.raises(ValueError, match="26 character") as exc_info: |
| 326 |
impl.ulid_to_timestamp(value) |
| 327 |
assert repr(value) in str(exc_info.value) |