Skip to content

Testing code with deferred unique constraints gives false positives #786

Open
@tadeoos

Description

@tadeoos

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions