Skip to content
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

Fixture django_db_setup stopped working when upgrading from 4.6 to 4.7 #1115

Open
jeroenbrouwer opened this issue Mar 4, 2024 · 11 comments
Open

Comments

@jeroenbrouwer
Copy link

I've been using a custom django_db_setup fixture which has worked for years. After upgrading from 4.6 to 4.7 this suddenly doesn't work anymore, resulting in the following error:

RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

This might be a bug. Anybody any idea how to fix this from 4.7?
The fixture is something like this:

@pytest.yield_fixture(scope="session", autouse=True)
def django_db_setup(django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

        # At this point the DB setup for the tests is complete
        yield
@dperetti
Copy link

Shouldn't your yield be aligned with the with?

@pytest.yield_fixture(scope="session", autouse=True)
def django_db_setup(django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

    # At this point the DB setup for the tests is complete
    yield

@jeroenbrouwer
Copy link
Author

Thanks for the suggestion, it doesn't solve it unfortunately.

@jeroenbrouwer
Copy link
Author

Somehow it always worked without marking the individual tests with pytest.mark.django_db until now.... Fixed for me, so I'm closing this issue.

@jeroenbrouwer
Copy link
Author

Reopening because the problem still occurs when running this in my CI pipeline. Downgrading to 4.6 fixes it.

@jeroenbrouwer jeroenbrouwer reopened this Mar 28, 2024
@yhay81
Copy link

yhay81 commented Apr 30, 2024

After updating pytest-django from v4.4 to v4.8, I'm experiencing the same database access error despite using @pytest.mark.django_db.
This issue(#1118) also seems to be the same one.

@bluetech
Copy link
Member

bluetech commented Jul 9, 2024

It does work for me, so I would need some way to reproduce it in order to help.

@bluetech
Copy link
Member

bluetech commented Jul 9, 2024

One note, pytest.yield_fixture is deprecated, you should change it to pytest.fixture. Also, you should just remove the yield, it's not really needed (you don't need a hook wrapper, just a regular hook). This might actually help.

@bluetech
Copy link
Member

bluetech commented Jul 9, 2024

Argh sorry I didn't mean hook/hookwrapper, I meant fixture/yield fixture. You should write it like this:

@pytest.fixture(scope="session", autouse=True)
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

I added a django_db_setup parameter, and removed the yield.

@jeroenbrouwer
Copy link
Author

Argh sorry I didn't mean hook/hookwrapper, I meant fixture/yield fixture. You should write it like this:

@pytest.fixture(scope="session", autouse=True)
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        # Code that requires DB access and populates data required for all tests.
        Model.objects.create()

I added a django_db_setup parameter, and removed the yield.

Thanks for the notice about yield_fixture being deprecated. According to the docs it's just an alias of fixture, so changing that shouldn't fix anything. The fixture decorator still supports yield and I'm using that to perform some cleanup after the yield. Not sure how I would go about the cleanup otherwise?

@bluetech
Copy link
Member

bluetech commented Jul 9, 2024

Yes, it's probably fine, just something to try. The important thing is to make sure to request django_db_setup from your own django_db_setup.

@jeroenbrouwer
Copy link
Author

Yes, it's probably fine, just something to try. The important thing is to make sure to request django_db_setup from your own django_db_setup.

The thing is that it always worked, but stopped working with version 4.7+ and only in my CI pipelines... For now I'm just staying on 4.6 as this fixture still works on that version.

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