def test_to_python_int_values(self): self.assertEqual( ULIDField().to_python(0), ulid.parse('00000000-0000-0000-0000-000000000000') ) # Works for integers less than 128 bits. self.assertEqual( ULIDField().to_python((2 ** 128) - 1), ulid.parse('ffffffff-ffff-ffff-ffff-ffffffffffff') )
class ULIDModel(models.Model): field = ULIDField(default=None, null=False)
class PrimaryKeyULIDModel(models.Model): id = ULIDField(primary_key=True, default=ulid.new)
class NullableULIDModel(models.Model): field = ULIDField(blank=True, null=True)
def test_to_python_int_too_large(self): # Fails for integers larger than 128 bits. with self.assertRaises(exceptions.ValidationError): ULIDField().to_python(2 ** 128)
def test_to_python(self): self.assertIsNone(ULIDField().to_python(None))
def test_deconstruct(self): field = ULIDField() name, path, args, kwargs = field.deconstruct() self.assertEqual(kwargs, {})
def test_ulid_instance_ok(self): field = ULIDField() field.clean(ulid.new(), None) # no error
def test_invalid_ulid(self): field = ULIDField() 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 ULID.")