@FulTupFul

Тестирование django приложения с несколькими базами?

Приложение разделено на две части, и имеет две базы 'records' и 'django'
settings.py:
DATABASES = {
    'default': {},
    'records': {
        'ENGINE': 'django.db.backends.postgresql',
        ................
    },
    'django': {
        'ENGINE': 'django.db.backends.postgresql',
        ..............
    },
}

роуты разделяют логику
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 ImproperlyConfigured("Circular dependency in TEST[DEPENDENCIES]")
django.core.exceptions.ImproperlyConfigured: Circular dependency in TEST[DEPENDENCIES]
  • Вопрос задан
  • 154 просмотра
Решения вопроса 1
tumbler
@tumbler Куратор тега Django
бекенд-разработчик на python
Для ответа на вопрос как минимум нужна часть с многоточиями из settings.DATABASES; кроме того, вполне может быть достаточно, чтобы тесты выполнялись в одной общей БД (с оговоркой что роутер покрыт отдельными тестами)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы