@@ -79,17 +79,23 @@ class NestedSerializer(serializers.Serializer):
79
79
class TestSerializer (serializers .Serializer ):
80
80
allow_null = NestedSerializer (many = True , allow_null = True )
81
81
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 )
82
84
83
85
self .Serializer = TestSerializer
84
86
85
87
def test_null_allowed_if_allow_null_is_set (self ):
86
88
input_data = {
87
89
'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' }],
89
93
}
90
94
expected_data = {
91
95
'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 }],
93
99
}
94
100
serializer = self .Serializer (data = input_data )
95
101
@@ -99,7 +105,9 @@ def test_null_allowed_if_allow_null_is_set(self):
99
105
def test_null_is_not_allowed_if_allow_null_is_not_set (self ):
100
106
input_data = {
101
107
'allow_null' : None ,
102
- 'not_allow_null' : None
108
+ 'not_allow_null' : None ,
109
+ 'allow_empty' : [{'example' : '2' }],
110
+ 'not_allow_empty' : [{'example' : '2' }],
103
111
}
104
112
serializer = self .Serializer (data = input_data )
105
113
@@ -118,10 +126,44 @@ def validate_allow_null(self, value):
118
126
119
127
input_data = {
120
128
'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 }],
122
132
}
123
133
serializer = TestSerializer (data = input_data )
124
134
125
135
assert serializer .is_valid ()
126
136
assert serializer .validated_data == input_data
127
137
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