Skip to content

Commit

Permalink
Handle UUIDField value more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Ambra committed Sep 18, 2017
1 parent 050471d commit ae54043
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 5 additions & 4 deletions declaration/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ def encode(self, value):
class UUIDField(GenericField):

def parse(self, value):
try:
value = uuid.UUID(value, version=4)
except ValueError:
pass
if not isinstance(value, uuid.UUID):
try:
value = uuid.UUID(value, version=4)
except ValueError:
pass

return value

Expand Down
6 changes: 6 additions & 0 deletions declaration/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def test_uuid_field_converts(self):

assert converted == field.parse(value)

def test_uuid_field_passes_through_if_already_uuid(self):
field = fields.UUIDField()
value = uuid.UUID("c3864285-1f1e-4c0c-bd95-ad5a8dbf3232", version=4)

assert value == field.parse(value)

def test_uuid_field_fails_if_not_uuid(self):
field = fields.UUIDField()
value = "garbage"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
author='Jordan Ambra',
author_email='jordan@serenitysoftware.io',
url='https://github.com/boomletsgo/declaration',
version='0.1.2',
version='0.1.3',
classifiers=classifiers,
description='Base classes and fields for declarative programming, like Django ORM',
keywords='declarative declaration metaclass',
Expand Down

0 comments on commit ae54043

Please sign in to comment.