diff options
| author | Tom Christie | 2013-03-22 12:33:09 +0000 | 
|---|---|---|
| committer | Tom Christie | 2013-03-22 12:33:09 +0000 | 
| commit | c32d9969acaebc83ad1e2dd888d4a2829c35571e (patch) | |
| tree | e42dbe5b129b6f855c1982f89713ce45c3cdea91 /rest_framework | |
| parent | b2dc66448503c2120d943a2f282eab235afc67ba (diff) | |
| download | django-rest-framework-c32d9969acaebc83ad1e2dd888d4a2829c35571e.tar.bz2 | |
Add extra tests for errors from incorrect data with multiple create/update
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/serializers.py | 36 | ||||
| -rw-r--r-- | rest_framework/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 34120dc6..3029cf1c 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: -                    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) +                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) -                    ret.append(self.from_native(item, None)) -                    errors.append(self._errors) +                    if update: +                        self._deleted = identity_to_objects.values() -                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 66fca883..2f416488 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 = [              {  | 
