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

Suggestion: module-level fixtures for populating a database #1145

Open
nbelakovski-mssm opened this issue Sep 13, 2024 · 0 comments
Open

Suggestion: module-level fixtures for populating a database #1145

nbelakovski-mssm opened this issue Sep 13, 2024 · 0 comments

Comments

@nbelakovski-mssm
Copy link

nbelakovski-mssm commented Sep 13, 2024

I had a lot of trouble figuring out how to set up my tests given that various tests needed different things in the database, and some tests used live_server which removes all data from the database at the end of the particular test (this prevented me from using a session level fixture, since the live_server test would get rid of all the data that the session level fixture adds to the db)

I also didn't want tests to interfere with each other, and I finally came up with the following solution:

from django.db import transaction 
import pytest

@pytest.fixture(scope='module')
def self_restoring_test_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        with transaction.atomic():
            sid = transaction.savepoint()
            yield
            transaction.savepoint_rollback(sid)

However, this feels like something the plugin should provide. I tried using various fixtures like db and transactional_db, but I kept getting issues with ScopeMismatch because they are scoped at the default function level.

I also tried various things to get the live_server tests to roll back the database to where it had been before with serialized_rollback and such things, but they didn't work. Actually the solution above could probably be applied to a live_server test just as effectively.

I'm not entirely sure how a fixture like the above would fit within this plugin but I hope you find it helpful.

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

1 participant