mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
Add nested serializer with list tests
TestNestedSerializerWithList checks if a list (a MulitpleChoiceField in this case) in a nested serializer is correctly parsed from json or multipart formatted data.
This commit is contained in:
parent
057cf13578
commit
877ed6edc2
|
@ -167,3 +167,32 @@ class TestNestedSerializerWithMany:
|
||||||
|
|
||||||
expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
|
expected_errors = {'not_allow_empty': {'non_field_errors': [serializers.ListSerializer.default_error_messages['empty']]}}
|
||||||
assert serializer.errors == expected_errors
|
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])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user