mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-02 20:54:42 +03:00
Moar tests
This commit is contained in:
parent
b361c54c5c
commit
cf72b9a8b7
|
@ -24,6 +24,7 @@ from rest_framework.utils.field_mapping import (
|
||||||
lookup_class
|
lookup_class
|
||||||
)
|
)
|
||||||
import copy
|
import copy
|
||||||
|
import inspect
|
||||||
|
|
||||||
# Note: We do the following so that users of the framework can use this style:
|
# Note: We do the following so that users of the framework can use this style:
|
||||||
#
|
#
|
||||||
|
@ -268,6 +269,7 @@ class ListSerializer(BaseSerializer):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
self.child = kwargs.pop('child', copy.deepcopy(self.child))
|
self.child = kwargs.pop('child', copy.deepcopy(self.child))
|
||||||
assert self.child is not None, '`child` is a required argument.'
|
assert self.child is not None, '`child` is a required argument.'
|
||||||
|
assert not inspect.isclass(self.child), '`child` has not been instantiated.'
|
||||||
self.context = kwargs.pop('context', {})
|
self.context = kwargs.pop('context', {})
|
||||||
kwargs.pop('partial', None)
|
kwargs.pop('partial', None)
|
||||||
|
|
||||||
|
|
|
@ -473,3 +473,36 @@ class TestIntegration(TestCase):
|
||||||
'through': []
|
'through': []
|
||||||
}
|
}
|
||||||
self.assertEqual(serializer.data, expected)
|
self.assertEqual(serializer.data, expected)
|
||||||
|
|
||||||
|
|
||||||
|
# Tests for bulk create using `ListSerializer`.
|
||||||
|
|
||||||
|
class BulkCreateModel(models.Model):
|
||||||
|
name = models.CharField(max_length=10)
|
||||||
|
|
||||||
|
|
||||||
|
class TestBulkCreate(TestCase):
|
||||||
|
def test_bulk_create(self):
|
||||||
|
class BasicModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = BulkCreateModel
|
||||||
|
fields = ('name',)
|
||||||
|
|
||||||
|
class BulkCreateSerializer(serializers.ListSerializer):
|
||||||
|
child = BasicModelSerializer()
|
||||||
|
|
||||||
|
data = [{'name': 'a'}, {'name': 'b'}, {'name': 'c'}]
|
||||||
|
serializer = BulkCreateSerializer(data=data)
|
||||||
|
assert serializer.is_valid()
|
||||||
|
|
||||||
|
# Objects are returned by save().
|
||||||
|
instances = serializer.save()
|
||||||
|
assert len(instances) == 3
|
||||||
|
assert [item.name for item in instances] == ['a', 'b', 'c']
|
||||||
|
|
||||||
|
# Objects have been created in the database.
|
||||||
|
assert BulkCreateModel.objects.count() == 3
|
||||||
|
assert list(BulkCreateModel.objects.values_list('name', flat=True)) == ['a', 'b', 'c']
|
||||||
|
|
||||||
|
# Serializer returns correct data.
|
||||||
|
assert serializer.data == data
|
||||||
|
|
Loading…
Reference in New Issue
Block a user