def test_deconstruct(self): field = native_shortuuid.NativeShortUUIDField() name, path, args, kwargs = field.deconstruct() self.assertEqual(kwargs, {}) field = native_shortuuid.NativeShortUUIDField( default=native_shortuuid.fields.short_uuid4) name, path, args, kwargs = field.deconstruct() self.assertEqual(set(kwargs.keys()), {'default'}) self.assertEqual(kwargs['default'], native_shortuuid.fields.short_uuid4) field = native_shortuuid.NativeShortUUIDField(default=uuid.uuid4) name, path, args, kwargs = field.deconstruct() self.assertEqual(set(kwargs.keys()), {'default'}) self.assertEqual(kwargs['default'], uuid.uuid4)
def test_invalid_uuid(self): field = native_shortuuid.NativeShortUUIDField() with self.assertRaises(exceptions.ValidationError) as cm: field.clean('550e8400', None) self.assertEqual(cm.exception.code, 'invalid') self.assertEqual(cm.exception.message % cm.exception.params, '“550e8400” is not a valid ShortUUID.')
class ShortUUIDModel(django.db.models.Model): field = native_shortuuid.NativeShortUUIDField()
class PrimaryKeyShortUUIDModel(django.db.models.Model): id = native_shortuuid.NativeShortUUIDField(primary_key=True, default=uuid.uuid4)
class NullableShortUUIDModel(django.db.models.Model): field = native_shortuuid.NativeShortUUIDField(blank=True, null=True)
def test_uuid_instance_ok(self): field = native_shortuuid.NativeShortUUIDField() field.clean(shortuuid.encode(uuid.uuid4()), None) # no error field.clean(uuid.uuid4(), None) # no error
def test_to_python_shortuuid_too_small(self): # Fails for strings less than 22 characters. with self.assertRaises(exceptions.ValidationError): native_shortuuid.NativeShortUUIDField().to_python('2' * 21)
def test_to_python(self): self.assertIsNone( native_shortuuid.NativeShortUUIDField().to_python(None))