Refs #26022 -- Used context manager version of assertRaises in tests.

This commit is contained in:
Hasan 2016-01-17 14:56:39 +03:30 committed by Tim Graham
parent 575706331b
commit 3d0dcd7f5a
118 changed files with 1086 additions and 760 deletions

View file

@ -83,8 +83,10 @@ class ImageFieldTestMixin(SerializeMixin):
field = getattr(instance, field_name)
# Check height/width attributes of field.
if width is None and height is None:
self.assertRaises(ValueError, getattr, field, 'width')
self.assertRaises(ValueError, getattr, field, 'height')
with self.assertRaises(ValueError):
getattr(field, 'width')
with self.assertRaises(ValueError):
getattr(field, 'height')
else:
self.assertEqual(field.width, width)
self.assertEqual(field.height, height)

View file

@ -129,7 +129,8 @@ class DecimalFieldTests(test.TestCase):
f = models.DecimalField(max_digits=4, decimal_places=2)
self.assertEqual(f.to_python(3), Decimal("3"))
self.assertEqual(f.to_python("3.14"), Decimal("3.14"))
self.assertRaises(ValidationError, f.to_python, "abc")
with self.assertRaises(ValidationError):
f.to_python("abc")
def test_default(self):
f = models.DecimalField(default=Decimal("0.00"))
@ -598,7 +599,8 @@ class SlugFieldTests(test.TestCase):
class ValidationTest(test.SimpleTestCase):
def test_charfield_raises_error_on_empty_string(self):
f = models.CharField()
self.assertRaises(ValidationError, f.clean, "", None)
with self.assertRaises(ValidationError):
f.clean("", None)
def test_charfield_cleans_empty_string_when_blank_true(self):
f = models.CharField(blank=True)
@ -610,7 +612,8 @@ class ValidationTest(test.SimpleTestCase):
def test_integerfield_raises_error_on_invalid_intput(self):
f = models.IntegerField()
self.assertRaises(ValidationError, f.clean, "a", None)
with self.assertRaises(ValidationError):
f.clean("a", None)
def test_charfield_with_choices_cleans_valid_choice(self):
f = models.CharField(max_length=1,
@ -619,7 +622,8 @@ class ValidationTest(test.SimpleTestCase):
def test_charfield_with_choices_raises_error_on_invalid_choice(self):
f = models.CharField(choices=[('a', 'A'), ('b', 'B')])
self.assertRaises(ValidationError, f.clean, "not a", None)
with self.assertRaises(ValidationError):
f.clean("not a", None)
def test_charfield_get_choices_with_blank_defined(self):
f = models.CharField(choices=[('', '<><>'), ('a', 'A')])
@ -639,7 +643,8 @@ class ValidationTest(test.SimpleTestCase):
def test_nullable_integerfield_raises_error_with_blank_false(self):
f = models.IntegerField(null=True, blank=False)
self.assertRaises(ValidationError, f.clean, None, None)
with self.assertRaises(ValidationError):
f.clean(None, None)
def test_nullable_integerfield_cleans_none_on_null_and_blank_true(self):
f = models.IntegerField(null=True, blank=True)
@ -647,16 +652,20 @@ class ValidationTest(test.SimpleTestCase):
def test_integerfield_raises_error_on_empty_input(self):
f = models.IntegerField(null=False)
self.assertRaises(ValidationError, f.clean, None, None)
self.assertRaises(ValidationError, f.clean, '', None)
with self.assertRaises(ValidationError):
f.clean(None, None)
with self.assertRaises(ValidationError):
f.clean('', None)
def test_integerfield_validates_zero_against_choices(self):
f = models.IntegerField(choices=((1, 1),))
self.assertRaises(ValidationError, f.clean, '0', None)
with self.assertRaises(ValidationError):
f.clean('0', None)
def test_charfield_raises_error_on_empty_input(self):
f = models.CharField(null=False)
self.assertRaises(ValidationError, f.clean, None, None)
with self.assertRaises(ValidationError):
f.clean(None, None)
def test_datefield_cleans_date(self):
f = models.DateField()
@ -664,7 +673,8 @@ class ValidationTest(test.SimpleTestCase):
def test_boolean_field_doesnt_accept_empty_input(self):
f = models.BooleanField()
self.assertRaises(ValidationError, f.clean, None, None)
with self.assertRaises(ValidationError):
f.clean(None, None)
class IntegerFieldTests(test.TestCase):
@ -864,7 +874,8 @@ class BinaryFieldTests(test.TestCase):
def test_max_length(self):
dm = DataModel(short_data=self.binary_data * 4)
self.assertRaises(ValidationError, dm.full_clean)
with self.assertRaises(ValidationError):
dm.full_clean()
class GenericIPAddressFieldTests(test.TestCase):
@ -875,10 +886,12 @@ class GenericIPAddressFieldTests(test.TestCase):
"""
model_field = models.GenericIPAddressField(protocol='IPv4')
form_field = model_field.formfield()
self.assertRaises(ValidationError, form_field.clean, '::1')
with self.assertRaises(ValidationError):
form_field.clean('::1')
model_field = models.GenericIPAddressField(protocol='IPv6')
form_field = model_field.formfield()
self.assertRaises(ValidationError, form_field.clean, '127.0.0.1')
with self.assertRaises(ValidationError):
form_field.clean('127.0.0.1')
def test_null_value(self):
"""