diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_fields.py | 2 | ||||
| -rw-r--r-- | tests/test_serializer_bulk_update.py | 240 | 
2 files changed, 121 insertions, 121 deletions
| diff --git a/tests/test_fields.py b/tests/test_fields.py index 96d09900..5db381ac 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -859,7 +859,7 @@ class TestMultipleChoiceField(FieldValues):          ('aircon', 'manual'): set(['aircon', 'manual']),      }      invalid_inputs = { -        'abc': ['Expected a list of items but got type `str`'], +        'abc': ['Expected a list of items but got type `str`.'],          ('aircon', 'incorrect'): ['`incorrect` is not a valid choice.']      }      outputs = [ diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 3341ce59..85b6b2fa 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -1,123 +1,123 @@ -# """ -# Tests to cover bulk create and update using serializers. -# """ -# from __future__ import unicode_literals -# from django.test import TestCase -# from rest_framework import serializers - - -# class BulkCreateSerializerTests(TestCase): -#     """ -#     Creating multiple instances using serializers. -#     """ - -#     def setUp(self): -#         class BookSerializer(serializers.Serializer): -#             id = serializers.IntegerField() -#             title = serializers.CharField(max_length=100) -#             author = serializers.CharField(max_length=100) - -#         self.BookSerializer = BookSerializer - -#     def test_bulk_create_success(self): -#         """ -#         Correct bulk update serialization should return the input data. -#         """ - -#         data = [ -#             { -#                 'id': 0, -#                 'title': 'The electric kool-aid acid test', -#                 'author': 'Tom Wolfe' -#             }, { -#                 'id': 1, -#                 'title': 'If this is a man', -#                 'author': 'Primo Levi' -#             }, { -#                 'id': 2, -#                 'title': 'The wind-up bird chronicle', -#                 'author': 'Haruki Murakami' -#             } -#         ] - -#         serializer = self.BookSerializer(data=data, many=True) -#         self.assertEqual(serializer.is_valid(), True) -#         self.assertEqual(serializer.object, data) - -#     def test_bulk_create_errors(self): -#         """ -#         Correct bulk update serialization should return the input data. -#         """ - -#         data = [ -#             { -#                 'id': 0, -#                 'title': 'The electric kool-aid acid test', -#                 'author': 'Tom Wolfe' -#             }, { -#                 'id': 1, -#                 'title': 'If this is a man', -#                 'author': 'Primo Levi' -#             }, { -#                 'id': 'foo', -#                 'title': 'The wind-up bird chronicle', -#                 'author': 'Haruki Murakami' -#             } -#         ] -#         expected_errors = [ -#             {}, -#             {}, -#             {'id': ['Enter a whole number.']} -#         ] - -#         serializer = self.BookSerializer(data=data, many=True) -#         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) +""" +Tests to cover bulk create and update using serializers. +""" +from __future__ import unicode_literals +from django.test import TestCase +from rest_framework import serializers + + +class BulkCreateSerializerTests(TestCase): +    """ +    Creating multiple instances using serializers. +    """ + +    def setUp(self): +        class BookSerializer(serializers.Serializer): +            id = serializers.IntegerField() +            title = serializers.CharField(max_length=100) +            author = serializers.CharField(max_length=100) + +        self.BookSerializer = BookSerializer + +    def test_bulk_create_success(self): +        """ +        Correct bulk update serialization should return the input data. +        """ + +        data = [ +            { +                'id': 0, +                'title': 'The electric kool-aid acid test', +                'author': 'Tom Wolfe' +            }, { +                'id': 1, +                'title': 'If this is a man', +                'author': 'Primo Levi' +            }, { +                'id': 2, +                'title': 'The wind-up bird chronicle', +                'author': 'Haruki Murakami' +            } +        ] + +        serializer = self.BookSerializer(data=data, many=True) +        self.assertEqual(serializer.is_valid(), True) +        self.assertEqual(serializer.validated_data, data) + +    def test_bulk_create_errors(self): +        """ +        Incorrect bulk create serialization should return errors. +        """ + +        data = [ +            { +                'id': 0, +                'title': 'The electric kool-aid acid test', +                'author': 'Tom Wolfe' +            }, { +                'id': 1, +                'title': 'If this is a man', +                'author': 'Primo Levi' +            }, { +                'id': 'foo', +                'title': 'The wind-up bird chronicle', +                'author': 'Haruki Murakami' +            } +        ] +        expected_errors = [ +            {}, +            {}, +            {'id': ['A valid integer is required.']} +        ] + +        serializer = self.BookSerializer(data=data, many=True) +        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. Expected a dictionary, but got unicode.']}, +            {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']}, +            {'non_field_errors': ['Invalid data. Expected a dictionary, but got unicode.']} +        ] + +        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 but got type `int`.']} + +        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 but got type `dict`.']} + +        self.assertEqual(serializer.errors, expected_errors)  # class BulkUpdateSerializerTests(TestCase): | 
