mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-14 10:33:17 +03:00
Minor cleanup for ModelSerializer tests (#5598)
* Replace assertRaises with assertRaisesMessage * Remove outdated implicit Meta.fields test * Simplify parent declared field test
This commit is contained in:
parent
134a6f66f9
commit
ae88f5c55b
|
@ -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):
|
||||||
"""
|
"""
|
||||||
|
@ -327,13 +325,7 @@ class TestRegularFieldMappings(TestCase):
|
||||||
class TestSerializer(serializers.ModelSerializer):
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
missing = serializers.ReadOnlyField()
|
missing = serializers.ReadOnlyField()
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = RegularFieldsModel
|
|
||||||
fields = '__all__'
|
|
||||||
|
|
||||||
class ChildSerializer(TestSerializer):
|
class ChildSerializer(TestSerializer):
|
||||||
missing = serializers.ReadOnlyField()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = RegularFieldsModel
|
model = RegularFieldsModel
|
||||||
fields = ('auto_field',)
|
fields = ('auto_field',)
|
||||||
|
@ -348,22 +340,6 @@ class TestRegularFieldMappings(TestCase):
|
||||||
|
|
||||||
ExampleSerializer()
|
ExampleSerializer()
|
||||||
|
|
||||||
def test_fields_and_exclude_behavior(self):
|
|
||||||
class ImplicitFieldsSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = RegularFieldsModel
|
|
||||||
fields = '__all__'
|
|
||||||
|
|
||||||
class ExplicitFieldsSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = RegularFieldsModel
|
|
||||||
fields = '__all__'
|
|
||||||
|
|
||||||
implicit = ImplicitFieldsSerializer()
|
|
||||||
explicit = ExplicitFieldsSerializer()
|
|
||||||
|
|
||||||
assert implicit.data == explicit.data
|
|
||||||
|
|
||||||
|
|
||||||
class TestDurationFieldMapping(TestCase):
|
class TestDurationFieldMapping(TestCase):
|
||||||
def test_duration_field(self):
|
def test_duration_field(self):
|
||||||
|
@ -862,28 +838,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 +859,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."
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_declared_fields_with_exclude_option(self):
|
def test_declared_fields_with_exclude_option(self):
|
||||||
class ExampleSerializer(serializers.ModelSerializer):
|
class ExampleSerializer(serializers.ModelSerializer):
|
||||||
text = serializers.CharField()
|
text = serializers.CharField()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user