Ejemplo n.º 1
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()) == {'father', 'mother'}
        assert isinstance(result['father'], fields.Nested)
        assert set(result['father'].nested.keys()) == {'name'}
        assert isinstance(result['mother'], fields.Nested)
        assert set(result['mother'].nested.keys()) == {'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)
Ejemplo n.º 2
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()) == {'members'}
        assert isinstance(result['members'], fields.List)
        assert isinstance(result['members'].container, fields.Nested)
        assert set(result['members'].container.nested.keys()) == {'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)
Ejemplo n.º 3
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()) == {'nested'}
        assert isinstance(result['nested'], fields.Nested)
        assert set(result['nested'].nested.keys()) == {'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)
Ejemplo n.º 4
0
    def test_marshal_handle_inheritance(self, app):
        api = Api(app)

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

        child = api.inherit('Child', person, {
            'extra': fields.String,
        })

        data = {'name': 'John Doe', 'age': 42, 'extra': 'extra'}

        values = (
            ('name', {
                'name': 'John Doe'
            }),
            ('name,extra', {
                'name': 'John Doe',
                'extra': 'extra'
            }),
            ('extra', {
                'extra': 'extra'
            }),
        )

        for value, expected in values:
            result = marshal(data, child, mask=value)
            assert result == expected
Ejemplo n.º 5
0
    def test_list_fields_with_simple_field(self):
        family_fields = {
            'name': fields.String,
            'members': fields.List(fields.String)
        }

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

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

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    def test_list_fields_with_raw(self):
        family_fields = {'members': fields.List(fields.Raw)}

        result = mask.apply(family_fields, 'members{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)
Ejemplo n.º 8
0
    def test_raw_api_fields(self):
        family_fields = {
            'father': fields.Raw,
            'mother': fields.Raw,
        }

        result = mask.apply(family_fields, 'father{name},mother{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)
Ejemplo n.º 9
0
def marshal_nested_with_mask(app):
    with app.test_request_context(
            '/', headers={'X-Fields': 'father,children{name}'}):
        return marshal(family(), family_fields)
Ejemplo n.º 10
0
def marshal_simple_with_mask(app):
    with app.test_request_context('/', headers={'X-Fields': 'name'}):
        return marshal(person(), person_fields)
Ejemplo n.º 11
0
def marshal_nested():
    return marshal(family(), family_fields)
Ejemplo n.º 12
0
def marshal_simple():
    return marshal(person(), person_fields)