Skip to content

Multi db tests behave differently on different dbs #423

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
obiwanus opened this issue Nov 18, 2016 · 8 comments
Closed

Multi db tests behave differently on different dbs #423

obiwanus opened this issue Nov 18, 2016 · 8 comments

Comments

@obiwanus
Copy link

I have two identical sqlite databases for testing

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'first',
    },
    'second': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'second',
    },
}

At the start of each test the default database is clean, but the second database contains the data from the previous test runs.
The transaction setting pytestmark = pytest.mark.django_db(transaction=True) doesn't seem to have any effect on the default database behaviour - it is always clean for every test regardless of whether transaction is True or False. The same is true for the second database except it's always dirty.

@blueyed
Copy link
Contributor

blueyed commented Nov 18, 2016

Are you using --reuse-db and/or --no-migrations?
What Django and pytest-django versions are you using?

IIRC Django only runs management commands (including migrations etc) on the default DB only.
Not sure about tests though - although we should be using Django's behavior/methods there already.

@obiwanus
Copy link
Author

I'm not using --reuse-db or --no-migrations, just running pytest tests.

pytest-django==3.0.0, Django==1.10.3

My second database has no migrations, so I assume Django only does syncdb for it. I don't see though how this would affect tests - migrations should only be applied once before running the whole suite anyway, not between test runs, right?

@obiwanus
Copy link
Author

I'll try to create a minimal working example to reproduce the issue on Monday

@obiwanus
Copy link
Author

OK I've created a repo with a minimal example that should demonstrate the issue

https://github.com/obiwanus/pytest-django-bug

Please let me know if you have any problems with it. Thanks

@blueyed
Copy link
Contributor

blueyed commented Nov 24, 2016

I think a failing test (as a PR) would be better. But the above might be enough for others to look at already.

@vbarbaresi
Copy link
Member

Django only deals with the default database per default, as an optimization. Link to the doc
You have to wrap the test into a class and define multi_db = True for it to handle the 2 databases.

I found this in pytest-django doc: https://github.com/pytest-dev/pytest-django/blob/master/docs/database.rst#tests-requiring-multiple-databases

To implement the option in pytest-django without having to create classes, I hacked here:

diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py
index 692e061..e95122e 100644
--- a/pytest_django/fixtures.py
+++ b/pytest_django/fixtures.py
@@ -119,6 +119,7 @@ def _django_db_fixture_helper(transactional, request, django_db_blocker):
     else:
         from django.test import TestCase as django_case

+    django_case.multi_db = True
     test_case = django_case(methodName='__init__')
     test_case._pre_setup()
     request.addfinalizer(test_case._post_teardown)

We could enable this on demand with a pytest.mark.multi_db marker, what do you think?

@blueyed
Copy link
Contributor

blueyed commented Oct 26, 2017

@vbarbaresi
See #431.

@bluetech
Copy link
Member

bluetech commented May 7, 2021

I'll close this issue as a duplicate of general multi-db support.

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

No branches or pull requests

4 participants