diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/db_functions/migrations/0002_create_test_models.py | 6 | ||||
| -rw-r--r-- | tests/db_functions/models.py | 4 | ||||
| -rw-r--r-- | tests/db_functions/test_uuid.py | 64 |
3 files changed, 74 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 37ee93f92f..32fa83b910 100644 --- a/tests/db_functions/migrations/0002_create_test_models.py +++ b/tests/db_functions/migrations/0002_create_test_models.py @@ -88,4 +88,10 @@ class Migration(migrations.Migration): ("f2", models.FloatField(null=True, blank=True)), ], ), + migrations.CreateModel( + name="UUIDModel", + fields=[ + ("uuid", models.UUIDField(null=True)), + ], + ), ] diff --git a/tests/db_functions/models.py b/tests/db_functions/models.py index c126f1bae1..4985b4bfca 100644 --- a/tests/db_functions/models.py +++ b/tests/db_functions/models.py @@ -55,3 +55,7 @@ class IntegerModel(models.Model): class FloatModel(models.Model): f1 = models.FloatField(null=True, blank=True) f2 = models.FloatField(null=True, blank=True) + + +class UUIDModel(models.Model): + uuid = models.UUIDField(null=True) diff --git a/tests/db_functions/test_uuid.py b/tests/db_functions/test_uuid.py new file mode 100644 index 0000000000..b595aafc1b --- /dev/null +++ b/tests/db_functions/test_uuid.py @@ -0,0 +1,64 @@ +import uuid + +from django.db import NotSupportedError, connection +from django.db.models.functions import UUID4, UUID7 +from django.test import TestCase +from django.test.testcases import skipIfDBFeature, skipUnlessDBFeature + +from .models import UUIDModel + + +class TestUUID(TestCase): + @skipUnlessDBFeature("supports_uuid4_function") + def test_uuid4(self): + m1 = UUIDModel.objects.create() + m2 = UUIDModel.objects.create() + UUIDModel.objects.update(uuid=UUID4()) + m1.refresh_from_db() + m2.refresh_from_db() + self.assertIsInstance(m1.uuid, uuid.UUID) + self.assertEqual(m1.uuid.version, 4) + self.assertNotEqual(m1.uuid, m2.uuid) + + @skipUnlessDBFeature("supports_uuid7_function") + def test_uuid7(self): + m1 = UUIDModel.objects.create() + m2 = UUIDModel.objects.create() + UUIDModel.objects.update(uuid=UUID7()) + m1.refresh_from_db() + m2.refresh_from_db() + self.assertIsInstance(m1.uuid, uuid.UUID) + self.assertEqual(m1.uuid.version, 7) + self.assertNotEqual(m1.uuid, m2.uuid) + + @skipIfDBFeature("supports_uuid4_function") + def test_uuid4_unsupported(self): + if connection.vendor == "mysql": + if connection.mysql_is_mariadb: + msg = "UUID4 requires MariaDB version 11.7 or later." + else: + msg = "UUID4 is not supported on MySQL." + elif connection.vendor == "oracle": + msg = "UUID4 requires Oracle version 23ai/26ai (23.9) or later." + else: + msg = "UUID4 is not supported on this database backend." + + with self.assertRaisesMessage(NotSupportedError, msg): + UUIDModel.objects.update(uuid=UUID4()) + + @skipIfDBFeature("supports_uuid7_function") + def test_uuid7_unsupported(self): + if connection.vendor == "mysql": + if connection.mysql_is_mariadb: + msg = "UUID7 requires MariaDB version 11.7 or later." + else: + msg = "UUID7 is not supported on MySQL." + elif connection.vendor == "postgresql": + msg = "UUID7 requires PostgreSQL version 18 or later." + elif connection.vendor == "sqlite": + msg = "UUID7 on SQLite requires Python version 3.14 or later." + else: + msg = "UUID7 is not supported on this database backend." + + with self.assertRaisesMessage(NotSupportedError, msg): + UUIDModel.objects.update(uuid=UUID7()) |
