具有自动路由功能的多数据库 Django 测试
简单的问题 - 我正在成功使用 multi-db使用 自动路由 设置,如上所述遗留数据库(其中不受管理)。现在我想测试一下。我已经设置了一个测试运行程序来绕过 托管问题,我可以确认我正在按预期创建数据库。
我的问题是数据库路由仍在尝试查看非测试数据库。如何设置 routers.py 文件以在测试模式下查看 test_ 数据库,并在其他时间查看非测试数据库。
应该很简单,但我正在为这个问题而把头撞在墙上。FWIW
:
class PmCatalogRouter(object):
"""A router to control all database operations on models in
the PmCatalog application"""
def db_for_read(self, model, **hints):
"Point all operations on pmCatalog models to 'catalog'"
if model._meta.app_label == 'pmCatalog':
return 'catalog'
return None
def db_for_write(self, model, **hints):
"Point all operations on pmCatalog models to 'catalog'"
if model._meta.app_label == 'pmCatalog':
return 'catalog'
return None
def allow_syncdb(self, db, model):
"Make sure the pmCatalog app only appears on the 'catalog' db"
if db == 'catalog':
return model._meta.app_label == 'pmCatalog'
elif model._meta.app_label == 'pmCatalog':
return False
return None
非常感谢更多的关注;)
谢谢
Simple problem - I'm using multi-db successfully with automatic routing setup as documented on a legacy db (which are unmanaged). Now I want to test it. I've already set a testrunner to get around the managed problem and I can confirm that I am creating the databases and as expected.
My problem is that the database routing is still trying to look at the non-test database. How can I setup my routers.py file to look at the test_ database when in test mode and the non-test database any other time.
Should be simple but I'm beating my head on the wall over this one..
FWIW:
class PmCatalogRouter(object):
"""A router to control all database operations on models in
the PmCatalog application"""
def db_for_read(self, model, **hints):
"Point all operations on pmCatalog models to 'catalog'"
if model._meta.app_label == 'pmCatalog':
return 'catalog'
return None
def db_for_write(self, model, **hints):
"Point all operations on pmCatalog models to 'catalog'"
if model._meta.app_label == 'pmCatalog':
return 'catalog'
return None
def allow_syncdb(self, db, model):
"Make sure the pmCatalog app only appears on the 'catalog' db"
if db == 'catalog':
return model._meta.app_label == 'pmCatalog'
elif model._meta.app_label == 'pmCatalog':
return False
return None
Much appreciate the additional eyeballs on this ;)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧 - 这就是发生的事情。事实证明它一直完全有效,但是两个不同的问题导致我的测试无法通过。在本例中,我正在针对旧方法测试 django 查询方法。我没有通过测试,因为遗留方法不查看测试数据库,而是查看原始数据库。我解决了这个问题,然后我意识到程序没有在测试运行器中创建。
一旦这两个问题得到纠正,一切就神奇地结合在一起了……
有人。
OK - so here's what happened. Turns out it was completely working all along, but two separate issues caused my tests from passing. In this case I am testing the django query methods against the legacy methods. I wasn't passing my test because the legacy methods where not looking at the test database but rather the original database. I fixed that problem and then I realized that the procedures where not getting created in the testrunner.
Once these two problems were corrected everything magically fell together...
HTH someone.