diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-12-18 10:45:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-18 10:45:13 (GMT) |
commit | 62a68b762a479a72c3defba9ace5f72a0063c5c6 (patch) | |
tree | dcadb35bdadeaae4cd99842e65e2a3bf412336ec /Lib/test | |
parent | 1dd035954bb03c41b954ebbd63969b4bcb0e106e (diff) | |
download | cpython-62a68b762a479a72c3defba9ace5f72a0063c5c6.zip cpython-62a68b762a479a72c3defba9ace5f72a0063c5c6.tar.gz cpython-62a68b762a479a72c3defba9ace5f72a0063c5c6.tar.bz2 |
bpo-31784: Use time.time_ns() in uuid.uuid1() (GH-11189)
uuid.uuid1() now calls time.time_ns() rather than
int(time.time() * 1e9). Replace also int(nanoseconds/100)
with nanoseconds // 100. Add an unit test.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_uuid.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py index dc502b9..757bf3c 100644 --- a/Lib/test/test_uuid.py +++ b/Lib/test/test_uuid.py @@ -9,6 +9,7 @@ import pickle import shutil import subprocess import sys +from unittest import mock py_uuid = support.import_fresh_module('uuid', blocked=['_uuid']) c_uuid = support.import_fresh_module('uuid', fresh=['_uuid']) @@ -567,6 +568,23 @@ class BaseTestUUID: u = self.uuid.uuid1() self.assertEqual(u.is_safe, self.uuid.SafeUUID.unknown) + def test_uuid1_time(self): + with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \ + mock.patch.object(self.uuid, '_generate_time_safe', None), \ + mock.patch.object(self.uuid, '_last_timestamp', None), \ + mock.patch.object(self.uuid, 'getnode', return_value=93328246233727), \ + mock.patch('time.time_ns', return_value=1545052026752910643), \ + mock.patch('random.getrandbits', return_value=5317): # guaranteed to be random + u = self.uuid.uuid1() + self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) + + with mock.patch.object(self.uuid, '_has_uuid_generate_time_safe', False), \ + mock.patch.object(self.uuid, '_generate_time_safe', None), \ + mock.patch.object(self.uuid, '_last_timestamp', None), \ + mock.patch('time.time_ns', return_value=1545052026752910643): + u = self.uuid.uuid1(node=93328246233727, clock_seq=5317) + self.assertEqual(u, self.uuid.UUID('a7a55b92-01fc-11e9-94c5-54e1acf6da7f')) + def test_uuid3(self): equal = self.assertEqual |