I have a model with a ValidationError
constraint:
class MyModel(models.Model)
title = models.CharField()
def clean(self):
error_dict = {}
if self.title='invalid_title':
error_dict['title'] = ValidationError(
'Title is invalid', code='invalid_title_code')
if error_dict:
raise ValidationError(error_dict)
I want to unittest this ValidationError
, but how do I test the code
is correct? For example:
def test_title_invalid(self):
with self.assertRaises(ValidationError) as cm:
MyModel.objects.create(title='invalid_title')
exception = cm.exception
# Test the key exists
self.assertTrue(hasattr(exception, 'error_dict'))
self.assertIn('title', exception.error_dict)
# Test the message exists
self.assertIn('Title is invalid', exception.messages)
# Test the code exists
self.assertIn('invalid_title_code', exception.code)
This all works fine until self.assertIn('invalid_title_code', exception.code)
, which results in an AttributeError
that states 'ValidationError' object has no attribute 'code'
In the django source for django.core.exceptions.ValidationError
it appears that if you pass a dict
object, the code attribute is not added:
class ValidationError(Exception):
"""An error while validating data."""
def __init__(self, message, code=None, params=None):
super().__init__(message, code, params)
if isinstance(message, dict):
self.error_dict = {}
for field, messages in message.items():
if not isinstance(messages, ValidationError):
messages = ValidationError(messages)
self.error_dict[field] = messages.error_list
else:
self.message = message
self.code = code
self.params = params
self.error_list = [self]
Is this the cause of the problem?
This is a nested exception, you can access this with:
exception = cm.exception
self.assertTrue(hasattr(exception, 'error_dict'))
self.assertIn('title', exception.error_dict)
title_exceptions = exception.error_dict['title']
self.assertEqual(1, len(title_exceptions))
title_exception = title_exceptions[0]
self.assertEqual('invalid_title_code', title_exception.code)