Skip to content

Added support for multi_db #397

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
wants to merge 1 commit into from

Conversation

pmourlanne
Copy link
Contributor

This adds support a marker and a fixture, that make tests behave like Django TestCase's multi_db class attribute (as defined in #76).

I added empty tests to test the added fixture and marker. I'm not sure how to implement them. I'm willing to try if you help me with a few pointers :>

(I have added the same kind of multi_db support for pytest-django 2.9.1, I can do another PR if you are interested)

@@ -124,6 +125,7 @@ def _django_db_fixture_helper(transactional, request, django_db_blocker):
from django.test import TestCase as django_case

test_case = django_case(methodName='__init__')
test_case.multi_db = multi_db
test_case._pre_setup()
request.addfinalizer(test_case._post_teardown)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more universal approach would be to get access to the Django TestCase, where you then could set multi_db on. I will create a PR for that.
/cc @pelme

blueyed added a commit to blueyed/pytest-django that referenced this pull request Nov 21, 2016
This adds `django_db_testcase` and `django_transactional_db_testcase`,
which allows to override them to e.g. enable the `multi_db` feature:

```
@pytest.fixture
def django_db_testcase(django_db_testcase):
    django_db_testcase.multi_db = True
    return django_db_testcase
```

Ref: pytest-dev#397
@blueyed
Copy link
Contributor

blueyed commented Nov 21, 2016

Heh, since multi_db is a class attribute the following (in your conftest.py) is enough after all:

from django.test import TestCase
TestCase.multi_db = True

@blueyed
Copy link
Contributor

blueyed commented Nov 21, 2016

FWIW, I am using the following now as a workaround until something like this and/or #431 gets in:

@pytest.fixture
def multi_db():
    pass

And in a plugin:

def pytest_generate_tests(metafunc):
    if 'multi_db' in metafunc.fixturenames:
        # Enable Django's multi_db feature for tests globally.
        # Can be improved after
        # https://github.com/pytest-dev/pytest-django/pull/431.
        if 'transactional_db' in metafunc.fixturenames:
            from djanto.test import TransactionTestCase
            TransactionTestCase.multi_db = True
        else:
            from django.test import TestCase
            TestCase.multi_db = True

(It could/should hook into / use django_db_modify_db_settings probably instead to be a bit cleaner)

@pmourlanne
Copy link
Contributor Author

@blueyed I made the changes you requested (I think :o)

I still don't know how to write the tests for the new marker and fixture :/

@blueyed
Copy link
Contributor

blueyed commented Nov 22, 2016

Don't worry. Let's wait for some feeback from others.
With e.g. #431 this might not be necessary after all.

Maybe also related (just closed it): #74

Created a `django_multi_db` fixture and added a `multi_db` kwarg to the
`django_db` marker.
@pmourlanne
Copy link
Contributor Author

Hello, I just updated this pull request :)

I had difficulties making the marker work. The solution used here, to check for the marker in _django_db_fixture_helper is not ideal, other solutions are welcome :)
Internal tests should be working with sqlite, I still need to work on other backends.

@bluetech
Copy link
Member

bluetech commented May 7, 2021

I like the approach here, unfortunately multi_db is now deprecated, replaced by databases, so we should change to use that. I'll close this PR, as it's probably better to do that in a new PR, maybe basing on this one.

Thanks @pmourlanne!

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

Successfully merging this pull request may close these issues.

3 participants