Unit tests project with multiple application and databases - Circular dependency in TEST[DEPENDENCIES]
Asked Answered
A

1

9

So I've got a django project with several applications.

Each application use its own database, and they share a common database containing django tables (such as auth, sessions).

For this I've got several database routers, so my settings looks like this :

DATABASES = {
    'default': {
     ..
    },
    'app1_db': {
      ..
    },
    'app2_db':{
    ..
    }
}

DATABASE_ROUTERS = ["site.db_router.App1Router", "site.db_router.App2Router"]
# no router for default database

Each application also got its unit tests. To troubleshoot my problem I extracted one of the application. It contains a tests module with four test files. Test file number one looks like below:

class ExcelTestCase(TransactionTestCase):
    databases = ["app1_db"]
    # some tests

Test case 1

python manage.py test app1.tests.testfile1 raises this error django.core.exceptions.ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES]

Test case 2

I comment the databases section : only default test database is created (not the application one) and this error is raised:

AssertionError: Database queries to 'app1_db' are not allowed in this test. Add 'app1_db' to app1.tests.testfile1.ExcelTestCase.databases to ensure proper test isolation and silence this fai
lure.

Test case 3

I uncomment databases setting and run python manage.py test app1.tests so every test are runned.

Three test files out of four are TransactionTestCase and therefore have databases settings.

I need to comment at least the first or third (second one is not TransactionTestCase) test file databases or I get Circulary dependency error, and by doing so I got AssertionError because I can't use the app database.

I'm using Django 2.2 and have been struggling on this issue for a while, any help will be appreciated !

Apulia answered 5/7, 2019 at 8:31 Comment(0)
A
8

Maybe setting your 'DEPENDECIES'.

controlling creation order for test databases

Check that they receive a list of dependencies and the databases without dependencies receive an EMPTY list.

Basically you have to add something like:

'default': {
 ..
    'TEST': {
        'DEPENDENCIES': ['app1_db'],
    },
},
'app1_db': {
# ... db settings
    'TEST': {
        'DEPENDENCIES': [],
    },
}
Araarab answered 6/7, 2019 at 2:40 Comment(1)
Indeed setting dependencies solved the issue, in addition I had to specify databases = {'app1_db', 'default'} in the test Class. Thanks !Apulia

© 2022 - 2024 — McMap. All rights reserved.