有什么可以从playhouse.test_utils替换test_database()函数?
What can replace the test_database () function from playhouse.test_utils?
我想使用自定义 sqlite 数据库对代码进行单元测试。
question 的答案使用 playhouse.test_utils
中的 test_database
。
但是,目前那里不可用。
我可以用什么代替它?
您可以使用 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
我想使用自定义 sqlite 数据库对代码进行单元测试。
question 的答案使用 playhouse.test_utils
中的 test_database
。
但是,目前那里不可用。
我可以用什么代替它?
您可以使用 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