Beispiel #1
0
    def test_model_deepcopy(self):
        parent = Model('Person', {
            'name': fields.String,
            'age': fields.Integer(description="foo"),
        })

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

        parent_copy = copy.deepcopy(parent)

        assert parent_copy["age"].description == "foo"

        parent_copy["age"].description = "bar"

        assert parent["age"].description == "foo"
        assert parent_copy["age"].description == "bar"

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

        child_copy = copy.deepcopy(child)
        assert child_copy.__parents__[0] == parent
Beispiel #2
0
    def test_inherit_from_instance_from_multiple_parents(self):
        grand_parent = Model('GrandParent', {
            'grand_parent': fields.String,
        })

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

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

        assert child.__schema__ == {
            'allOf': [{
                '$ref': '#/definitions/GrandParent'
            }, {
                '$ref': '#/definitions/Parent'
            }, {
                'properties': {
                    'extra': {
                        'type': 'string'
                    }
                },
                'type': 'object'
            }]
        }
Beispiel #3
0
    def test_inherit_from_class(self):
        parent = Model('Parent', {
            'name': fields.String,
            'age': fields.Integer,
        })

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

        assert parent.__schema__ == {
            'properties': {
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
            },
            'type': 'object'
        }
        assert child.__schema__ == {
            'allOf': [{
                '$ref': '#/definitions/Parent'
            }, {
                'properties': {
                    'extra': {
                        'type': 'string'
                    }
                },
                'type': 'object'
            }]
        }
Beispiel #4
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'
        }