1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Clean up unique validation tests to not use bare excepts and use assertRaises. Refs [16345].

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16348 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Andrew Godwin 2011-06-09 19:35:44 +00:00
parent dce278673a
commit da0c7cd777

View File

@ -83,12 +83,9 @@ class PerformUniqueChecksTest(TestCase):
slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3)) slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3)) p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
try: with self.assertRaises(ValidationError) as cm:
p.full_clean() p.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'title': [u'Title must be unique for Posted date.']})
self.assertEqual(e.message_dict, {'title': [u'Title must be unique for Posted date.']})
else:
self.fail('unique_for_date checks should catch this.')
# Should work without errors # Should work without errors
p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3)) p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
@ -99,28 +96,19 @@ class PerformUniqueChecksTest(TestCase):
p.full_clean() p.full_clean()
p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1)) p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
try: with self.assertRaises(ValidationError) as cm:
p.full_clean() p.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
self.assertEqual(e.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
else:
self.fail('unique_for_year checks should catch this.')
p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30)) p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
try: with self.assertRaises(ValidationError) as cm:
p.full_clean() p.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
self.assertEqual(e.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
else:
self.fail('unique_for_month checks should catch this.')
p = Post(title="Django 1.0 is released") p = Post(title="Django 1.0 is released")
try: with self.assertRaises(ValidationError) as cm:
p.full_clean() p.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'posted': [u'This field cannot be null.']})
self.assertEqual(e.message_dict, {'posted': [u'This field cannot be null.']})
else:
self.fail("Model validation shouldn't allow an absent value for a DateField without null=True.")
def test_unique_for_date_with_nullable_date(self): def test_unique_for_date_with_nullable_date(self):
p1 = FlexibleDatePost.objects.create(title="Django 1.0 is released", p1 = FlexibleDatePost.objects.create(title="Django 1.0 is released",
@ -131,40 +119,28 @@ class PerformUniqueChecksTest(TestCase):
p.full_clean() p.full_clean()
except ValidationError, e: except ValidationError, e:
self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.") self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
except:
self.fail("unique_for_date checks shouldn't explode when the associated DateField is None.")
p = FlexibleDatePost(slug="Django 1.0") p = FlexibleDatePost(slug="Django 1.0")
try: try:
p.full_clean() p.full_clean()
except ValidationError, e: except ValidationError, e:
self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.") self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
except:
self.fail("unique_for_year checks shouldn't explode when the associated DateField is None.")
p = FlexibleDatePost(subtitle="Finally") p = FlexibleDatePost(subtitle="Finally")
try: try:
p.full_clean() p.full_clean()
except ValidationError, e: except ValidationError, e:
self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.") self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
except:
self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
def test_unique_errors(self): def test_unique_errors(self):
m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10) m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10)
m = UniqueErrorsModel(name='Some Name', number=11) m = UniqueErrorsModel(name='Some Name', number=11)
try: with self.assertRaises(ValidationError) as cm:
m.full_clean() m.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'name': [u'Custom unique name message.']})
self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']})
except:
self.fail('unique checks should catch this.')
m = UniqueErrorsModel(name='Some Other Name', number=10) m = UniqueErrorsModel(name='Some Other Name', number=10)
try: with self.assertRaises(ValidationError) as cm:
m.full_clean() m.full_clean()
except ValidationError, e: self.assertEqual(cm.exception.message_dict, {'number': [u'Custom unique number message.']})
self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']})
except:
self.fail('unique checks should catch this.')