Repository: django/django
File: tests/db_functions/test_uuid.py (Python)

Code before:
        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)

    @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))
        m.refresh_from_db()
        self.assertIsInstance(m.uuid, uuid.UUID)
        self.assertEqual(m.uuid.version, 7)
        self.assertTrue(str(m.uuid).startswith("0107965e-e400"))

    @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 = UUIDDurationModel.objects.create(shift=shift)
        UUIDDurationModel.objects.update(uuid=UUID7("shift"))
        m.refresh_from_db()
        self.assertIsInstance(m.uuid, uuid.UUID)
        self.assertEqual(m.uuid.version, 7)
        self.assertTrue(str(m.uuid).startswith("0107965e-e400"))

    @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."


Review comment (bug):
These assertions are failing for me:
```py
System check identified no issues (46 silenced).
FFs
======================================================================
FAIL: test_uuid7_shift (db_functions.test_uuid.TestUUID.test_uuid7_shift)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/django/source/tests/db_functions/test_uuid.py", line 44, in test_uuid7_shift
    self.assertTrue(str(m.uuid).startswith("0107965e-e400"))
AssertionError: False is not true

----------------------------------------------------------------------
(0.005) INSERT INTO "db_functions_uuidmodel" ("uuid")
VALUES (UUIDV7('-7331 days 1:44:20.748468'::interval)) RETURNING "db_functions_uuidmodel"."id",
                                                                 "db_functions_uuidmodel"."uuid"; args=(datetime.timedelta(days=-7331, seconds=6260, microseconds=748468),); alias=default
(0.001) SELECT "db_functions_uuidmodel"."id",
       "db_functions_uuidmodel"."uuid"
FROM "db_functions_uuidmodel"
WHERE "db_functions_uuidmodel"."id" = 5
LIMIT 21; args=(5,); alias=default

======================================================================
FAIL: test_uuid7_shift_duration_field (db_functions.test_uuid.TestUUID.test_uuid7_shift_duration_field)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/django/source/tests/db_functions/test_uuid.py", line 56, in test_uuid7_shift_duration_field
    self.assertTrue(str(m.uuid).startswith("0107965e-e400"))
AssertionError: False is not true

----------------------------------------------------------------------
(0.001) INSERT INTO "db_functions_uuiddurationmodel" ("uuid",
                                              "shift")
VALUES (NULL, '-7331 days 1:44:20.725522'::interval) RETURNING "db_functions_uuiddurationmodel"."id"; args=(None, datetime.timedelta(days=-7331, seconds=6260, microseconds=725522)); alias=default
(0.001) UPDATE "db_functions_uuiddurationmodel"
SET "uuid" = UUIDV7("db_functions_uuiddurationmodel"."shift"); args=(); alias=default
(0.001) SELECT "db_functions_uuiddurationmodel"."id",
       "db_functions_uuiddurationmodel"."uuid",
       "db_functions_uuiddurationmodel"."shift"
FROM "db_functions_uuiddurationmodel"
WHERE "db_functions_uuiddurationmodel"."id" = 5
LIMIT 21; args=(5,); alias=default

----------------------------------------------------------------------
Ran 3 tests in 0.042s

FAILED (failures=2, skipped=1)
```

Code after:
        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)

    @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":
            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):