Commit cf72b9a8 by Tom Christie

Moar tests

parent b361c54c
...@@ -24,6 +24,7 @@ from rest_framework.utils.field_mapping import ( ...@@ -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): ...@@ -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): ...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment