我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用pytest.inlineCallbacks()。
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
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' ]
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]