pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

Testing code with deferred unique constraints gives false positives

Open tadeoos opened this issue 5 years ago • 1 comments

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.

tadeoos avatar Nov 25 '19 16:11 tadeoos

I believe this is a django ticket related to the problem: https://code.djangoproject.com/ticket/20581

tadeoos avatar Nov 26 '19 09:11 tadeoos