Skip to content

testing with django's mirroring db #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stavinsky opened this issue Jun 16, 2016 · 2 comments
Closed

testing with django's mirroring db #342

stavinsky opened this issue Jun 16, 2016 · 2 comments

Comments

@stavinsky
Copy link

Hello.
Is any way to use Django's mirror db function like here?

@blueyed
Copy link
Contributor

blueyed commented Jul 15, 2016

I don't think so.
See "Tests requiring multiple databases" in docs/database.rst.

@pelme
I think for this we should provide a fixture that can be overwritten and wraps the Django TestCase class?

Django uses this _database_names method: https://github.com/django/django/blob/ab2f5f764a2f6db97e23cccd5c4f5abbb43d1caf/django/test/testcases.py#L869-L877

I have tried the following:

@pytest.fixture
def use_multidb_for_django_testcases(monkeypatch):
    monkeypatch.setattr('django.test.TransactionTestCase.multi_db', True,
                        raising=False)


@pytest.fixture
def django_multidb(use_multidb_for_django_testcases, db):
    return db


def test_foo(django_multidb, …):

With --reuse-db I am getting this error on teardown:

..…/Vcs/django/django/test/testcases.py:924: in _post_teardown
    self._fixture_teardown()
..…/Vcs/django/django/test/testcases.py:1078: in _fixture_teardown
    self._rollback_atomics(self.atomics)
..…/Vcs/django/django/test/testcases.py:1020: in _rollback_atomics
    transaction.set_rollback(True, using=db_name)
..…/Vcs/django/django/db/transaction.py:103: in set_rollback
    return get_connection(using).set_rollback(rollback)
..…/Vcs/django/django/db/backends/base/base.py:415: in set_rollback
    "The rollback flag doesn't work outside of an 'atomic' block.")
E   django.db.transaction.TransactionManagementError: The rollback flag doesn't work outside of an 'atomic' block.

With --create-db it results in:

PASSED
project/app/tests/test_models.py::test_db_router ERRORDestroying test database for alias 'replica' ('test_project')...
Traceback (most recent call last):
  File "…/Vcs/django/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: database "test_project" is being accessed by other users
DETAIL:  There is 1 other session using the database.

@bluetech
Copy link
Member

bluetech commented May 7, 2021

I think once we have multi-db support, then this should follow naturally, so I'll close as a duplicate (there are many issues about mutli db).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants