コード例 #1
0
def test_proto_rendering():

    class TestSchema(BaseModelSchema):
        a = fields.Int()
        b = fields.String()
        c = fields.Boolean()

        config = {
            'ordering': [
                ('a', ('asc',))
            ],
            'protobuffers': {
                'load': proto.Test(),
                'dump': proto.Test(),
                'load_many': proto.Test(),
                'dump_many': proto.TestCollection()
            }
        }

    @TestSchema()
    def business_logic(*args, **kwargs):
        return {
            'a': 1,
            'b': 'One',
            'c': True
        }

    with app.test_request_context(
            '/',
            method='POST',
            data=proto.Test().dict_to_message({'a': 2, 'b': 'Two', 'c': False}).SerializeToString(),
            headers={
                'Content-Type': 'application/octet-stream',
                'Accept': 'application/octet-stream'
            }):
        result = business_logic()
        assert isinstance(result, bytes)
        result = proto.Test().proto_to_dict(result)
        assert result == {
            'a': 1,
            'b': 'One',
            'c': True
        }

    proto_data = proto.Test().dict_to_message({'a': 2, 'b': 'Two', 'c': False}).SerializeToString()
    with app.test_request_context(
            '/',
            method='POST',
            data=proto_data[:-1],
            headers={
                'Content-Type': 'application/octet-stream',
                'Accept': 'application/octet-stream'
            }):
        with pytest.raises(flask_exceptions.BadRequest) as excinfo:
            business_logic()
        assert excinfo.value.message == 'Invalid protocol buffer data'
コード例 #2
0
    class TestSchema(base.BaseModelSchema):
        a = fields.Integer()
        b = fields.String()

        config = {
            'protobuffers': {
                'load': proto.Test(),
                'dump': proto.Test(),
                'load_many': proto.Test(),
                'dump_many': proto.TestCollection()
            }
        }
コード例 #3
0
    class TestSchema(BaseModelSchema):
        a = fields.Int()
        b = fields.String()
        c = fields.Boolean()

        config = {
            'ordering': [
                ('a', ('asc',))
            ],
            'protobuffers': {
                'load': proto.Test(),
                'dump': proto.Test(),
                'load_many': proto.Test(),
                'dump_many': proto.TestCollection()
            }
        }
コード例 #4
0
    class TestSchema(BaseModelSchema):
        a = fields.Int()
        b = fields.String()
        c = fields.Boolean()

        config = {
            'ordering': [
                ('a', ('asc',))
            ],
            'protobuffers': {
                'dump': proto.Test(),
            }
        }
コード例 #5
0
def test_proto_with_empty_request_body(*args, **kwargs):

    class TestSchema(BaseModelSchema):
        a = fields.Int()
        b = fields.String()
        c = fields.Boolean()

        config = {
            'ordering': [
                ('a', ('asc',))
            ],
            'protobuffers': {
                'dump': proto.Test(),
            }
        }

    @TestSchema()
    def business_logic(*args, **kwargs):
        return {
            'a': 1,
            'b': 'One',
            'c': True
        }

    with app.test_request_context(
            '/',
            method='POST',
            headers={
                'Content-Type': 'application/octet-stream',
                'Accept': 'application/octet-stream'
            }):
        result = business_logic()
        assert isinstance(result, bytes)
        result = proto.Test().proto_to_dict(result)
        assert result == {
            'a': 1,
            'b': 'One',
            'c': True
        }