Pytest-django: testing with django's mirroring db

Created on 16 Jun 2016  Â·  2Comments  Â·  Source: pytest-dev/pytest-django

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

db-configuration

Most helpful comment

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:

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.

All 2 comments

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:

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.

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).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dan-passaro picture dan-passaro  Â·  4Comments

clintonb picture clintonb  Â·  4Comments

ojake picture ojake  Â·  6Comments

rodrigorodriguescosta picture rodrigorodriguescosta  Â·  4Comments

WoLpH picture WoLpH  Â·  7Comments