Skip to content

Commit

Permalink
Serialize date/datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
pcraciunoiu committed May 13, 2016
1 parent 5a19508 commit 2e72192
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cc_dynamodb3/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ class DynamoDBJSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return str(o)
if isinstance(o, (datetime.datetime, datetime.date)):
return o.isoformat()
return super(DynamoDBJSONEncoder, self).default(o)


Expand Down
11 changes: 10 additions & 1 deletion tests/test_hash_only_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import mock

from cc_dynamodb3.models import to_json
from factories.hash_only_model import (
HashOnlyModel,
HashOnlyModelFactory,
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_has_changed_primary_key_save_logs(log_data_mock):
assert called_with[0][0] == 'save overwrite=True table=dev_hash_only'


def test_to_json():
def test_model_to_json():
HashOnlyModelFactory.create_table()
obj = HashOnlyModelFactory(agency_subdomain='metzler', external_id=123)

Expand All @@ -88,6 +89,14 @@ def test_to_json():
assert ('"updated": "%s"' % obj.updated.isoformat()) in json_data


def test_to_json():
adatetime = datetime.datetime.utcnow()
adate = datetime.date.today()
json_data = to_json({'adatetime': adatetime,
'adate': adate})
assert '"{0}"'.format(adate.isoformat()) in json_data


def test_negative_timestamp():
long_ago = datetime.datetime.utcnow()
long_ago = long_ago.replace(year=1899)
Expand Down

0 comments on commit 2e72192

Please sign in to comment.