首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >什么可以替代playhouse.test_utils中的test_database ()函数?

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

Stack Overflow用户
提问于 2019-02-08 02:56:59
回答 1查看 238关注 0票数 1

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

但是,这在那里是不可用的。

我可以用什么来替换它?

EN

Stack Overflow用户

回答已采纳

发布于 2019-02-08 05:09:15

您可以使用Database.bind()Database.bind_ctx()方法,这两种方法都有说明:

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

文档中包含的场景正是这样的:

代码语言:javascript
运行
复制
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
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54580385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档