| 1 |
""" |
| 2 |
Tests to cover bulk create and update using serializers. |
| 3 |
""" |
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
from rest_framework import serializers |
| 7 |
|
| 8 |
|
| 9 |
class BulkCreateSerializerTests(TestCase): |
| 10 |
""" |
| 11 |
Creating multiple instances using serializers. |
| 12 |
""" |
| 13 |
|
| 14 |
def setUp(self): |
| 15 |
class BookSerializer(serializers.Serializer): |
| 16 |
id = serializers.IntegerField() |
| 17 |
title = serializers.CharField(max_length=100) |
| 18 |
author = serializers.CharField(max_length=100) |
| 19 |
|
| 20 |
self.BookSerializer = BookSerializer |
| 21 |
|
| 22 |
def test_bulk_create_success(self): |
| 23 |
""" |
| 24 |
Correct bulk update serialization should return the input data. |
| 25 |
""" |
| 26 |
|
| 27 |
data = [ |
| 28 |
{ |
| 29 |
'id': 0, |
| 30 |
'title': 'The electric kool-aid acid test', |
| 31 |
'author': 'Tom Wolfe' |
| 32 |
}, { |
| 33 |
'id': 1, |
| 34 |
'title': 'If this is a man', |
| 35 |
'author': 'Primo Levi' |
| 36 |
}, { |
| 37 |
'id': 2, |
| 38 |
'title': 'The wind-up bird chronicle', |
| 39 |
'author': 'Haruki Murakami' |
| 40 |
} |
| 41 |
] |
| 42 |
|
| 43 |
serializer = self.BookSerializer(data=data, many=True) |
| 44 |
assert serializer.is_valid() is True |
| 45 |
assert serializer.validated_data == data |
| 46 |
assert serializer.errors == [] |
| 47 |
|
| 48 |
def test_bulk_create_errors(self): |
| 49 |
""" |
| 50 |
Incorrect bulk create serialization should return errors. |
| 51 |
""" |
| 52 |
|
| 53 |
data = [ |
| 54 |
{ |
| 55 |
'id': 0, |
| 56 |
'title': 'The electric kool-aid acid test', |
| 57 |
'author': 'Tom Wolfe' |
| 58 |
}, { |
| 59 |
'id': 1, |
| 60 |
'title': 'If this is a man', |
| 61 |
'author': 'Primo Levi' |
| 62 |
}, { |
| 63 |
'id': 'foo', |
| 64 |
'title': 'The wind-up bird chronicle', |
| 65 |
'author': 'Haruki Murakami' |
| 66 |
} |
| 67 |
] |
| 68 |
expected_errors = { |
| 69 |
2: {'id': ['A valid integer is required.']} |
| 70 |
} |
| 71 |
|
| 72 |
serializer = self.BookSerializer(data=data, many=True) |
| 73 |
assert serializer.is_valid() is False |
| 74 |
assert serializer.errors == expected_errors |
| 75 |
assert serializer.validated_data == [] |
| 76 |
|
| 77 |
def test_invalid_list_datatype(self): |
| 78 |
""" |
| 79 |
Data containing list of incorrect data type should return errors. |
| 80 |
""" |
| 81 |
data = ['foo', 'bar', 'baz'] |
| 82 |
serializer = self.BookSerializer(data=data, many=True) |
| 83 |
assert serializer.is_valid() is False |
| 84 |
|
| 85 |
message = 'Invalid data. Expected a dictionary, but got str.' |
| 86 |
expected_errors = {idx: {'non_field_errors': [message]} for idx in range(len(data))} |
| 87 |
|
| 88 |
assert serializer.errors == expected_errors |
| 89 |
|
| 90 |
def test_invalid_single_datatype(self): |
| 91 |
""" |
| 92 |
Data containing a single incorrect data type should return errors. |
| 93 |
""" |
| 94 |
data = 123 |
| 95 |
serializer = self.BookSerializer(data=data, many=True) |
| 96 |
assert serializer.is_valid() is False |
| 97 |
|
| 98 |
expected_errors = {'non_field_errors': ['Expected a list of items but got type "int".']} |
| 99 |
|
| 100 |
assert serializer.errors == expected_errors |
| 101 |
|
| 102 |
def test_invalid_single_object(self): |
| 103 |
""" |
| 104 |
Data containing only a single object, instead of a list of objects |
| 105 |
should return errors. |
| 106 |
""" |
| 107 |
data = { |
| 108 |
'id': 0, |
| 109 |
'title': 'The electric kool-aid acid test', |
| 110 |
'author': 'Tom Wolfe' |
| 111 |
} |
| 112 |
serializer = self.BookSerializer(data=data, many=True) |
| 113 |
assert serializer.is_valid() is False |
| 114 |
|
| 115 |
expected_errors = {'non_field_errors': ['Expected a list of items but got type "dict".']} |
| 116 |
|
| 117 |
assert serializer.errors == expected_errors |