Answer the question
In order to leave comments, you need to log in
Testing a django app with multiple bases?
The application is divided into two parts, and has two databases 'records' and 'django'
settings.py:
DATABASES = {
'default': {},
'records': {
'ENGINE': 'django.db.backends.postgresql',
................
},
'django': {
'ENGINE': 'django.db.backends.postgresql',
..............
},
}
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
class RecordFlowTestCase(TransactionTestCase):
databases = {'records'}
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 ImproperlyConfigured("Circular dependency in TEST[DEPENDENCIES]")
django.core.exceptions.ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES]
Answer the question
In order to leave comments, you need to log in
At least the ellipsis part of settings.DATABASES is needed to answer the question; in addition, it may well be enough that the tests are performed in one common database (with the proviso that the router is covered by separate tests)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question