Skip to content

Expose fixtures to change Django's {Transaction,}TestCase #431

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 2 commits into from

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented 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: #397

TODO:

  • doc
  • test(s) - not sure what should be tested here? Maybe the multi_db feature (which requires to setup multiple DBs though)

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 Author

blueyed commented Nov 21, 2016

Not really required for multi_db after all, in case you want to change it globally (#397 (comment)).

This PR makes still sense, and is needed in case you want to use some specialized/derived class, especially since it's only required for a subset of tests usually:

from django.test import TestCase

class MultiDbTestCase(TestCase):
    multi_db = True

@pytest.fixture
def django_db_testcase():
    return MultiDbTestCase

@Alphare
Copy link

Alphare commented Apr 4, 2018

Is there any way I could help with furthering the support for multiple databases?
I have a multi-tenant system with a connection for each tenant and I need to get testing quite soon.

@callmesangio
Copy link

+1
Having this merged would be great!
I'm finding myself dealing with django and multiple DBs...

@blueyed blueyed mentioned this pull request Jun 26, 2018
@chris-erickson
Copy link

We'd like to support read replicas on Amazon Aurora/Postgres and this is one impediment to doing it confidently. We have a vast test suite that would be a lot of work to rework with the Django TestCase. I've tried various versions of ideas suggested here and none seem to solve the multi-db problem.

However, if I write a router with

    def db_for_read(self, model, **hints):
        return "default"

    def db_for_write(self, model, **hints):
        return "default"

it works as one would expect. Of course this doesn't really test multiple databases in the way one would want, but I think that's more of a system dynamics problem that I assume is accounted for (replica lag and it's ramifications).

Any clarity on the "best" way to hack this in right now would also be appreciated, I see a few different examples but none seem to do it, that I've tried.

@bluetech
Copy link
Member

bluetech commented May 7, 2021

In my opinion this is a bit of a cop-out, I'd like us to try and provide proper interfaces as much as we can. The major one ATM it seems is multi-db support (database attribute), let's work on supporting that (there are many issues tracking that). So I'll close this for now.

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.

5 participants