Phoenix
Phoenix

Reputation: 4274

How can add multiple database for tests in Django tests

Question:

How can I add multiple databases for testing in Django?

When I ran my test suits I got this Error:

AssertionError: Database queries to 'mig' are not allowed in this test. Add 'mig' to path_to_test_suit.MigrationServiceTest.databases to ensure proper test isolation and silence this failure.

Here are my PostgreSQL settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_1_name',
        'USER': 'db_1_user',
        'PASSWORD': 'db_1_passwd',
        'HOST': 'db_1_host',
        'PORT': 'db_1_port',
    },
    'mig': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': db_2_name,
        'USER': db_2_user,
        'PASSWORD': 'db_2_passwd',
        'HOST': 'db_2_host',
        'PORT': 'db_2_port',
    },
}

I use Django-nose for running test suits and use Django 2.2

Upvotes: 2

Views: 1844

Answers (2)

M.Void
M.Void

Reputation: 2894

You could use the database parameter in your tests. For example:

class TestYourClass(TestCase):
    databases = {'default', 'mig'}

def test_some_method(self):
    call_some_method()

For more information please see Multi-database support.

P.S. In earliest Django version than 2.2 please use multi_db = True

class TestYourClass(TestCase):
    multi_db = True

Upvotes: 3

Esther
Esther

Reputation: 391

You need to grant DJANGO user the priviledge to write. Then, to make the tests and preserve your data, you have to make a copy of your schema with test_ as prefix and use the keywork --keepdb.

Upvotes: -1

Related Questions