有什么可以代替playhouse.test_utils的test_database()函数?

时间:2019-02-07 18:56:59

标签: peewee

我想使用自定义sqlite数据库进行代码的单元测试。 question的答案使用test_database中的playhouse.test_utils

但是,目前那里不可用。

我可以用什么代替它?

1 个答案:

答案 0 :(得分:1)

您可以使用已记录的Database.bind()Database.bind_ctx()方法:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Database.bind_ctx

文档涵盖了这种确切的情况:

MODELS = (User, Account, Note)

# Bind the given models to the db for the duration of wrapped block.
def use_test_database(fn):
    @wraps(fn)
    def inner(self):
        with test_db.bind_ctx(MODELS):
            test_db.create_tables(MODELS)
            try:
                fn(self)
            finally:
                test_db.drop_tables(MODELS)
    return inner


class TestSomething(TestCase):
    @use_test_database
    def test_something(self):
        # ... models are bound to test database ...
        pass