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

Method test_uuid7

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

Source from the content-addressed store, hash-verified

873 equal((u.int >> 96) & 0xffff_ffff, 0x1ec9_414c)
874
875 def test_uuid7(self):
876 equal = self.assertEqual
877 u = self.uuid.uuid7()
878 equal(u.variant, self.uuid.RFC_4122)
879 equal(u.version, 7)
880
881 # 1 Jan 2023 12:34:56.123_456_789
882 timestamp_ns = 1672533296_123_456_789 # ns precision
883 timestamp_ms, _ = divmod(timestamp_ns, 1_000_000)
884
885 for _ in range(100):
886 counter_hi = random.getrandbits(11)
887 counter_lo = random.getrandbits(30)
888 counter = (counter_hi << 30) | counter_lo
889
890 tail = random.getrandbits(32)
891 # effective number of bits is 32 + 30 + 11 = 73
892 random_bits = counter << 32 | tail
893
894 # set all remaining MSB of fake random bits to 1 to ensure that
895 # the implementation correctly removes them
896 random_bits = (((1 << 7) - 1) << 73) | random_bits
897 random_data = random_bits.to_bytes(10)
898
899 with (
900 mock.patch.multiple(
901 self.uuid,
902 _last_timestamp_v7=None,
903 _last_counter_v7=0,
904 ),
905 mock.patch('time.time_ns', return_value=timestamp_ns),
906 mock.patch('os.urandom', return_value=random_data) as urand
907 ):
908 u = self.uuid.uuid7()
909 urand.assert_called_once_with(10)
910 equal(u.variant, self.uuid.RFC_4122)
911 equal(u.version, 7)
912
913 equal(self.uuid._last_timestamp_v7, timestamp_ms)
914 equal(self.uuid._last_counter_v7, counter)
915
916 unix_ts_ms = timestamp_ms & 0xffff_ffff_ffff
917 equal(u.time, unix_ts_ms)
918 equal((u.int >> 80) & 0xffff_ffff_ffff, unix_ts_ms)
919
920 equal((u.int >> 75) & 1, 0) # check that the MSB is 0
921 equal((u.int >> 64) & 0xfff, counter_hi)
922 equal((u.int >> 32) & 0x3fff_ffff, counter_lo)
923 equal(u.int & 0xffff_ffff, tail)
924
925 def test_uuid7_uniqueness(self):
926 # Test that UUIDv7-generated values are unique.

Callers

nothing calls this directly

Calls 4

to_bytesMethod · 0.80
divmodFunction · 0.50
getrandbitsMethod · 0.45

Tested by

no test coverage detected