pytest-django
pytest-django copied to clipboard
Testing code with deferred unique constraints gives false positives
Hey,
I have a formset of let's say Animal
objects Animal1(age=1, name='test') and Animal2(age=4, name='rex')
which have a unique constrain on name
field. I want a formset to validate when a user switches the names in the UI. This will normally raise at least a django.db.utils.IntegrityError
.
I managed to achieve that using the postges' deferred unique constraint. What I also needed to do is run formset.save()
inside the transaction.atomic()
block for the deferred constraint to be triggered. When I click through my local build everything works as expected.
Problem comes when I want to test it: the test passes on every setup (with or without the mentioned atomic block) resulting in a false positive.
I guess it has to do with how transactions are handled. I see there is transactional_db
fixture, I've tried to use it but without any luck.
I believe this is a django ticket related to the problem: https://code.djangoproject.com/ticket/20581