2016-04-10 01:17:34 +08:00
|
|
|
import uuid
|
|
|
|
|
2020-02-12 21:48:49 +08:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.forms import UUIDField
|
2016-04-10 01:17:34 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class UUIDFieldTest(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_uuidfield_1(self):
|
|
|
|
field = UUIDField()
|
|
|
|
value = field.clean('550e8400e29b41d4a716446655440000')
|
|
|
|
self.assertEqual(value, uuid.UUID('550e8400e29b41d4a716446655440000'))
|
|
|
|
|
2018-08-18 06:43:35 +08:00
|
|
|
def test_clean_value_with_dashes(self):
|
|
|
|
field = UUIDField()
|
|
|
|
value = field.clean('550e8400-e29b-41d4-a716-446655440000')
|
|
|
|
self.assertEqual(value, uuid.UUID('550e8400e29b41d4a716446655440000'))
|
|
|
|
|
2016-04-10 01:17:34 +08:00
|
|
|
def test_uuidfield_2(self):
|
|
|
|
field = UUIDField(required=False)
|
2020-09-25 16:00:05 +08:00
|
|
|
self.assertIsNone(field.clean(''))
|
|
|
|
self.assertIsNone(field.clean(None))
|
2016-04-10 01:17:34 +08:00
|
|
|
|
|
|
|
def test_uuidfield_3(self):
|
|
|
|
field = UUIDField()
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ValidationError, 'Enter a valid UUID.'):
|
2016-04-10 01:17:34 +08:00
|
|
|
field.clean('550e8400')
|
|
|
|
|
|
|
|
def test_uuidfield_4(self):
|
|
|
|
field = UUIDField()
|
|
|
|
value = field.prepare_value(uuid.UUID('550e8400e29b41d4a716446655440000'))
|
2018-08-18 06:43:35 +08:00
|
|
|
self.assertEqual(value, '550e8400-e29b-41d4-a716-446655440000')
|