Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Type not serializable #45

Open
japo32 opened this issue Mar 1, 2016 · 2 comments
Open

TypeError: Type not serializable #45

japo32 opened this issue Mar 1, 2016 · 2 comments

Comments

@japo32
Copy link

japo32 commented Mar 1, 2016

Hi, I have the following test below that puts simple text into an object:

from nose2.compat import unittest
import boto3
import placebo

class S3PlaceboTestCase(unittest.TestCase):

    def setUp(self):
        super().setUp()
        self.session = boto3.Session()


class S3PlaceboRecordCase(S3PlaceboTestCase):

    def setUp(self):
        super().setUp()
        path = 'responses'
        self.pill = placebo.attach(self.session, data_path=path)
        self.pill.record(services='s3')
        self.s3 = self.session.client("s3")

    def tearDown(self):
        super().tearDown()
        self.pill.stop()

    def test_pill_record(self):
        self.s3.put_object(Bucket='my_bucket', ContentType='text/plain', Key='pill_test.txt', Body='Sample text')
        obj = self.s3.get_object(Bucket='my_bucket, Key='pill_test.txt')
        self.s3.delete_object(Bucket='my_bucket', Key='pill_test.txt')
        self.assertTrue(True)

And when I run the test I'm getting:

  File "/usr/share/stuff-site-adcontrol/.test-venv/lib/python3.4/site-packages/placebo/serializer.py", line 60, in serialize
    raise TypeError("Type not serializable")
TypeError: Type not serializable

Which is strange because it's simple text.

Might be related to #5

@japo32
Copy link
Author

japo32 commented Mar 1, 2016

We managed to fix it temporarily by adding a decode in the serialize function and encode in the deserialize function.

def deserialize(obj):
...
    if class_name == 'StreamingBody':
        return StringIO(target['body'].encode('UTF-8'))
...
def serialize(obj):
...
    if isinstance(obj, StreamingBody):
        result['body'] = obj.read().decode('UTF-8')
        return result
...

@japo32
Copy link
Author

japo32 commented Mar 3, 2016

Unfortunately the StreamingBody of obj here is an empty byte object:

obj = self.s3.get_object(Bucket='my_bucket, Key='pill_test.txt')
pp.pprint(obj['Body'].read())

produces:

b''

when I comment out pill.record() I get this:

b'Sample text'

But when I look at the recorded JSON file, the data is properly recorded:

"Body": {
            "__class__": "StreamingBody",
            "body": "Sample text",
            "__module__": "botocore.response"
        },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant