Skip to content

Commit

Permalink
Trying another approach to fix PR #70.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch Garnaat committed Sep 24, 2021
1 parent 2d4a310 commit b4d0eff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
15 changes: 12 additions & 3 deletions placebo/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import json
import pickle
import datetime
import base64
from io import BytesIO

from botocore.response import StreamingBody
from six import StringIO


class UTC(datetime.tzinfo):
Expand Down Expand Up @@ -78,7 +80,9 @@ def deserialize(obj):
if class_name == 'datetime':
return datetime.datetime(tzinfo=utc, **target)
if class_name == 'StreamingBody':
return StringIO(target['body'])
b64_body = obj['body']
decoded_body = base64.b64decode(b64_body)
return BytesIO(decoded_body)
# Return unrecognized structures as-is
return obj

Expand All @@ -92,6 +96,8 @@ def serialize(obj):
except AttributeError:
pass
# Convert objects to dictionary representation based on type
print('+++++ %s' % obj)
print('+++++ %s' % type(obj))
if isinstance(obj, datetime.datetime):
result['year'] = obj.year
result['month'] = obj.month
Expand All @@ -103,9 +109,12 @@ def serialize(obj):
return result
if isinstance(obj, StreamingBody):
result['body'] = obj.read()
obj._raw_stream = StringIO(result['body'])
obj._raw_stream = BytesIO(result['body'])
obj._amount_read = 0
return result
if isinstance(obj, bytes):
encoded = base64.b64encode(obj)
return encoded.decode('utf-8')
# Raise a TypeError if the object isn't recognized
raise TypeError("Type not serializable")

Expand Down
22 changes: 21 additions & 1 deletion tests/unit/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import unittest
import json

from six import StringIO, BytesIO
from io import StringIO, BytesIO

from botocore.response import StreamingBody

from placebo.serializer import serialize, deserialize, utc, Format
from placebo.serializer import get_serializer, get_deserializer
Expand All @@ -33,6 +35,15 @@

date_json = """{"LoginProfile": {"CreateDate": {"__class__": "datetime", "day": 4, "hour": 9, "microsecond": 0, "minute": 1, "month": 1, "second": 2, "year": 2015}, "UserName": "baz"}}"""

content = b'this is a test'
stream = BytesIO(content)

streaming_body_sample = {
'Body': StreamingBody(stream, len(content))
}

streaming_body_json = """{"Body": {"__class__": "StreamingBody", "__module__": "botocore.response", "body": "dGhpcyBpcyBhIHRlc3Q="}}"""


class TestSerializers(unittest.TestCase):

Expand All @@ -44,6 +55,15 @@ def test_datetime_from_json(self):
response = json.loads(date_json, object_hook=deserialize)
self.assertEqual(response, date_sample)

def test_streaming_body_to_json(self):
result = json.dumps(
streaming_body_sample, default=serialize, sort_keys=True)
self.assertEqual(result, streaming_body_json)

def test_streaming_body_from_json(self):
response = json.loads(streaming_body_json, object_hook=deserialize)
self.assertEqual(response['Body'].read(), content)

def test_roundtrip_json(self):
ser = get_serializer(Format.JSON)
deser = get_deserializer(Format.JSON)
Expand Down

0 comments on commit b4d0eff

Please sign in to comment.