Testing django module in multiple databases

I am working on a django project where all my unit test cases work perfectly.

Soon, when I introduced the second database, all of my test cases that inherit from TestCase were broken. At this point, I did not create any test case for this second database, but my router is working fine.

When I run the tests, I get an error,

"KeyError:" SUPPORTS_TRANSACTIONS "

It seems to me that I'm trying to verify that all the databases that I have installed support transactions, but the second database is never created.

Any ideas on how to run a test script to create a second database.

+3
source share
4 answers

... "SUPPORTS_TRANSACTIONS": True . , , .

+3

, , , multi_db = True , :

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

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

, django flush ( , ).

db

, Django,

+2

"SUPPORTS_TRANSACTIONS": . db . @user298404: db?

. ; ...

0

db, :

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 DB_MAIL_SLAVE - , .
: DB_PRIMARY_MASTER = 'default'

, !

0

Source: https://habr.com/ru/post/1774436/


All Articles