Skip to content

Commit 544967f

Browse files
committed
Test tweaks
1 parent c611a2c commit 544967f

File tree

3 files changed

+58
-64
lines changed

3 files changed

+58
-64
lines changed

rest_framework/serializers.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,10 +795,16 @@ def get_fields(self):
795795
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})
796796

797797
if fields and not isinstance(fields, (list, tuple)):
798-
raise TypeError('`fields` must be a list or tuple')
798+
raise TypeError(
799+
'The `fields` option must be a list or tuple. Got %s.' %
800+
type(fields).__name__
801+
)
799802

800803
if exclude and not isinstance(exclude, (list, tuple)):
801-
raise TypeError('`exclude` must be a list or tuple')
804+
raise TypeError(
805+
'The `exclude` option must be a list or tuple. Got %s.' %
806+
type(exclude).__name__
807+
)
802808

803809
assert not (fields and exclude), "Cannot set both 'fields' and 'exclude'."
804810

tests/test_model_serializer.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,53 @@ class BulkCreateSerializer(serializers.ListSerializer):
559559

560560
# Serializer returns correct data.
561561
assert serializer.data == data
562+
563+
564+
class TestMetaClassModel(models.Model):
565+
text = models.CharField(max_length=100)
566+
567+
568+
class TestSerializerMetaClass(TestCase):
569+
def test_meta_class_fields_option(self):
570+
class ExampleSerializer(serializers.ModelSerializer):
571+
class Meta:
572+
model = TestMetaClassModel
573+
fields = 'text'
574+
575+
with self.assertRaises(TypeError) as result:
576+
ExampleSerializer().fields
577+
578+
exception = result.exception
579+
assert str(exception).startswith(
580+
"The `fields` option must be a list or tuple"
581+
)
582+
583+
def test_meta_class_exclude_option(self):
584+
class ExampleSerializer(serializers.ModelSerializer):
585+
class Meta:
586+
model = TestMetaClassModel
587+
exclude = 'text'
588+
589+
with self.assertRaises(TypeError) as result:
590+
ExampleSerializer().fields
591+
592+
exception = result.exception
593+
assert str(exception).startswith(
594+
"The `exclude` option must be a list or tuple"
595+
)
596+
597+
def test_meta_class_fields_and_exclude_options(self):
598+
class ExampleSerializer(serializers.ModelSerializer):
599+
class Meta:
600+
model = TestMetaClassModel
601+
fields = ('text',)
602+
exclude = ('text',)
603+
604+
with self.assertRaises(AssertionError) as result:
605+
ExampleSerializer().fields
606+
607+
exception = result.exception
608+
self.assertEqual(
609+
str(exception),
610+
"Cannot set both 'fields' and 'exclude'."
611+
)

tests/test_serializer_metaclass.py

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)