python类validates()的实例源码

test_model.py 文件源码 项目:dynamorm 作者: NerdWalletOSS 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def test_explicit_schema_parents():
    """Inner Schema classes should be able to have explicit parents"""
    class SuperMixin(object):
        bbq = String()

    if 'marshmallow' in (os.getenv('SERIALIZATION_PKG') or ''):
        class Mixin(SuperMixin):
            is_mixin = True
            bar = String()

            @validates('bar')
            def validate_bar(self, value):
                if value != 'bar':
                    raise SchemaValidationError('bar must be bar')
    else:
        class Mixin(SuperMixin):
            is_mixin = True
            bar = String()

            def validate_bar(self, data, value):
                if value != 'bar':
                    raise SchemaValidationError('bar must be bar')

    class Model(DynaModel):
        class Table:
            name = 'table'
            hash_key = 'foo'
            read = 1
            write = 1

        class Schema(Mixin):
            foo = Number(required=True)
            baz = String(required=True)

    assert Model.Schema.is_mixin is True
    assert list(sorted(Model.Schema.dynamorm_fields().keys())) == ['bar', 'baz', 'bbq', 'foo']

    with pytest.raises(ValidationError):
        Model(foo='foo', baz='baz', bar='not bar')


问题


面经


文章

微信
公众号

扫码关注公众号