Skip to content

Commit 21a16f6

Browse files
felixxmjacobtylerwalls
authored andcommitted
Refs #36735 -- Adjusted UUID7 assertions for timezone shifts.
Thanks Simon Charette and Jacob Walls for reviews.
1 parent d43bd46 commit 21a16f6

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

tests/db_functions/test_uuid.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,27 @@ def test_uuid7(self):
3434

3535
@skipUnlessDBFeature("supports_uuid7_function_shift")
3636
def test_uuid7_shift(self):
37+
shift = timedelta(minutes=1)
3738
now = datetime.now(timezone.utc)
38-
past = datetime(2005, 11, 16, tzinfo=timezone.utc)
39-
shift = past - now
4039
m = UUIDModel.objects.create(uuid=UUID7(shift))
41-
self.assertTrue(str(m.uuid).startswith("0107965e-e40"), m.uuid)
40+
ts = int.from_bytes(m.uuid.bytes[:6])
41+
uuid_timestamp = datetime.fromtimestamp(ts / 1000, tz=timezone.utc)
42+
self.assertAlmostEqual(
43+
(uuid_timestamp - now).total_seconds(), shift.total_seconds(), places=1
44+
)
4245

4346
@skipUnlessDBFeature("supports_uuid7_function_shift")
4447
def test_uuid7_shift_duration_field(self):
45-
now = datetime.now(timezone.utc)
46-
past = datetime(2005, 11, 16, tzinfo=timezone.utc)
47-
shift = past - now
48+
shift = timedelta(minutes=1)
4849
m = UUIDModel.objects.create(shift=shift)
50+
now = datetime.now(timezone.utc)
4951
UUIDModel.objects.update(uuid=UUID7("shift"))
5052
m.refresh_from_db()
51-
self.assertTrue(str(m.uuid).startswith("0107965e-e40"), m.uuid)
53+
ts = int.from_bytes(m.uuid.bytes[:6])
54+
uuid_timestamp = datetime.fromtimestamp(ts / 1000, tz=timezone.utc)
55+
self.assertAlmostEqual(
56+
(uuid_timestamp - now).total_seconds(), shift.total_seconds(), places=1
57+
)
5258

5359
@skipIfDBFeature("supports_uuid4_function")
5460
def test_uuid4_unsupported(self):

0 commit comments

Comments
 (0)