Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are multiple databases supported by the django testing framework?

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 that manually as (test_mydbname) it keeps failing.

Is this supported?

like image 368
user298404 Avatar asked Nov 15 '22 05:11

user298404


1 Answers

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']
like image 170
benzkji Avatar answered Jan 02 '23 20:01

benzkji