Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup for ModelSerializer tests #5598

Merged
merged 5 commits into from
Nov 22, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 16 additions & 53 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ class Meta:
'non_model_field': 'bar',
})
serializer.is_valid()
with self.assertRaises(TypeError) as excinfo:
serializer.save()

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):
"""
Expand All @@ -147,10 +147,10 @@ class Meta:
serializer = TestSerializer(data={
'afield': 'foo',
})
with self.assertRaises(ValueError) as excinfo:
serializer.is_valid()

msginitial = 'Cannot use ModelSerializer with Abstract Models.'
assert str(excinfo.exception).startswith(msginitial)
with self.assertRaisesMessage(ValueError, msginitial):
serializer.is_valid()


class TestRegularFieldMappings(TestCase):
Expand Down Expand Up @@ -294,10 +294,9 @@ class Meta:
model = RegularFieldsModel
fields = ('auto_field', 'invalid')

with self.assertRaises(ImproperlyConfigured) as excinfo:
TestSerializer().fields
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):
"""
Expand All @@ -311,13 +310,12 @@ class Meta:
model = RegularFieldsModel
fields = ('auto_field',)

with self.assertRaises(AssertionError) as excinfo:
TestSerializer().fields
expected = (
"The field 'missing' was declared on serializer TestSerializer, "
"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):
"""
Expand All @@ -327,13 +325,7 @@ def test_missing_superclass_field(self):
class TestSerializer(serializers.ModelSerializer):
missing = serializers.ReadOnlyField()

class Meta:
model = RegularFieldsModel
fields = '__all__'

class ChildSerializer(TestSerializer):
missing = serializers.ReadOnlyField()

class Meta:
model = RegularFieldsModel
fields = ('auto_field',)
Expand All @@ -348,22 +340,6 @@ class Meta:

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):
def test_duration_field(self):
Expand Down Expand Up @@ -862,44 +838,31 @@ class Meta:
model = MetaClassTestModel
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

exception = result.exception
assert str(exception).startswith(
"The `fields` option must be a list or tuple"
)

def test_meta_class_exclude_option(self):
class ExampleSerializer(serializers.ModelSerializer):
class Meta:
model = MetaClassTestModel
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

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):
class ExampleSerializer(serializers.ModelSerializer):
class Meta:
model = MetaClassTestModel
fields = ('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

exception = result.exception
self.assertEqual(
str(exception),
"Cannot set both 'fields' and 'exclude' options on serializer ExampleSerializer."
)


class Issue2704TestCase(TestCase):
def test_queryset_all(self):
Expand Down