python类inlineCallbacks()的实例源码

test_txmongo.py 文件源码 项目:umongo 作者: Scille 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_io_validate_list(self, instance, classroom_model):
        Student = classroom_model.Student
        called = []
        values = [1, 2, 3, 4]

        @inlineCallbacks
        def io_validate(field, value):
            yield called.append(value)

        @instance.register
        class IOStudent(Student):
            io_field = fields.ListField(fields.IntField(io_validate=io_validate))

        student = IOStudent(name='Marty', io_field=values)
        yield student.io_validate()
        assert called == values
test_txmongo.py 文件源码 项目:umongo 作者: Scille 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_pre_post_hooks_with_defers(self, instance):

        events = []

        @instance.register
        class Person(Document):
            name = fields.StrField()
            age = fields.IntField()

            @inlineCallbacks
            def pre_insert(self):
                events.append('start pre_insert')
                yield succeed
                events.append('end pre_insert')

            @inlineCallbacks
            def post_insert(self, ret):
                events.append('start post_insert')
                yield succeed
                events.append('end post_insert')

        p = Person(name='John', age=20)
        yield p.commit()
        assert events == [
            'start pre_insert',
            'end pre_insert',
            'start post_insert',
            'end post_insert'
        ]
test_txmongo.py 文件源码 项目:umongo 作者: Scille 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def test_io_validate_multi_validate(self, instance, classroom_model):
        Student = classroom_model.Student
        called = []

        defer1 = Deferred()
        defer2 = Deferred()
        defer3 = Deferred()
        defer4 = Deferred()

        @inlineCallbacks
        def io_validate11(field, value):
            called.append(1)
            defer1.callback(None)
            yield defer3
            called.append(4)
            defer4.callback(None)

        @inlineCallbacks
        def io_validate12(field, value):
            yield defer4
            called.append(5)

        @inlineCallbacks
        def io_validate21(field, value):
            yield defer2
            called.append(3)
            defer3.callback(None)

        @inlineCallbacks
        def io_validate22(field, value):
            yield defer1
            called.append(2)
            defer2.callback(None)

        @instance.register
        class IOStudent(Student):
            io_field1 = fields.StrField(io_validate=(io_validate11, io_validate12))
            io_field2 = fields.StrField(io_validate=(io_validate21, io_validate22))

        student = IOStudent(name='Marty', io_field1='io1', io_field2='io2')
        yield student.io_validate()
        assert called == [1, 2, 3, 4, 5]


问题


面经


文章

微信
公众号

扫码关注公众号