How to write unittest for django project that using two database (mysql and mongo)

163 Views Asked by At

My django projects use two type of database (mariadb and mongoengine) I have no idea how to write the unittest that test both of them in the same testcase. please guide me.

now i try to use fixtures_mongoengine to write fixture mongo data but that cannot use with mysql fixture in the same testcase

1

There are 1 best solutions below

0
Allen Shaw On

If you are using multiple database like:

# settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'other': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'other.sqlite3',
    }
}

You may use TransactionTestCase like:

# test.py
class TestMyViews(TransactionTestCase):
    databases = {'default', 'other'}

    def test_index_page_view(self):
        call_some_test_code()

reference the document here.

I don't know if mongoengine works, you could have a try.