Skip to content

Commit a3a51ce

Browse files
committed
add tests for UUIDv7 history_id
Signed-off-by: Benjamin Skov Kaas-Hansen <[email protected]>
1 parent 73f4f1a commit a3a51ce

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

simple_history/tests/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,18 @@ class UUIDDefaultModel(models.Model):
797797
history = HistoricalRecords()
798798

799799

800+
# Set the SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION
801+
setattr(settings, "SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION", 7)
802+
803+
804+
class UUIDv7Model(models.Model):
805+
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
806+
history = HistoricalRecords()
807+
808+
809+
# Clear the SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION
810+
delattr(settings, "SIMPLE_HISTORY_HISTORY_ID_UUID_VERSION")
811+
800812
# Clear the SIMPLE_HISTORY_HISTORY_ID_USE_UUID
801813
delattr(settings, "SIMPLE_HISTORY_HISTORY_ID_USE_UUID")
802814

simple_history/tests/tests/test_models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import time
23
import unittest
34
import uuid
45
import warnings
@@ -122,6 +123,7 @@
122123
UserTextFieldChangeReasonModel,
123124
UUIDDefaultModel,
124125
UUIDModel,
126+
UUIDv7Model,
125127
WaterLevel,
126128
)
127129
from .utils import (
@@ -637,6 +639,21 @@ def test_uuid_default_history_id(self):
637639
history = entry.history.all()[0]
638640
self.assertTrue(isinstance(history.history_id, uuid.UUID))
639641

642+
def test_uuidv7_history_id(self):
643+
"""
644+
Tests that UUIDv7 is being used for history_id when defined in the settings,
645+
and that consequent PKs are ordered based on creation time.
646+
"""
647+
entries = []
648+
for _ in range(10):
649+
entries.append(UUIDv7Model.objects.create())
650+
time.sleep(0.01) # Ensure increasing time part of UUIDv7 PKs
651+
652+
history = [entry.history.all()[0] for entry in entries]
653+
self.assertTrue(all(isinstance(h.history_id, uuid.UUID) for h in history))
654+
for h1, h2 in zip(history[:-1], history[1:]):
655+
self.assertLess(h1.history_id, h2.history_id)
656+
640657
def test_default_history_change_reason(self):
641658
entry = CharFieldChangeReasonModel.objects.create(greeting="what's up?")
642659
history = entry.history.get()

0 commit comments

Comments
 (0)