Testing a django app with multiple databases?

The application is divided into two parts and has two bases 'records' and 'django'
settings.py:
DATABASES = {
 'default': {},
 'records': {
 'ENGINE': 'django.db.backends.postgresql',
................
},
 'django': {
 'ENGINE': 'django.db.backends.postgresql',
..............
},
}

ranting the logic
routers.py:
class AuthRouter(object):
 labels = ['auth', 'user_profile', 'sessions', 'admin', 'contenttypes', 'test']
 labels_relation = ['auth', 'user_profile']
 def db_for_read(self, model, **hints):
"""
 Attempts to read auth models go to auth_db.
"""
 if model._meta.app_label in self.labels:
 return 'django'

 def db_for_write(self, model, **hints):
"""
 Attempts to write auth models go to auth_db.
"""
 if model._meta.app_label in self.labels:
 return 'django'

 def allow_relation(self, obj1, obj2, **hints):
"""
 Allow relations if a model in the auth app is involved.
"""
 if obj1._meta.app_label in self.labels or obj2._meta.app_label in self.labels:
 return True

 def allow_migrate(self, db, app_label, model=None, **hints):
 if app_label in self.labels:
 return db == 'django'


class RecordsRouter(object):
 labels = ['records']

 def db_for_read(self, model, **hints):
 if model._meta.app_label in self.labels:
 return 'records'

 def db_for_write(self, model, **hints):
 if model._meta.app_label in self.labels:
 return 'records'

 def allow_relation(self, obj1, obj2, **hints):
 if obj1._meta.app_label in self.labels or obj2._meta.app_label in self.labels:
 return True


 def allow_migrate(self, db, app_label, model=None, **hints):
 if app_label in self.labels:
 return db == 'records'
 elif db == 'records':
 return False


tests.py:
class RecordFlowTestCase(TransactionTestCase):
 databases = {'records'}

traceback:
Traceback (most recent call last):
 File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
 self.execute(*args, **cmd_options)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
 output = self.handle(*args, **options)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/core/management/commands/test.py", line 53, in handle
 failures = test_runner.run_tests(test_labels)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/test/runner.py", line 629, in run_tests
 old_config = self.setup_databases(aliases=databases)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/test/runner.py", line 554, in setup_databases
 self.parallel, **kwargs
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/test/utils.py", line 157, in setup_databases
 test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/test/utils.py", line 283, in get_unique_databases_and_mirrors
 test_databases = dependency_ordered(test_databases.items(), dependencies)
 File "/home/workspace/PycharmProjects/parusarium/venv/lib/python3.6/site-packages/django/test/utils.py", line 236, in dependency_ordered
 raise improperly configured("Circular dependency in TEST[DEPENDENCIES]")
django.core.exceptions.Improperly configured: Circular dependency in TEST[DEPENDENCIES]
March 20th 20 at 11:23
1 answer
March 20th 20 at 11:25
Solution
To answer the question at least need the part with the dots of settings.DATABASES; in addition, it may be sufficient that the tests were performed in one common database (with the caveat that the router is covered by individual tests)
Okay, seems clear.
if 'test' in sys.argv:
 DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.postgresql',
........................
}
}
else:
 DATABASES = {
 'default': {},
 'records': {
 'ENGINE': 'django.db.backends.postgresql',
.....................
},
 'django': {
 'ENGINE': 'django.db.backends.postgresql',
.......................
},
}
 DATABASE_ROUTERS = ['parusarium.routers.RecordsRouter', 'parusarium.routers.AuthRouter']
- Bethel_Renner commented on March 20th 20 at 11:31
I'm not stupid, and the bases really are different.
How to perform the configuration tests to be performed in one database? I tried using the MIRROR, but the problem is the same.
The router is not covered by the individual tests, I don't even know how to do this now - Bethel_Renner commented on March 20th 20 at 11:28
@Otilia_Witting81, the direction is correct.
The router is covered by simple unit tests which names the database it returns for specific models in different situations - cleora.Kertzmann commented on March 20th 20 at 11:34

Find more questions by tags Django