[Python] How to have pytest-django commit data to database instead of in a transaction?

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    There is a conftest.py file that allows me to use my custom transactional fixture fake_user

    @pytest.mark.django_db(transaction=True)
    @pytest.fixture
    def fake_user():
    user = getattr(fake_user, 'user', None)
    if user is None:
    user_data = dict(
    id=1,
    is_superuser=False,
    is_staff=False,
    email='foo@barmail.com',
    username='foobaruser',
    password='passwordmaster',
    date_joined=timezone.now()
    )
    user = User.objects.create(
    **user_data
    )
    user.save()
    # pdb.set_trace()
    fake_user.user = user
    yield user


    Somehow if I debug with pdb.set_trace() the code above, I get User.objects.all() equal to <QuerySet [<User: foobaruser>]>. However there are no any real test DB records. So when querying "User" objects in another high level function, e.g. "GraphQL" or REST call, I get "Users" table being absolutely empty. How could I enable real test DB transactions? Why does't pytest allow any physical records or what prevents them from being inserted?

    Continue reading...

Compartilhe esta Página