具有自动路由功能的多数据库 Django 测试

发布于 2024-11-18 02:38:08 字数 1462 浏览 10 评论 0原文

简单的问题 - 我正在成功使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Bonjour°[大白 2024-11-25 02:38:08

好吧 - 这就是发生的事情。事实证明它一直完全有效,但是两个不同的问题导致我的测试无法通过。在本例中,我正在针对旧方法测试 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文