django-rest-framework/tests/test_serializer_bulk_update.py

124 lines
3.7 KiB
Python
Raw Normal View History

2014-11-06 13:34:59 +03:00
"""
Tests to cover bulk create and update using serializers.
"""
from django.test import TestCase
2015-06-25 23:55:51 +03:00
2014-11-06 13:34:59 +03:00
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)
assert serializer.is_valid() is True
assert serializer.validated_data == data
assert serializer.errors == []
2014-11-06 13:34:59 +03:00
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)
assert serializer.is_valid() is False
assert serializer.errors == expected_errors
assert serializer.validated_data == []
2014-11-06 13:34:59 +03:00
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)
assert serializer.is_valid() is False
2014-11-06 13:34:59 +03:00
message = 'Invalid data. Expected a dictionary, but got str.'
2014-11-06 13:34:59 +03:00
expected_errors = [
2014-11-06 14:51:10 +03:00
{'non_field_errors': [message]},
{'non_field_errors': [message]},
{'non_field_errors': [message]}
2014-11-06 13:34:59 +03:00
]
assert serializer.errors == expected_errors
2014-11-06 13:34:59 +03:00
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)
assert serializer.is_valid() is False
2014-11-06 13:34:59 +03:00
expected_errors = {'non_field_errors': ['Expected a list of items but got type "int".']}
2014-11-06 13:34:59 +03:00
assert serializer.errors == expected_errors
2014-11-06 13:34:59 +03:00
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)
assert serializer.is_valid() is False
2014-11-06 13:34:59 +03:00
expected_errors = {'non_field_errors': ['Expected a list of items but got type "dict".']}
2014-11-06 13:34:59 +03:00
assert serializer.errors == expected_errors