Skip to content

Commit

Permalink
Fixed #13352 -- Added __repr__ method for Validation Error. Thanks to…
Browse files Browse the repository at this point in the history
… elpaso66 for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12976 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Apr 15, 2010
1 parent 1cec641 commit d9aaad4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions django/core/exceptions.py
Expand Up @@ -64,6 +64,11 @@ def __str__(self):
return repr(self.message_dict)
return repr(self.messages)

def __repr__(self):
if hasattr(self, 'message_dict'):
return 'ValidationError(%s)' % repr(self.message_dict)
return 'ValidationError(%s)' % repr(self.messages)

def update_error_dict(self, error_dict):
if hasattr(self, 'message_dict'):
if error_dict:
Expand Down
15 changes: 14 additions & 1 deletion tests/modeltests/validators/tests.py
Expand Up @@ -137,7 +137,20 @@ def test_func(self):
# Dynamically assemble a test class with the contents of TEST_DATA

class TestSimpleValidators(TestCase):
pass
def test_single_message(self):
v = ValidationError('Not Valid')
self.assertEquals(str(v), "[u'Not Valid']")
self.assertEquals(repr(v), "ValidationError([u'Not Valid'])")

def test_message_list(self):
v = ValidationError(['First Problem', 'Second Problem'])
self.assertEquals(str(v), "[u'First Problem', u'Second Problem']")
self.assertEquals(repr(v), "ValidationError([u'First Problem', u'Second Problem'])")

def test_message_dict(self):
v = ValidationError({'first': 'First Problem'})
self.assertEquals(str(v), "{'first': 'First Problem'}")
self.assertEquals(repr(v), "ValidationError({'first': 'First Problem'})")

test_counter = 0
for validator, value, expected in TEST_DATA:
Expand Down

0 comments on commit d9aaad4

Please sign in to comment.