user298404
user298404

Reputation: 203

django unit testing on multiple databases

I'm working on a django project where all my unit test cases were working perfectly.

Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.

When I run the tests I get the error,

"KeyError: 'SUPPORTS_TRANSACTIONS'"

It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.

Any ideas on how to have the test script to build the second database.

Upvotes: 7

Views: 10017

Answers (5)

Peko Chan
Peko Chan

Reputation: 364

Referring to that link Django doc Multi-Db you can:

from django.test import TransactionTestCase

class TestMyViews(TransactionTestCase):
    databases = {'default', 'other'} # '__all__' should work too

    def test_index_page_view(self):
        call_some_test_code()

thanks to

@sih4sing5hog5

Upvotes: 2

farridav
farridav

Reputation: 933

I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the multi_db = True flag to my testcase, e.g:

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

Source https://github.com/django/django/blob/master/django/test/testcases.py#L861

This causes django to call flush on all databases (or rollback if they support transactions)

I too am using a db router

I'm afraid I cant find this in Django's documentation, so no link for that

Upvotes: 10

Howard
Howard

Reputation: 1449

Here is a multiple db setup that I currently have in production:

DATABASES = {
    # 'default' is used as the WRITE (master) connection
    DB_PRIMARY_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_PRIMARY_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_PRIMARY_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },

    # 'mail_default' is used as the WRITE (master) connection for the mail database
    DB_MAIL_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_MAIL_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_MAIL_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },
}

DB_PRIMARY_MASTER, DB_PRIMARY_SLAVE, DB_MAIL_MASTER, and DB_MAIL_SLAVE are all string constants so that they can be used in my database router.
Hint: DB_PRIMARY_MASTER='default'

I hope this helps!

Upvotes: 0

RickyA
RickyA

Reputation: 16029

'SUPPORTS_TRANSACTIONS':True worked for me too. However I have a kind of weird multiple db setup using database routers. @user298404: how does your multiple db setup look like?

ps. sorry; not enough points for comment...

Upvotes: 1

Howard
Howard

Reputation: 1449

yes I had a similar problem... my fix was to set 'SUPPORTS_TRANSACTIONS': True for each of the database connections in the settings file. Not sure if this is the correct way to fix it, but it worked for me.

Upvotes: 4

Related Questions