3

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?

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

title_exceptions[0] presents a KeyError. The repr of title_exceptions is {'title': [ValidationError(['Title is invalid'])]}, it doesn't appear to be nested?
@alias51: sorry made an error, it should be title_exceptions = exception.error_dict['title'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.