From c32d9969acaebc83ad1e2dd888d4a2829c35571e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 22 Mar 2013 12:33:09 +0000 Subject: [PATCH] Add extra tests for errors from incorrect data with multiple create/update --- rest_framework/serializers.py | 36 ++++++----- .../tests/serializer_bulk_update.py | 60 ++++++++++++++++++- 2 files changed, 81 insertions(+), 15 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 34120dc68..3029cf1c1 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -371,22 +371,30 @@ class BaseSerializer(Field): identities = [self.get_identity(self.to_native(obj)) for obj in 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: - # 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) + self._deleted = identity_to_objects.values() - ret.append(self.from_native(item, None)) - errors.append(self._errors) - - if update: - self._deleted = identity_to_objects.values() - self._errors = any(errors) and errors or [] + self._errors = any(errors) and errors or [] else: ret = self.from_native(data, files) diff --git a/rest_framework/tests/serializer_bulk_update.py b/rest_framework/tests/serializer_bulk_update.py index 66fca8835..2f4164880 100644 --- a/rest_framework/tests/serializer_bulk_update.py +++ b/rest_framework/tests/serializer_bulk_update.py @@ -7,6 +7,9 @@ from rest_framework import serializers class BulkCreateSerializerTests(TestCase): + """ + Creating multiple instances using serializers. + """ def setUp(self): class BookSerializer(serializers.Serializer): @@ -71,11 +74,63 @@ class BulkCreateSerializerTests(TestCase): self.assertEqual(serializer.is_valid(), False) 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): + """ + Updating multiple instances using serializers. + """ def setUp(self): class Book(object): + """ + A data type that can be persisted to a mock storage backend + with `.save()` and `.delete()`. + """ object_map = {} def __init__(self, id, title, author): @@ -126,6 +181,9 @@ class BulkUpdateSerializerTests(TestCase): book.save() def books(self): + """ + Return all the objects in the mock storage backend. + """ return self.Book.object_map.values() def test_bulk_update_success(self): @@ -152,7 +210,7 @@ class BulkUpdateSerializerTests(TestCase): def test_bulk_update_error(self): """ - Correct bulk update serialization should return the input data. + Incorrect bulk update serialization should return error data. """ data = [ {