summaryrefslogtreecommitdiff
path: root/tests/db_functions
diff options
context:
space:
mode:
authorLily Acorn <code@lilyf.org>2025-11-20 20:10:13 +0000
committerJacob Walls <jacobtylerwalls@gmail.com>2025-12-12 11:50:36 -0500
commite95468ed97b1c250939da316218c39dad577ba32 (patch)
treec4059a0fa69e071b8d87d918cf01791d7f381af1 /tests/db_functions
parentaccceec9493d08e19d59fa1a59f69c0fdf23bb13 (diff)
Refs #36735 -- Supported shift parameter for UUID7 on PostgreSQL.
Diffstat (limited to 'tests/db_functions')
-rw-r--r--tests/db_functions/migrations/0002_create_test_models.py1
-rw-r--r--tests/db_functions/models.py1
-rw-r--r--tests/db_functions/test_uuid.py27
3 files changed, 29 insertions, 0 deletions
diff --git a/tests/db_functions/migrations/0002_create_test_models.py b/tests/db_functions/migrations/0002_create_test_models.py
index 32fa83b910..6c4626e7ea 100644
--- a/tests/db_functions/migrations/0002_create_test_models.py
+++ b/tests/db_functions/migrations/0002_create_test_models.py
@@ -92,6 +92,7 @@ class Migration(migrations.Migration):
name="UUIDModel",
fields=[
("uuid", models.UUIDField(null=True)),
+ ("shift", models.DurationField(null=True)),
],
),
]
diff --git a/tests/db_functions/models.py b/tests/db_functions/models.py
index 4985b4bfca..45a5a027f4 100644
--- a/tests/db_functions/models.py
+++ b/tests/db_functions/models.py
@@ -59,3 +59,4 @@ class FloatModel(models.Model):
class UUIDModel(models.Model):
uuid = models.UUIDField(null=True)
+ shift = models.DurationField(null=True)
diff --git a/tests/db_functions/test_uuid.py b/tests/db_functions/test_uuid.py
index b595aafc1b..4071071d2d 100644
--- a/tests/db_functions/test_uuid.py
+++ b/tests/db_functions/test_uuid.py
@@ -1,4 +1,5 @@
import uuid
+from datetime import datetime, timedelta, timezone
from django.db import NotSupportedError, connection
from django.db.models.functions import UUID4, UUID7
@@ -31,6 +32,24 @@ class TestUUID(TestCase):
self.assertEqual(m1.uuid.version, 7)
self.assertNotEqual(m1.uuid, m2.uuid)
+ @skipUnlessDBFeature("supports_uuid7_function_shift")
+ def test_uuid7_shift(self):
+ now = datetime.now(timezone.utc)
+ past = datetime(2005, 11, 16, tzinfo=timezone.utc)
+ shift = past - now
+ m = UUIDModel.objects.create(uuid=UUID7(shift))
+ self.assertTrue(str(m.uuid).startswith("0107965e-e40"), m.uuid)
+
+ @skipUnlessDBFeature("supports_uuid7_function_shift")
+ def test_uuid7_shift_duration_field(self):
+ now = datetime.now(timezone.utc)
+ past = datetime(2005, 11, 16, tzinfo=timezone.utc)
+ shift = past - now
+ m = UUIDModel.objects.create(shift=shift)
+ UUIDModel.objects.update(uuid=UUID7("shift"))
+ m.refresh_from_db()
+ self.assertTrue(str(m.uuid).startswith("0107965e-e40"), m.uuid)
+
@skipIfDBFeature("supports_uuid4_function")
def test_uuid4_unsupported(self):
if connection.vendor == "mysql":
@@ -62,3 +81,11 @@ class TestUUID(TestCase):
with self.assertRaisesMessage(NotSupportedError, msg):
UUIDModel.objects.update(uuid=UUID7())
+
+ @skipUnlessDBFeature("supports_uuid7_function")
+ @skipIfDBFeature("supports_uuid7_function_shift")
+ def test_uuid7_shift_unsupported(self):
+ msg = "The shift argument to UUID7 is not supported on this database backend."
+
+ with self.assertRaisesMessage(NotSupportedError, msg):
+ UUIDModel.objects.update(uuid=UUID7(shift=timedelta(hours=12)))