mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Add extra tests for errors from incorrect data with multiple create/update
This commit is contained in:
parent
b2dc664485
commit
c32d9969ac
|
@ -371,22 +371,30 @@ class BaseSerializer(Field):
|
||||||
identities = [self.get_identity(self.to_native(obj)) for obj in objects]
|
identities = [self.get_identity(self.to_native(obj)) for obj in objects]
|
||||||
identity_to_objects = dict(zip(identities, objects))
|
identity_to_objects = dict(zip(identities, objects))
|
||||||
|
|
||||||
for item in data:
|
try:
|
||||||
|
iter(data)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
raise TypeError
|
||||||
|
except TypeError:
|
||||||
|
self._errors = {'non_field_errors': ['Expected a list of items']}
|
||||||
|
else:
|
||||||
|
for item in data:
|
||||||
|
if update:
|
||||||
|
# Determine which object we're updating
|
||||||
|
try:
|
||||||
|
identity = self.get_identity(item)
|
||||||
|
except:
|
||||||
|
self.object = None
|
||||||
|
else:
|
||||||
|
self.object = identity_to_objects.pop(identity, None)
|
||||||
|
|
||||||
|
ret.append(self.from_native(item, None))
|
||||||
|
errors.append(self._errors)
|
||||||
|
|
||||||
if update:
|
if update:
|
||||||
# Determine which object we're updating
|
self._deleted = identity_to_objects.values()
|
||||||
try:
|
|
||||||
identity = self.get_identity(item)
|
|
||||||
except:
|
|
||||||
self.object = None
|
|
||||||
else:
|
|
||||||
self.object = identity_to_objects.pop(identity, None)
|
|
||||||
|
|
||||||
ret.append(self.from_native(item, None))
|
self._errors = any(errors) and errors or []
|
||||||
errors.append(self._errors)
|
|
||||||
|
|
||||||
if update:
|
|
||||||
self._deleted = identity_to_objects.values()
|
|
||||||
self._errors = any(errors) and errors or []
|
|
||||||
else:
|
else:
|
||||||
ret = self.from_native(data, files)
|
ret = self.from_native(data, files)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,9 @@ from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
class BulkCreateSerializerTests(TestCase):
|
class BulkCreateSerializerTests(TestCase):
|
||||||
|
"""
|
||||||
|
Creating multiple instances using serializers.
|
||||||
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
class BookSerializer(serializers.Serializer):
|
class BookSerializer(serializers.Serializer):
|
||||||
|
@ -71,11 +74,63 @@ class BulkCreateSerializerTests(TestCase):
|
||||||
self.assertEqual(serializer.is_valid(), False)
|
self.assertEqual(serializer.is_valid(), False)
|
||||||
self.assertEqual(serializer.errors, expected_errors)
|
self.assertEqual(serializer.errors, expected_errors)
|
||||||
|
|
||||||
|
def test_invalid_list_datatype(self):
|
||||||
|
"""
|
||||||
|
Data containing list of incorrect data type should return errors.
|
||||||
|
"""
|
||||||
|
data = ['foo', 'bar', 'baz']
|
||||||
|
serializer = self.BookSerializer(data=data, many=True)
|
||||||
|
self.assertEqual(serializer.is_valid(), False)
|
||||||
|
|
||||||
|
expected_errors = [
|
||||||
|
{'non_field_errors': ['Invalid data']},
|
||||||
|
{'non_field_errors': ['Invalid data']},
|
||||||
|
{'non_field_errors': ['Invalid data']}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(serializer.errors, expected_errors)
|
||||||
|
|
||||||
|
def test_invalid_single_datatype(self):
|
||||||
|
"""
|
||||||
|
Data containing a single incorrect data type should return errors.
|
||||||
|
"""
|
||||||
|
data = 123
|
||||||
|
serializer = self.BookSerializer(data=data, many=True)
|
||||||
|
self.assertEqual(serializer.is_valid(), False)
|
||||||
|
|
||||||
|
expected_errors = {'non_field_errors': ['Expected a list of items']}
|
||||||
|
|
||||||
|
self.assertEqual(serializer.errors, expected_errors)
|
||||||
|
|
||||||
|
def test_invalid_single_object(self):
|
||||||
|
"""
|
||||||
|
Data containing only a single object, instead of a list of objects
|
||||||
|
should return errors.
|
||||||
|
"""
|
||||||
|
data = {
|
||||||
|
'id': 0,
|
||||||
|
'title': 'The electric kool-aid acid test',
|
||||||
|
'author': 'Tom Wolfe'
|
||||||
|
}
|
||||||
|
serializer = self.BookSerializer(data=data, many=True)
|
||||||
|
self.assertEqual(serializer.is_valid(), False)
|
||||||
|
|
||||||
|
expected_errors = {'non_field_errors': ['Expected a list of items']}
|
||||||
|
|
||||||
|
self.assertEqual(serializer.errors, expected_errors)
|
||||||
|
|
||||||
|
|
||||||
class BulkUpdateSerializerTests(TestCase):
|
class BulkUpdateSerializerTests(TestCase):
|
||||||
|
"""
|
||||||
|
Updating multiple instances using serializers.
|
||||||
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
class Book(object):
|
class Book(object):
|
||||||
|
"""
|
||||||
|
A data type that can be persisted to a mock storage backend
|
||||||
|
with `.save()` and `.delete()`.
|
||||||
|
"""
|
||||||
object_map = {}
|
object_map = {}
|
||||||
|
|
||||||
def __init__(self, id, title, author):
|
def __init__(self, id, title, author):
|
||||||
|
@ -126,6 +181,9 @@ class BulkUpdateSerializerTests(TestCase):
|
||||||
book.save()
|
book.save()
|
||||||
|
|
||||||
def books(self):
|
def books(self):
|
||||||
|
"""
|
||||||
|
Return all the objects in the mock storage backend.
|
||||||
|
"""
|
||||||
return self.Book.object_map.values()
|
return self.Book.object_map.values()
|
||||||
|
|
||||||
def test_bulk_update_success(self):
|
def test_bulk_update_success(self):
|
||||||
|
@ -152,7 +210,7 @@ class BulkUpdateSerializerTests(TestCase):
|
||||||
|
|
||||||
def test_bulk_update_error(self):
|
def test_bulk_update_error(self):
|
||||||
"""
|
"""
|
||||||
Correct bulk update serialization should return the input data.
|
Incorrect bulk update serialization should return error data.
|
||||||
"""
|
"""
|
||||||
data = [
|
data = [
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user