Skip to content

Commit aa48182

Browse files
committed
Add test coverage on allow_empty for nested serializers.
1 parent 4404d40 commit aa48182

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

tests/test_serializer_nested.py

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ class NestedSerializer(serializers.Serializer):
7979
class TestSerializer(serializers.Serializer):
8080
allow_null = NestedSerializer(many=True, allow_null=True)
8181
not_allow_null = NestedSerializer(many=True)
82+
allow_empty = NestedSerializer(many=True, allow_empty=True)
83+
not_allow_empty = NestedSerializer(many=True, allow_empty=False)
8284

8385
self.Serializer = TestSerializer
8486

8587
def test_null_allowed_if_allow_null_is_set(self):
8688
input_data = {
8789
'allow_null': None,
88-
'not_allow_null': [{'example': '2'}, {'example': '3'}]
90+
'not_allow_null': [{'example': '2'}, {'example': '3'}],
91+
'allow_empty': [{'example': '2'}],
92+
'not_allow_empty': [{'example': '2'}],
8993
}
9094
expected_data = {
9195
'allow_null': None,
92-
'not_allow_null': [{'example': 2}, {'example': 3}]
96+
'not_allow_null': [{'example': 2}, {'example': 3}],
97+
'allow_empty': [{'example': 2}],
98+
'not_allow_empty': [{'example': 2}],
9399
}
94100
serializer = self.Serializer(data=input_data)
95101

@@ -99,7 +105,9 @@ def test_null_allowed_if_allow_null_is_set(self):
99105
def test_null_is_not_allowed_if_allow_null_is_not_set(self):
100106
input_data = {
101107
'allow_null': None,
102-
'not_allow_null': None
108+
'not_allow_null': None,
109+
'allow_empty': [{'example': '2'}],
110+
'not_allow_empty': [{'example': '2'}],
103111
}
104112
serializer = self.Serializer(data=input_data)
105113

@@ -118,10 +126,44 @@ def validate_allow_null(self, value):
118126

119127
input_data = {
120128
'allow_null': None,
121-
'not_allow_null': [{'example': 2}]
129+
'not_allow_null': [{'example': 2}],
130+
'allow_empty': [{'example': 2}],
131+
'not_allow_empty': [{'example': 2}],
122132
}
123133
serializer = TestSerializer(data=input_data)
124134

125135
assert serializer.is_valid()
126136
assert serializer.validated_data == input_data
127137
assert TestSerializer.validation_was_run
138+
139+
def test_empty_allowed_if_allow_empty_is_set(self):
140+
input_data = {
141+
'allow_null': [{'example': '2'}],
142+
'not_allow_null': [{'example': '2'}],
143+
'allow_empty': [],
144+
'not_allow_empty': [{'example': '2'}],
145+
}
146+
expected_data = {
147+
'allow_null': [{'example': 2}],
148+
'not_allow_null': [{'example': 2}],
149+
'allow_empty': [],
150+
'not_allow_empty': [{'example': 2}],
151+
}
152+
serializer = self.Serializer(data=input_data)
153+
154+
assert serializer.is_valid(), serializer.errors
155+
assert serializer.validated_data == expected_data
156+
157+
def test_empty_not_allowed_if_allow_empty_is_set_to_false(self):
158+
input_data = {
159+
'allow_null': [{'example': '2'}],
160+
'not_allow_null': [{'example': '2'}],
161+
'allow_empty': [],
162+
'not_allow_empty': [],
163+
}
164+
serializer = self.Serializer(data=input_data)
165+
166+
assert not serializer.is_valid()
167+
168+
expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
169+
assert serializer.errors == expected_errors

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy