开发者

Are multiple databases supported by the django testing framework?

开发者 https://www.devze.com 2023-01-25 15:53 出处:网络
I\'m using django 1.2, and I had to setup a second database on my project. As soon as I setup the second connection and the router on my

I'm using django 1.2, and I had to setup a second database on my project. As soon as I setup the second connection and the router on my project, all my test cases which aren't even referring to that second database start to fail. Running the application works fine, syncdb works fine, is just the testing (unit testing) that I'm having problems with.

It appears to me that the second database is never created, and even if I create th开发者_如何转开发at manually as (test_mydbname) it keeps failing.

Is this supported?


I had to add a DefaultRouter, then my tests started working again. Seems like in the test scenario, django doesn't know about a "default" database. All the infos, including test db creation order: https://docs.djangoproject.com/en/dev/topics/testing/advanced/#tests-and-multiple-databases

this is my default router (django 1.7, not so sure about allow_relation and allow_migrate, though

class DefaultRouter(object):
    """
    A router for the default db. add last in settings
    """

    db_label = 'default'

    def db_for_read(self, model, **hints):
        return self.db_label

    def db_for_write(self, model, **hints):
        return self.db_label

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model=None, **hints):
        return True

and in settings.py:

DATABASE_ROUTERS = ['otherapp.dbrouter.CustomDbRouter', 'project.dbrouter.DefaultRouter']
0

精彩评论

暂无评论...
验证码 换一张
取 消