forked from encode/django-rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_serializer_nested.py
198 lines (157 loc) · 6.46 KB
/
test_serializer_nested.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from django.http import QueryDict
from rest_framework import serializers
class TestNestedSerializer:
def setup(self):
class NestedSerializer(serializers.Serializer):
one = serializers.IntegerField(max_value=10)
two = serializers.IntegerField(max_value=10)
class TestSerializer(serializers.Serializer):
nested = NestedSerializer()
self.Serializer = TestSerializer
def test_nested_validate(self):
input_data = {
'nested': {
'one': '1',
'two': '2',
}
}
expected_data = {
'nested': {
'one': 1,
'two': 2,
}
}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
assert serializer.validated_data == expected_data
def test_nested_serialize_empty(self):
expected_data = {
'nested': {
'one': None,
'two': None
}
}
serializer = self.Serializer()
assert serializer.data == expected_data
class TestNotRequiredNestedSerializer:
def setup(self):
class NestedSerializer(serializers.Serializer):
one = serializers.IntegerField(max_value=10)
class TestSerializer(serializers.Serializer):
nested = NestedSerializer(required=False)
self.Serializer = TestSerializer
def test_json_validate(self):
input_data = {}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
input_data = {'nested': {'one': '1'}}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
def test_multipart_validate(self):
input_data = QueryDict('')
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
input_data = QueryDict('nested[one]=1')
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
class TestNestedSerializerWithMany:
def setup(self):
class NestedSerializer(serializers.Serializer):
example = serializers.IntegerField(max_value=10)
class TestSerializer(serializers.Serializer):
allow_null = NestedSerializer(many=True, allow_null=True)
not_allow_null = NestedSerializer(many=True)
allow_empty = NestedSerializer(many=True, allow_empty=True)
not_allow_empty = NestedSerializer(many=True, allow_empty=False)
self.Serializer = TestSerializer
def test_null_allowed_if_allow_null_is_set(self):
input_data = {
'allow_null': None,
'not_allow_null': [{'example': '2'}, {'example': '3'}],
'allow_empty': [{'example': '2'}],
'not_allow_empty': [{'example': '2'}],
}
expected_data = {
'allow_null': None,
'not_allow_null': [{'example': 2}, {'example': 3}],
'allow_empty': [{'example': 2}],
'not_allow_empty': [{'example': 2}],
}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid(), serializer.errors
assert serializer.validated_data == expected_data
def test_null_is_not_allowed_if_allow_null_is_not_set(self):
input_data = {
'allow_null': None,
'not_allow_null': None,
'allow_empty': [{'example': '2'}],
'not_allow_empty': [{'example': '2'}],
}
serializer = self.Serializer(data=input_data)
assert not serializer.is_valid()
expected_errors = {'not_allow_null': [serializer.error_messages['null']]}
assert serializer.errors == expected_errors
def test_run_the_field_validation_even_if_the_field_is_null(self):
class TestSerializer(self.Serializer):
validation_was_run = False
def validate_allow_null(self, value):
TestSerializer.validation_was_run = True
return value
input_data = {
'allow_null': None,
'not_allow_null': [{'example': 2}],
'allow_empty': [{'example': 2}],
'not_allow_empty': [{'example': 2}],
}
serializer = TestSerializer(data=input_data)
assert serializer.is_valid()
assert serializer.validated_data == input_data
assert TestSerializer.validation_was_run
def test_empty_allowed_if_allow_empty_is_set(self):
input_data = {
'allow_null': [{'example': '2'}],
'not_allow_null': [{'example': '2'}],
'allow_empty': [],
'not_allow_empty': [{'example': '2'}],
}
expected_data = {
'allow_null': [{'example': 2}],
'not_allow_null': [{'example': 2}],
'allow_empty': [],
'not_allow_empty': [{'example': 2}],
}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid(), serializer.errors
assert serializer.validated_data == expected_data
def test_empty_not_allowed_if_allow_empty_is_set_to_false(self):
input_data = {
'allow_null': [{'example': '2'}],
'not_allow_null': [{'example': '2'}],
'allow_empty': [],
'not_allow_empty': [],
}
serializer = self.Serializer(data=input_data)
assert not serializer.is_valid()
expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
assert serializer.errors == expected_errors
class TestNestedSerializerWithList:
def setup(self):
class NestedSerializer(serializers.Serializer):
example = serializers.MultipleChoiceField(choices=[1, 2, 3])
class TestSerializer(serializers.Serializer):
nested = NestedSerializer()
self.Serializer = TestSerializer
def test_nested_serializer_with_list_json(self):
input_data = {
'nested': {
'example': [1, 2],
}
}
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
assert serializer.validated_data['nested']['example'] == set([1, 2])
def test_nested_serializer_with_list_multipart(self):
input_data = QueryDict('nested.example=1&nested.example=2')
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
assert serializer.validated_data['nested']['example'] == set([1, 2])