Beispiel #1
0
    def test_multiple_nested_api_fields(self):
        level_2 = {'nested_2': fields.Nested(person_fields)}
        level_1 = {'nested_1': fields.Nested(level_2)}
        root = {'nested': fields.Nested(level_1)}

        result = mask.apply(root, 'nested{nested_1{nested_2{name}}}')
        assert set(result.keys()) == set(['nested'])
        assert isinstance(result['nested'], fields.Nested)
        assert set(result['nested'].nested.keys()) == set(['nested_1'])

        data = {
            'nested': {
                'nested_1': {
                    'nested_2': {
                        'name': 'John',
                        'age': 42
                    }
                }
            }
        }
        expected = {'nested': {'nested_1': {'nested_2': {'name': 'John'}}}}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, root), data)
Beispiel #2
0
    def test_nested_api_fields(self):
        family_fields = {
            'father': fields.Nested(person_fields),
            'mother': fields.Nested(person_fields),
        }

        result = mask.apply(family_fields, 'father{name},mother{age}')
        assert set(result.keys()) == set(['father', 'mother'])
        assert isinstance(result['father'], fields.Nested)
        assert set(result['father'].nested.keys()) == set(['name'])
        assert isinstance(result['mother'], fields.Nested)
        assert set(result['mother'].nested.keys()) == set(['age'])

        data = {
            'father': {
                'name': 'John',
                'age': 42
            },
            'mother': {
                'name': 'Jane',
                'age': 42
            },
        }
        expected = {'father': {'name': 'John'}, 'mother': {'age': 42}}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
Beispiel #3
0
    def test_as_list_is_reusable(self, api):
        nested_fields = api.model('NestedModel', {'name': fields.String})

        field = fields.Nested(nested_fields, as_list=True)
        assert field.__schema__ == {'type': 'array', 'items': {'$ref': '#/definitions/NestedModel'}}

        field = fields.Nested(nested_fields)
        assert field.__schema__ == {'$ref': '#/definitions/NestedModel'}
Beispiel #4
0
    def test_list_fields_with_nested(self):
        family_fields = {'members': fields.List(fields.Nested(person_fields))}

        result = mask.apply(family_fields, 'members{name}')
        assert set(result.keys()) == set(['members'])
        assert isinstance(result['members'], fields.List)
        assert isinstance(result['members'].container, fields.Nested)
        assert set(result['members'].container.nested.keys()) == set(['name'])

        data = {
            'members': [
                {
                    'name': 'John',
                    'age': 42
                },
                {
                    'name': 'Jane',
                    'age': 42
                },
            ]
        }
        expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
Beispiel #5
0
 def test_with_title(self, api):
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, title='A title')
     assert field.__schema__ == {
         '$ref': '#/definitions/NestedModel',
         'title': 'A title'
     }
Beispiel #6
0
 def test_with_description(self, api):
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, description='A description')
     assert field.__schema__ == {
         '$ref': '#/definitions/NestedModel',
         'description': 'A description'
     }
    def test_list_fields_with_nested_inherited(self, app):
        api = Api(app)

        person = api.model('Person', {
            'name': fields.String,
            'age': fields.Integer
        })
        child = api.inherit('Child', person, {
            'attr': fields.String
        })

        family = api.model('Family', {
            'children': fields.List(fields.Nested(child))
        })

        result = mask.apply(family.resolved, 'children{name,attr}')

        data = {'children': [
            {'name': 'John', 'age': 5, 'attr': 'value-john'},
            {'name': 'Jane', 'age': 42, 'attr': 'value-jane'},
        ]}
        expected = {'children': [
            {'name': 'John', 'attr': 'value-john'},
            {'name': 'Jane', 'attr': 'value-jane'},
        ]}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family), data)
Beispiel #8
0
 def test_with_readonly(self, app):
     api = Api(app)
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, readonly=True)
     assert field.__schema__ == {
         '$ref': '#/definitions/NestedModel',
         'readOnly': True
     }
Beispiel #9
0
    def test_with_nested_field(self, api):
        nested_fields = api.model('NestedModel', {'name': fields.String})
        field = fields.List(fields.Nested(nested_fields))
        assert field.__schema__ == {'type': 'array', 'items': {'$ref': '#/definitions/NestedModel'}}

        data = [{'name': 'John Doe', 'age': 42}, {'name': 'Jane Doe', 'age': 66}]
        expected = [OrderedDict([('name', 'John Doe')]), OrderedDict([('name', 'Jane Doe')])]
        self.assert_field(field, data, expected)
    def test_marshal_with_honour_complex_field_mask_header(self, app, client):
        api = Api(app)

        person = api.model('Person', person_fields)
        child = api.inherit('Child', person, {
            'attr': fields.String
        })

        family = api.model('Family', {
            'father': fields.Nested(person),
            'mother': fields.Nested(person),
            'children': fields.List(fields.Nested(child)),
            'free': fields.List(fields.Raw),
        })

        house = api.model('House', {
            'family': fields.Nested(family, attribute='people')
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(house)
            def get(self):
                return {'people': {
                    'father': {'name': 'John', 'age': 42},
                    'mother': {'name': 'Jane', 'age': 42},
                    'children': [
                        {'name': 'Jack', 'age': 5, 'attr': 'value-1'},
                        {'name': 'Julie', 'age': 7, 'attr': 'value-2'},
                    ],
                    'free': [
                        {'key-1': '1-1', 'key-2': '1-2'},
                        {'key-1': '2-1', 'key-2': '2-2'},
                    ]
                }}

        data = client.get_json('/test/', headers={
            'X-Fields': 'family{father{name},mother{age},children{name,attr},free{key-2}}'
        })
        assert data == {'family': {
            'father': {'name': 'John'},
            'mother': {'age': 42},
            'children': [{'name': 'Jack', 'attr': 'value-1'}, {'name': 'Julie', 'attr': 'value-2'}],
            'free': [{'key-2': '1-2'}, {'key-2': '2-2'}]
        }}
Beispiel #11
0
 def test_marshal_nested_with_skip_none(self):
     model = OrderedDict([('foo', fields.Raw),
                          ('fee',
                           fields.Nested(OrderedDict([('fye', fields.String)
                                                      ]),
                                         skip_none=True))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = marshal(marshal_fields, model, skip_none=True)
     expected = OrderedDict([('foo', 'bar')])
     assert output == expected
Beispiel #12
0
 def test_marshal_list_of_nesteds(self):
     model = OrderedDict([('foo', fields.Raw),
                          ('fee',
                           fields.List(fields.Nested({'fye':
                                                      fields.String})))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', {
                                       'fye': 'fum'
                                   })])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'),
                             ('fee', [OrderedDict([('fye', 'fum')])])])
     assert output == expected
Beispiel #13
0
 def test_marshal_nested_with_null(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee',
          fields.Nested(OrderedDict([('fye', fields.String),
                                     ('blah', fields.String)]),
                        allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'), ('fee', None)])
     assert output == expected
Beispiel #14
0
 def test_skip_none_presents_data(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee',
          fields.Nested(OrderedDict([('fye', fields.String),
                                     ('blah', fields.String),
                                     ('foe', fields.String)]),
                        skip_none=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', {
                                       'blah': 'cool',
                                       'foe': None
                                   })])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'),
                             ('fee', OrderedDict([('blah', 'cool')]))])
     assert output == expected
Beispiel #15
0
    def test_model_as_nested_dict_with_list(self):
        address = Model('Address', {
            'road': fields.String,
        })

        person = Model(
            'Person', {
                'name': fields.String,
                'age': fields.Integer,
                'birthdate': fields.DateTime,
                'addresses': fields.List(fields.Nested(address))
            })

        assert person.__schema__ == {
            # 'required': ['address'],
            'properties': {
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
                'birthdate': {
                    'type': 'string',
                    'format': 'date-time'
                },
                'addresses': {
                    'type': 'array',
                    'items': {
                        '$ref': '#/definitions/Address',
                    }
                }
            },
            'type': 'object'
        }

        assert address.__schema__ == {
            'properties': {
                'road': {
                    'type': 'string'
                },
            },
            'type': 'object'
        }
Beispiel #16
0
    def test_marshal_nested_ordered(self):
        model = OrderedDict([('foo', fields.Raw),
                             ('fee', fields.Nested({
                                 'fye': fields.String,
                             }))])

        marshal_fields = {
            'foo': 'bar',
            'bat': 'baz',
            'fee': {
                'fye': 'fum'
            },
        }
        expected = OrderedDict([('foo', 'bar'),
                                ('fee', OrderedDict([('fye', 'fum')]))])

        output = marshal(marshal_fields, model, ordered=True)

        assert isinstance(output, OrderedDict)
        assert output == expected
        assert isinstance(output['fee'], OrderedDict)
Beispiel #17
0
    def test_marshal_nested_property_with_skip_none(self):
        class TestObject(object):
            @property
            def fee(self):
                return {'blah': 'cool', 'foe': None}

        model = OrderedDict([
            ('foo', fields.Raw),
            ('fee',
             fields.Nested(OrderedDict([('fye', fields.String),
                                        ('blah', fields.String),
                                        ('foe', fields.String)]),
                           skip_none=True))
        ])
        obj = TestObject()
        obj.foo = 'bar'
        obj.bat = 'baz'
        output = marshal([obj], model)
        expected = [
            OrderedDict([('foo', 'bar'),
                         ('fee', OrderedDict([('blah', 'cool')]))])
        ]
        assert output == expected
Beispiel #18
0
    def test_marshal_nested(self):
        model = {
            'foo': fields.Raw,
            'fee': fields.Nested({'fye': fields.String}),
        }

        marshal_fields = {
            'foo': 'bar',
            'bat': 'baz',
            'fee': {
                'fye': 'fum'
            },
        }
        expected = {
            'foo': 'bar',
            'fee': {
                'fye': 'fum'
            },
        }

        output = marshal(marshal_fields, model)

        assert output == expected
Beispiel #19
0
    'todo2': {
        'task': '?????'
    },
    'todo3': {
        'task': 'profit!'
    },
}

todo = api.model(
    'Todo',
    {'task': fields.String(required=True, description='The task details')})

listed_todo = api.model(
    'ListedTodo', {
        'id': fields.String(required=True, description='The todo ID'),
        'todo': fields.Nested(todo, description='The Todo')
    })


def abort_if_todo_doesnt_exist(todo_id):
    if todo_id not in TODOS:
        api.abort(404, "Todo {} doesn't exist".format(todo_id))


parser = api.parser()
parser.add_argument('task',
                    type=str,
                    required=True,
                    help='The task details',
                    location='form')
import pytest

from faker import Faker

from sanic_restplus import marshal, fields

fake = Faker()

person_fields = {'name': fields.String, 'age': fields.Integer}

family_fields = {
    'father': fields.Nested(person_fields),
    'mother': fields.Nested(person_fields),
    'children': fields.List(fields.Nested(person_fields))
}


def person():
    return {'name': fake.name(), 'age': fake.pyint()}


def family():
    return {
        'father': person(),
        'mother': person(),
        'children': [person(), person()]
    }


def marshal_simple():
    return marshal(person(), person_fields)
Beispiel #21
0
 def test_defaults(self, api):
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields)
     assert not field.required
     assert field.__schema__ == {'$ref': '#/definitions/NestedModel'}
Beispiel #22
0
 def test_with_allow_null(self, api):
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, allow_null=True)
     assert not field.required
     assert field.allow_null
     assert field.__schema__ == {'$ref': '#/definitions/NestedModel'}
Beispiel #23
0
 def test_with_skip_none(self, api):
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, skip_none=True)
     assert not field.required
     assert field.skip_none
     assert field.__schema__ == {'$ref': '#/definitions/NestedModel'}