mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Test tweaks
This commit is contained in:
parent
c611a2c1fe
commit
544967f36e
|
@ -795,10 +795,16 @@ class ModelSerializer(Serializer):
|
||||||
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})
|
extra_kwargs = getattr(self.Meta, 'extra_kwargs', {})
|
||||||
|
|
||||||
if fields and not isinstance(fields, (list, tuple)):
|
if fields and not isinstance(fields, (list, tuple)):
|
||||||
raise TypeError('`fields` must be a list or tuple')
|
raise TypeError(
|
||||||
|
'The `fields` option must be a list or tuple. Got %s.' %
|
||||||
|
type(fields).__name__
|
||||||
|
)
|
||||||
|
|
||||||
if exclude and not isinstance(exclude, (list, tuple)):
|
if exclude and not isinstance(exclude, (list, tuple)):
|
||||||
raise TypeError('`exclude` must be a list or tuple')
|
raise TypeError(
|
||||||
|
'The `exclude` option must be a list or tuple. Got %s.' %
|
||||||
|
type(exclude).__name__
|
||||||
|
)
|
||||||
|
|
||||||
assert not (fields and exclude), "Cannot set both 'fields' and 'exclude'."
|
assert not (fields and exclude), "Cannot set both 'fields' and 'exclude'."
|
||||||
|
|
||||||
|
|
|
@ -559,3 +559,53 @@ class TestBulkCreate(TestCase):
|
||||||
|
|
||||||
# Serializer returns correct data.
|
# Serializer returns correct data.
|
||||||
assert serializer.data == data
|
assert serializer.data == data
|
||||||
|
|
||||||
|
|
||||||
|
class TestMetaClassModel(models.Model):
|
||||||
|
text = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
class TestSerializerMetaClass(TestCase):
|
||||||
|
def test_meta_class_fields_option(self):
|
||||||
|
class ExampleSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = TestMetaClassModel
|
||||||
|
fields = 'text'
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError) as result:
|
||||||
|
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 = TestMetaClassModel
|
||||||
|
exclude = 'text'
|
||||||
|
|
||||||
|
with self.assertRaises(TypeError) as result:
|
||||||
|
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 = TestMetaClassModel
|
||||||
|
fields = ('text',)
|
||||||
|
exclude = ('text',)
|
||||||
|
|
||||||
|
with self.assertRaises(AssertionError) as result:
|
||||||
|
ExampleSerializer().fields
|
||||||
|
|
||||||
|
exception = result.exception
|
||||||
|
self.assertEqual(
|
||||||
|
str(exception),
|
||||||
|
"Cannot set both 'fields' and 'exclude'."
|
||||||
|
)
|
||||||
|
|
|
@ -1,62 +0,0 @@
|
||||||
from django.test import TestCase
|
|
||||||
from rest_framework import serializers
|
|
||||||
from .models import BasicModel
|
|
||||||
|
|
||||||
|
|
||||||
class TestSerializerMetaClass(TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
class FieldsSerializer(serializers.ModelSerializer):
|
|
||||||
text = serializers.CharField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = BasicModel
|
|
||||||
fields = ('text')
|
|
||||||
|
|
||||||
class ExcludeSerializer(serializers.ModelSerializer):
|
|
||||||
text = serializers.CharField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = BasicModel
|
|
||||||
exclude = ('text')
|
|
||||||
|
|
||||||
class FieldsAndExcludeSerializer(serializers.ModelSerializer):
|
|
||||||
text = serializers.CharField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = BasicModel
|
|
||||||
fields = ('text',)
|
|
||||||
exclude = ('text',)
|
|
||||||
|
|
||||||
self.fields_serializer = FieldsSerializer
|
|
||||||
self.exclude_serializer = ExcludeSerializer
|
|
||||||
self.faeSerializer = FieldsAndExcludeSerializer
|
|
||||||
|
|
||||||
def test_meta_class_fields(self):
|
|
||||||
object = BasicModel(text="Hello World.")
|
|
||||||
serializer = self.fields_serializer(instance=object)
|
|
||||||
|
|
||||||
with self.assertRaises(TypeError) as result:
|
|
||||||
serializer.data
|
|
||||||
|
|
||||||
exception = result.exception
|
|
||||||
self.assertEqual(str(exception), "`fields` must be a list or tuple")
|
|
||||||
|
|
||||||
def test_meta_class_exclude(self):
|
|
||||||
object = BasicModel(text="Hello World.")
|
|
||||||
serializer = self.exclude_serializer(instance=object)
|
|
||||||
|
|
||||||
with self.assertRaises(TypeError) as result:
|
|
||||||
serializer.data
|
|
||||||
|
|
||||||
exception = result.exception
|
|
||||||
self.assertEqual(str(exception), "`exclude` must be a list or tuple")
|
|
||||||
|
|
||||||
def test_meta_class_fields_and_exclude(self):
|
|
||||||
object = BasicModel(text="Hello World.")
|
|
||||||
serializer = self.faeSerializer(instance=object)
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError) as result:
|
|
||||||
serializer.data
|
|
||||||
|
|
||||||
exception = result.exception
|
|
||||||
self.assertEqual(str(exception), "Cannot set both 'fields' and 'exclude'.")
|
|
Loading…
Reference in New Issue
Block a user