MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_uuid7_timestamp_backwards

Method test_uuid7_timestamp_backwards

Lib/test/test_uuid.py:1001–1039  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

999 self.assertLess(u1, u2)
1000
1001 def test_uuid7_timestamp_backwards(self):
1002 equal = self.assertEqual
1003 # 1 Jan 2023 12:34:56.123_456_789
1004 timestamp_ns = 1672533296_123_456_789 # ns precision
1005 timestamp_ms, _ = divmod(timestamp_ns, 1_000_000)
1006 fake_last_timestamp_v7 = timestamp_ms + 1
1007
1008 # counter_{hi,lo} are chosen so that "counter + 1" does not overflow
1009 counter_hi = random.getrandbits(11)
1010 counter_lo = random.getrandbits(29)
1011 counter = (counter_hi << 30) | counter_lo
1012 self.assertLess(counter + 1, 0x3ff_ffff_ffff)
1013
1014 tail_bytes = os.urandom(4)
1015 tail = int.from_bytes(tail_bytes)
1016
1017 with (
1018 mock.patch.multiple(
1019 self.uuid,
1020 _last_timestamp_v7=fake_last_timestamp_v7,
1021 _last_counter_v7=counter,
1022 ),
1023 mock.patch('time.time_ns', return_value=timestamp_ns),
1024 mock.patch('os.urandom', return_value=tail_bytes) as urand
1025 ):
1026 u = self.uuid.uuid7()
1027 urand.assert_called_once_with(4)
1028 equal(u.variant, self.uuid.RFC_4122)
1029 equal(u.version, 7)
1030 equal(self.uuid._last_timestamp_v7, fake_last_timestamp_v7 + 1)
1031 unix_ts_ms = (fake_last_timestamp_v7 + 1) & 0xffff_ffff_ffff
1032 equal(u.time, unix_ts_ms)
1033 equal((u.int >> 80) & 0xffff_ffff_ffff, unix_ts_ms)
1034 # 42-bit counter advanced by 1
1035 equal(self.uuid._last_counter_v7, counter + 1)
1036 equal((u.int >> 64) & 0xfff, counter_hi)
1037 # 42-bit counter advanced by 1 (counter_hi is untouched)
1038 equal((u.int >> 32) & 0x3fff_ffff, counter_lo + 1)
1039 equal(u.int & 0xffff_ffff, tail)
1040
1041 def test_uuid7_overflow_counter(self):
1042 equal = self.assertEqual

Callers

nothing calls this directly

Calls 5

divmodFunction · 0.50
getrandbitsMethod · 0.45
assertLessMethod · 0.45
from_bytesMethod · 0.45

Tested by

no test coverage detected