2015-07-08 15:19:42 +03:00
|
|
|
from django.http import QueryDict
|
|
|
|
|
2014-10-30 18:59:16 +03:00
|
|
|
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
|
2015-07-03 19:28:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2015-07-08 15:25:12 +03:00
|
|
|
input_data = QueryDict('')
|
2015-07-03 19:28:48 +03:00
|
|
|
serializer = self.Serializer(data=input_data)
|
|
|
|
assert serializer.is_valid()
|
|
|
|
|
2015-07-08 15:19:42 +03:00
|
|
|
input_data = QueryDict('nested[one]=1')
|
2015-07-03 19:28:48 +03:00
|
|
|
serializer = self.Serializer(data=input_data)
|
|
|
|
assert serializer.is_valid()
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestNestedSerializerWithMany:
|
|
|
|
def setup(self):
|
|
|
|
class NestedSerializer(serializers.Serializer):
|
2015-07-25 17:52:05 +03:00
|
|
|
example = serializers.IntegerField(max_value=10)
|
2015-03-26 02:51:40 +03:00
|
|
|
|
2015-07-25 17:52:05 +03:00
|
|
|
class TestSerializer(serializers.Serializer):
|
|
|
|
allow_null = NestedSerializer(many=True, allow_null=True)
|
|
|
|
not_allow_null = NestedSerializer(many=True)
|
2015-03-26 02:51:40 +03:00
|
|
|
|
2015-07-25 17:52:05 +03:00
|
|
|
self.Serializer = TestSerializer
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
def test_null_allowed_if_allow_null_is_set(self):
|
|
|
|
input_data = {
|
2015-07-25 17:52:05 +03:00
|
|
|
'allow_null': None,
|
|
|
|
'not_allow_null': [{'example': '2'}, {'example': '3'}]
|
2015-03-26 02:51:40 +03:00
|
|
|
}
|
|
|
|
expected_data = {
|
2015-07-25 17:52:05 +03:00
|
|
|
'allow_null': None,
|
|
|
|
'not_allow_null': [{'example': 2}, {'example': 3}]
|
2015-03-26 02:51:40 +03:00
|
|
|
}
|
2015-07-25 17:52:05 +03:00
|
|
|
serializer = self.Serializer(data=input_data)
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
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 = {
|
2015-07-25 17:52:05 +03:00
|
|
|
'allow_null': None,
|
|
|
|
'not_allow_null': None
|
2015-03-26 02:51:40 +03:00
|
|
|
}
|
2015-07-25 17:52:05 +03:00
|
|
|
serializer = self.Serializer(data=input_data)
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
assert not serializer.is_valid()
|
2015-07-25 17:52:05 +03:00
|
|
|
|
|
|
|
expected_errors = {'not_allow_null': [serializer.error_messages['null']]}
|
|
|
|
assert serializer.errors == expected_errors
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
def test_run_the_field_validation_even_if_the_field_is_null(self):
|
2015-07-25 17:52:05 +03:00
|
|
|
class TestSerializer(self.Serializer):
|
|
|
|
validation_was_run = False
|
2015-03-26 02:51:40 +03:00
|
|
|
|
2015-07-25 17:52:05 +03:00
|
|
|
def validate_allow_null(self, value):
|
|
|
|
TestSerializer.validation_was_run = True
|
|
|
|
return value
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
input_data = {
|
2015-07-25 17:52:05 +03:00
|
|
|
'allow_null': None,
|
|
|
|
'not_allow_null': [{'example': 2}]
|
2015-03-26 02:51:40 +03:00
|
|
|
}
|
2015-07-25 17:52:05 +03:00
|
|
|
serializer = TestSerializer(data=input_data)
|
2015-03-26 02:51:40 +03:00
|
|
|
|
|
|
|
assert serializer.is_valid()
|
2015-07-25 17:52:05 +03:00
|
|
|
assert serializer.validated_data == input_data
|
|
|
|
assert TestSerializer.validation_was_run
|