Files
wehub-resource-sync 9201ef759e
CI / lint (push) Waiting to run
CI / mypy (push) Waiting to run
CI / docs (push) Waiting to run
CI / test on 3.10 (standard) (push) Waiting to run
CI / test on 3.11 (standard) (push) Waiting to run
CI / test on 3.12 (standard) (push) Waiting to run
CI / test on 3.10 (all-extras) (push) Waiting to run
CI / test on 3.11 (all-extras) (push) Waiting to run
CI / test on 3.12 (all-extras) (push) Waiting to run
CI / test on 3.13 (all-extras) (push) Waiting to run
CI / test on 3.14 (pydantic-evals) (push) Waiting to run
Harness Compat / harness compat (push) Waiting to run
CI / test on 3.13 (standard) (push) Waiting to run
CI / test on 3.14 (standard) (push) Waiting to run
CI / test on 3.14 (all-extras) (push) Waiting to run
CI / test on 3.10 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.11 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.12 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.13 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.14 (pydantic-ai-slim) (push) Waiting to run
CI / test on 3.10 (pydantic-evals) (push) Waiting to run
CI / test on 3.11 (pydantic-evals) (push) Waiting to run
CI / test on 3.12 (pydantic-evals) (push) Waiting to run
CI / test on 3.13 (pydantic-evals) (push) Waiting to run
CI / test on 3.10 (lowest-versions) (push) Waiting to run
CI / test on 3.11 (lowest-versions) (push) Waiting to run
CI / test on 3.12 (lowest-versions) (push) Waiting to run
CI / test on 3.13 (lowest-versions) (push) Waiting to run
CI / test on 3.14 (lowest-versions) (push) Waiting to run
CI / test examples on 3.11 (push) Waiting to run
CI / test examples on 3.12 (push) Waiting to run
CI / test examples on 3.13 (push) Waiting to run
CI / test examples on 3.14 (push) Waiting to run
CI / coverage (push) Blocked by required conditions
CI / check (push) Blocked by required conditions
CI / deploy-docs (push) Blocked by required conditions
CI / deploy-docs-preview (push) Blocked by required conditions
CI / build release artifacts (push) Blocked by required conditions
CI / publish to PyPI (push) Blocked by required conditions
CI / Send tweet (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:27:52 +08:00

62 lines
2.3 KiB
Python

"""Tests for the UUIDv7 polyfill."""
from unittest.mock import patch
import pydantic_ai._uuid as uuid_mod
from pydantic_ai._uuid import uuid7
def test_uuid7_basic() -> None:
u = uuid7()
assert u.version == 7
assert u.variant == 'specified in RFC 4122'
assert len(str(u)) == 36
def test_uuid7_monotonic_within_millisecond() -> None:
"""Multiple uuid7() calls in the same millisecond produce monotonically increasing values."""
ids = [uuid7() for _ in range(100)]
for a, b in zip(ids, ids[1:]):
assert a < b
def test_uuid7_clock_regression() -> None:
"""uuid7() handles system clock going backward (e.g. NTP adjustment)."""
saved_ts = uuid_mod._last_timestamp_v7 # pyright: ignore[reportPrivateUsage]
saved_counter = uuid_mod._last_counter_v7 # pyright: ignore[reportPrivateUsage]
try:
# Generate a UUID at a known timestamp
with patch.object(uuid_mod.time, 'time_ns', return_value=2_000 * 1_000_000_000):
u1 = uuid7()
# Clock goes backward
with patch.object(uuid_mod.time, 'time_ns', return_value=1_000 * 1_000_000_000):
u2 = uuid7()
assert u2 > u1
assert u2.version == 7
finally:
uuid_mod._last_timestamp_v7 = saved_ts # pyright: ignore[reportPrivateUsage]
uuid_mod._last_counter_v7 = saved_counter # pyright: ignore[reportPrivateUsage]
def test_uuid7_counter_overflow() -> None:
"""uuid7() advances the timestamp when the 42-bit counter overflows."""
saved_ts = uuid_mod._last_timestamp_v7 # pyright: ignore[reportPrivateUsage]
saved_counter = uuid_mod._last_counter_v7 # pyright: ignore[reportPrivateUsage]
try:
# Set counter to max so the next same-ms call overflows
with patch.object(uuid_mod.time, 'time_ns', return_value=3_000 * 1_000_000_000):
u1 = uuid7()
uuid_mod._last_counter_v7 = 0x3FF_FFFF_FFFF # pyright: ignore[reportPrivateUsage]
with patch.object(uuid_mod.time, 'time_ns', return_value=3_000 * 1_000_000_000):
u2 = uuid7()
assert u2 > u1
assert u2.version == 7
finally:
uuid_mod._last_timestamp_v7 = saved_ts # pyright: ignore[reportPrivateUsage]
uuid_mod._last_counter_v7 = saved_counter # pyright: ignore[reportPrivateUsage]