Replace assertRaises with assertRaisesMessage

This commit is contained in:
Ryan P Kilby 2017-11-15 14:01:38 -05:00
parent 265375c104
commit 8170f850c3

View File

@ -123,10 +123,10 @@ class TestModelSerializer(TestCase):
'non_model_field': 'bar', 'non_model_field': 'bar',
}) })
serializer.is_valid() serializer.is_valid()
with self.assertRaises(TypeError) as excinfo:
serializer.save()
msginitial = 'Got a `TypeError` when calling `OneFieldModel.objects.create()`.' msginitial = 'Got a `TypeError` when calling `OneFieldModel.objects.create()`.'
assert str(excinfo.exception).startswith(msginitial) with self.assertRaisesMessage(TypeError, msginitial):
serializer.save()
def test_abstract_model(self): def test_abstract_model(self):
""" """
@ -147,10 +147,10 @@ class TestModelSerializer(TestCase):
serializer = TestSerializer(data={ serializer = TestSerializer(data={
'afield': 'foo', 'afield': 'foo',
}) })
with self.assertRaises(ValueError) as excinfo:
serializer.is_valid()
msginitial = 'Cannot use ModelSerializer with Abstract Models.' msginitial = 'Cannot use ModelSerializer with Abstract Models.'
assert str(excinfo.exception).startswith(msginitial) with self.assertRaisesMessage(ValueError, msginitial):
serializer.is_valid()
class TestRegularFieldMappings(TestCase): class TestRegularFieldMappings(TestCase):
@ -294,10 +294,9 @@ class TestRegularFieldMappings(TestCase):
model = RegularFieldsModel model = RegularFieldsModel
fields = ('auto_field', 'invalid') fields = ('auto_field', 'invalid')
with self.assertRaises(ImproperlyConfigured) as excinfo:
TestSerializer().fields
expected = 'Field name `invalid` is not valid for model `RegularFieldsModel`.' expected = 'Field name `invalid` is not valid for model `RegularFieldsModel`.'
assert str(excinfo.exception) == expected with self.assertRaisesMessage(ImproperlyConfigured, expected):
TestSerializer().fields
def test_missing_field(self): def test_missing_field(self):
""" """
@ -311,13 +310,12 @@ class TestRegularFieldMappings(TestCase):
model = RegularFieldsModel model = RegularFieldsModel
fields = ('auto_field',) fields = ('auto_field',)
with self.assertRaises(AssertionError) as excinfo:
TestSerializer().fields
expected = ( expected = (
"The field 'missing' was declared on serializer TestSerializer, " "The field 'missing' was declared on serializer TestSerializer, "
"but has not been included in the 'fields' option." "but has not been included in the 'fields' option."
) )
assert str(excinfo.exception) == expected with self.assertRaisesMessage(AssertionError, expected):
TestSerializer().fields
def test_missing_superclass_field(self): def test_missing_superclass_field(self):
""" """
@ -862,28 +860,20 @@ class TestSerializerMetaClass(TestCase):
model = MetaClassTestModel model = MetaClassTestModel
fields = 'text' fields = 'text'
with self.assertRaises(TypeError) as result: msginitial = "The `fields` option must be a list or tuple"
with self.assertRaisesMessage(TypeError, msginitial):
ExampleSerializer().fields ExampleSerializer().fields
exception = result.exception
assert str(exception).startswith(
"The `fields` option must be a list or tuple"
)
def test_meta_class_exclude_option(self): def test_meta_class_exclude_option(self):
class ExampleSerializer(serializers.ModelSerializer): class ExampleSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = MetaClassTestModel model = MetaClassTestModel
exclude = 'text' exclude = 'text'
with self.assertRaises(TypeError) as result: msginitial = "The `exclude` option must be a list or tuple"
with self.assertRaisesMessage(TypeError, msginitial):
ExampleSerializer().fields ExampleSerializer().fields
exception = result.exception
assert str(exception).startswith(
"The `exclude` option must be a list or tuple"
)
def test_meta_class_fields_and_exclude_options(self): def test_meta_class_fields_and_exclude_options(self):
class ExampleSerializer(serializers.ModelSerializer): class ExampleSerializer(serializers.ModelSerializer):
class Meta: class Meta:
@ -891,15 +881,10 @@ class TestSerializerMetaClass(TestCase):
fields = ('text',) fields = ('text',)
exclude = ('text',) exclude = ('text',)
with self.assertRaises(AssertionError) as result: msginitial = "Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer."
with self.assertRaisesMessage(AssertionError, msginitial):
ExampleSerializer().fields ExampleSerializer().fields
exception = result.exception
self.assertEqual(
str(exception),
"Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer."
)
class Issue2704TestCase(TestCase): class Issue2704TestCase(TestCase):
def test_queryset_all(self): def test_queryset_all(self):