Skip to content

Commit

Permalink
Merge pull request #48 from ksonbol/master
Browse files Browse the repository at this point in the history
Raise ValueError in get_db_prep_value() when value is of invalid length
  • Loading branch information
dcramer committed Jan 19, 2015
2 parents 57f6e43 + f00f89d commit a30a1eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion uuidfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def get_db_prep_value(self, value, connection, prepared=False):
value = str(value)
if isinstance(value, str):
if '-' in value:
return value.replace('-', '')
value = value.replace('-', '')
uuid.UUID(value) # raises ValueError with invalid UUID format
return value

def value_to_string(self, obj):
Expand Down
18 changes: 18 additions & 0 deletions uuidfield/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def test_namespace(self):
self.assertTrue(isinstance(obj.uuid, uuid.UUID))
self.assertEquals(obj.uuid.version, 5)

def test_long_uuid(self):
invalid_uuid = "1" * 33
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_short_uuid(self):
invalid_uuid = "1" * 31
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_invalid_hex(self):
invalid_uuid = 'z' * 32
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_broken_namespace(self):
self.assertRaises(ValueError, BrokenNamespaceUUIDField.objects.create)

Expand Down

0 comments on commit a30a1eb

Please sign in to comment.