printpub/tests/post/id_test.py

17 lines
543 B
Python

import unittest.mock
import printpub.post.id
def test_id_generation_has_unique_and_nonunique_part():
now_seconds = 1689031817.358745
with unittest.mock.patch("time.time", return_value=now_seconds):
id1 = printpub.post.id.generate()
id2 = printpub.post.id.generate()
time_mask = int("1" * 42 + "0" * (64 - 42), 2)
# The first 42 bits should be the time
assert id1 & time_mask == id2 & time_mask
# The last bits should be random
assert id1 & (~time_mask) != id2 & (~time_mask)