Esempio n. 1
0
    def test_polymorph_field_not_required(self, api):
        parent = api.model('Person', {
            'name': fields.String,
        })

        child1 = api.inherit('Child1', parent, {
            'extra1': fields.String,
        })

        child2 = api.inherit('Child2', parent, {
            'extra2': fields.String,
        })

        class Child1(object):
            name = 'child1'
            extra1 = 'extra1'

        class Child2(object):
            name = 'child2'
            extra2 = 'extra2'

        mapping = {Child1: child1, Child2: child2}

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        data = api.marshal({}, thing)

        assert data == {'owner': None}
Esempio n. 2
0
    def test_polymorph_field_unknown_class(self, api):
        parent = api.model('Person', {
            'name': fields.String,
        })

        child1 = api.inherit('Child1', parent, {
            'extra1': fields.String,
        })

        child2 = api.inherit('Child2', parent, {
            'extra2': fields.String,
        })

        class Child1(object):
            name = 'child1'
            extra1 = 'extra1'

        class Child2(object):
            name = 'child2'
            extra2 = 'extra2'

        mapping = {Child1: child1, Child2: child2}

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        with pytest.raises(ValueError):
            api.marshal({'owner': object()}, thing)
Esempio n. 3
0
    async def test_marshal_with_handle_polymorph(self, app, client):
        api = Api(app)

        parent = api.model('Person', {
            'name': fields.String,
        })

        child1 = api.inherit('Child1', parent, {
            'extra1': fields.String,
        })

        child2 = api.inherit('Child2', parent, {
            'extra2': fields.String,
        })

        class Child1(object):
            name = 'child1'
            extra1 = 'extra1'

        class Child2(object):
            name = 'child2'
            extra2 = 'extra2'

        mapping = {Child1: child1, Child2: child2}

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        @api.route('/thing-1/')
        class Thing1Resource(Resource):
            @api.marshal_with(thing)
            async def get(self):
                return {'owner': Child1()}

        @api.route('/thing-2/')
        class Thing2Resource(Resource):
            @api.marshal_with(thing)
            async def get(self):
                return {'owner': Child2()}

        data = await client.get_json('/thing-1/',
                                     headers={'X-Fields': 'owner{name}'})
        assert data == {'owner': {'name': 'child1'}}

        data = await client.get_json('/thing-1/',
                                     headers={'X-Fields': 'owner{extra1}'})
        assert data == {'owner': {'extra1': 'extra1'}}

        data = await client.get_json('/thing-2/',
                                     headers={'X-Fields': 'owner{name}'})
        assert data == {'owner': {'name': 'child2'}}
Esempio n. 4
0
    def test_polymorph_with_discriminator(self, api):
        parent = api.model('Person', {
            'name': fields.String,
            'model': fields.String(discriminator=True),
        })

        child1 = api.inherit('Child1', parent, {
            'extra1': fields.String,
        })

        child2 = api.inherit('Child2', parent, {
            'extra2': fields.String,
        })

        class Child1(object):
            name = 'child1'
            extra1 = 'extra1'

        class Child2(object):
            name = 'child2'
            extra2 = 'extra2'

        mapping = {Child1: child1, Child2: child2}

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        def data(cls):
            return api.marshal({'owner': cls()}, thing)

        assert data(Child1) == {
            'owner': {
                'name': 'child1',
                'model': 'Child1',
                'extra1': 'extra1'
            }
        }

        assert data(Child2) == {
            'owner': {
                'name': 'child2',
                'model': 'Child2',
                'extra2': 'extra2'
            }
        }
Esempio n. 5
0
    def test_polymorph_field_no_common_ancestor(self, api):
        child1 = api.model('Child1', {
            'extra1': fields.String,
        })

        child2 = api.model('Child2', {
            'extra2': fields.String,
        })

        class Child1(object):
            pass

        class Child2(object):
            pass

        mapping = {Child1: child1, Child2: child2}

        with pytest.raises(ValueError):
            fields.Polymorph(mapping)
Esempio n. 6
0
    def test_polymorph_field_ambiguous_mapping(self, api):
        parent = api.model('Parent', {
            'name': fields.String,
        })

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

        class Parent(object):
            name = 'parent'

        class Child(Parent):
            extra = 'extra'

        mapping = {Parent: parent, Child: child}

        thing = api.model('Thing', {
            'owner': fields.Polymorph(mapping),
        })

        with pytest.raises(ValueError):
            api.marshal({'owner': Child()}, thing)
Esempio n. 7
0
    def test_polymorph_inherit_common_ancestor(self):
        class Child1:
            pass

        class Child2:
            pass

        parent = Model('Person', {
            'name': fields.String,
            'age': fields.Integer,
        })

        child1 = parent.inherit('Child1', {
            'extra1': fields.String,
        })

        child2 = parent.inherit('Child2', {
            'extra2': fields.String,
        })

        mapping = {
            Child1: child1,
            Child2: child2,
        }

        output = Model('Output', {'child': fields.Polymorph(mapping)})

        # Should use the common ancestor
        assert output.__schema__ == {
            'properties': {
                'child': {
                    '$ref': '#/definitions/Person'
                },
            },
            'type': 'object'
        }