Reputation: 133
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='[email protected]',
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?
Upvotes: 1
Views: 1923
Reputation: 133
What really helped me:
This usage of django_db_blocker
: https://pytest-django.readthedocs.io/en/latest/database.html#randomize-database-sequences
After inserting the user this way:
@pytest.fixture(scope='session')
def fake_user(django_db_setup, django_db_blocker):
user = getattr(fake_user, 'user', None)
if user is None:
user_data = dict(
id=1,
is_superuser=False,
is_staff=False,
email='[email protected]',
username='foobaruser',
password='passwordmaster',
date_joined=timezone.now()
)
with django_db_blocker.unblock():
user = User.objects.create(
**user_data
)
fake_user.user = user
yield user
the data appeared in Postgres. Thanks!
Upvotes: 0
Reputation: 1323
Pytest flushes the test database between runs; add --reuse-db
to your options to retain the data.
Upvotes: 1