Search code examples
djangodjango-modelsdjango-unittestdjango-constraints

Django unit test constraints


So I have a problem in writing my unit tests for a Check and Unique constraint. Here are the their definition:

# assert that a partner can have only one headquarter
constraints = [
   models.UniqueConstraint(
       fields=['partner', 'active'],
       condition=models.Q(headquarter=True),
       name='HQ already set.'
     )
]


# assert every partner contant has at least one of email/phone number pair defined
constraints = [            
   models.CheckConstraint(
       check= (
                 models.Q(email__isnull=False) &
                 models.Q(phone_number__isnull=False)
              ),
       name='E-mail or phone number should be set.'
   )
]

And the unit test asserts which fails:

from django.db.utils import IntegrityError
....
# HQ already defined so it should work
with self.assertRaises(Exception) as raised:
    partnerHQCopy.save()

self.assertEqual(IntegrityError, type(raised.exception))

.....
# The contact added has both email and phone number None
with self.assertRaises(Exception) as raised:
    partnerContact.save()

self.assertEqual(IntegrityError, type(raised.exception))

And the traceback:

======================================================================
ERROR: test_unique_hq_for_partner (partners.tests.PartnerModelsTestCases)
----------------------------------------------------------------------

.....

sqlite3.IntegrityError: UNIQUE constraint failed: partners_partnerbranch.partner_id, partners_partnerbranch.active

.....

django.db.utils.IntegrityError: UNIQUE constraint failed: partners_partnerbranch.partner_id, partners_partnerbranch.active

======================================================================
ERROR: test_partner_contact_no_email_no_phone (partners.tests.PartnerModelsTestCases)
----------------------------------------------------------------------
.....

sqlite3.IntegrityError: CHECK constraint failed: E-mail or phone number should be set.

.....

django.db.utils.IntegrityError: CHECK constraint failed: E-mail or phone number should be set.


Solution

  • The reason these tests fail is because it raises an sqlite3.IntegrityError, not a django.db.utils.IntegrityError, you thus should assert the correct type of error:

    from sqlite3 import IntegrityError
    
    # …
    
    # HQ already defined so it should work
    with self.assertRaises(IntegrityError):
        partnerHQCopy.save()
    
    # …
    
    # The contact added has both email and phone number None
    with self.assertRaises(IntegrityError):
        partnerContact.save()