Skip to content

Test helpers #43

@samueljsb

Description

@samueljsb

I sometimes want to assert that a particular integrity error was raised during a test. At the moment I can do:

import pytest
from django.db.utils import IntegrityError

def test_():
    with pytest.raises(IntegrityError):
        ...

But, if I want to know which integrity error I'm catching, I need to either do some regexp matching on its message, or dig into the internals like this library does.

I'd love to be able to do:

from django_integrity import conversion, testing

def test_():
    with testing.raises(
        conversion.Unique(model=User, fields=("email",))
    ):
        ...

alternatives

I could also wrap the existing conversion code:

from django_integrity import conversion

def test_():
    class _MyIntegrityError(Exception):
        pass

   with (
        pytest.raises(_MyIntegrityError),
        conversion.refine_integrity_error(
            [(conversion.Unique(model=User, fields=("email",)), _MyIntegrityError)]
        ),
    ):
        ...

But that's a bit awkward. A helper for it would be so much easier to use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions