Skip to content

Commit

Permalink
to_dict now returns empty values
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Sep 3, 2015
1 parent cccce0c commit 31b79ec
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
12 changes: 12 additions & 0 deletions repocket/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def cast(cls, value):
if isinstance(value, PythonsUUID):
return value

if not value:
return

return super(AutoUUID, cls).cast(value)


Expand All @@ -110,6 +113,9 @@ def cast(cls, value):
if isinstance(value, PythonsUUID):
return value

if not value:
return

return super(UUID, cls).cast(value)


Expand Down Expand Up @@ -199,6 +205,9 @@ def __init__(self, to_model, null=False):
self.__base_type__ = to_model

def to_string(self, value):
if not value:
return json.dumps(None)

if not value.get_id():
raise ReferenceError('The model {0} must be saved before serialized as a pointer in another model'.format(value))

Expand All @@ -207,6 +216,9 @@ def to_string(self, value):
@classmethod
def cast(cls, value):
"""this method uses a redis connection to retrieve the referenced item"""
if value == json.dumps(None):
return

try:
_, module_name, model_name, model_uuid = value.split(':')
except ValueError:
Expand Down
10 changes: 5 additions & 5 deletions repocket/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ def to_dict(self, simple=False):
simple_version = {}
for name, field in self.__fields__.items():
value = getattr(self, name, field.get_empty_value())
if not value:
continue

try:
serialized_value = field.to_json(value)
except (AttributeError, TypeError):
raise TypeError('Failed to serialize field {0}.{1} of type {2} with value: {3}'.format(

except (AttributeError, TypeError) as e:
raise TypeError('Failed to serialize field {0}.{1} of type {2} with value: {3} - {4}'.format(
self.__class__.__name__,
name,
type(value),
value
value,
e
))

if isinstance(field, attributes.ByteStream):
Expand Down
7 changes: 5 additions & 2 deletions tests/functional/test_active_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ def test_to_simple_dict(context):
email='foo@bar.com',
)
result = author.to_dict(simple=True)

result.should.equal({
'id': 'b9c9bf17-ef60-45bf-8217-4daabc6bc483',
'email': 'foo@bar.com',
'access_token': '',
'email': u'foo@bar.com',
'github_metadata': u'',
'id': u'b9c9bf17-ef60-45bf-8217-4daabc6bc483'
})
2 changes: 1 addition & 1 deletion tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_active_record_calculate_key_for_field():
result = item._calculate_key_for_field('contents')
result.should.equal('repocket:tests.unit.test_model:UnitModelOne:059f3270-9e73-4d53-9970-443f83e412a0:field:contents')

repr(item).should.equal(b'tests.unit.test_model.UnitModelOne(hash={u\'id\': \'{"type": "AutoUUID", "value": "059f3270-9e73-4d53-9970-443f83e412a0", "module": "repocket.attributes"}\'}, strings={})')
repr(item).should.equal(b'tests.unit.test_model.UnitModelOne(hash={u\'id\': \'{"type": "AutoUUID", "value": "059f3270-9e73-4d53-9970-443f83e412a0", "module": "repocket.attributes"}\'}, strings={\'contents\': \'\'})')


def test_equality_ok():
Expand Down

0 comments on commit 31b79ec

Please sign in to comment.