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