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

Multi db in fixture(scope='session') didn't work #934

Closed
granescb opened this issue May 27, 2021 · 1 comment
Closed

Multi db in fixture(scope='session') didn't work #934

granescb opened this issue May 27, 2021 · 1 comment

Comments

@granescb
Copy link

granescb commented May 27, 2021

Look like if we will use scope session in fuxture that using multi db fixture will not work.
Example:

@pytest.fixture(scope='session')
@pytest.mark.django_db(databases=['default', 'other_db')
def create_unmanaged_db_models():
   #Make some db action
    ...

@pytest.mark.usefixtures('create_unmanaged_db_models')
@pytest.mark.django_db(databases=['default', settings.LEADGEN_DB])
def test_some_case():
    ...

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

But if we will delete scope session code will work fine.
Example:

@pytest.fixture()
@pytest.mark.django_db(databases=['default', 'other_db')
def create_unmanaged_db_models():
   #Make some db action
    ...

@pytest.mark.usefixtures('create_unmanaged_db_models')
@pytest.mark.django_db(databases=['default', settings.LEADGEN_DB])
def test_some_case():
    ...

Package versions:
pytest-django==4.3.0
Django==3.2.3

@bluetech
Copy link
Member

Indeed, doing DB operations in the session scope (or really, any scope above function) is not supported. It is not related to multi-db, just a general constraint. The reason is that DB objects need to be created in the context of a test, so they could be cleaned up/rolled back at the end of it.

Closed as a duplicate of #105.

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

2 participants