mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
test(tests/test_fields): added test for nested SerializerMethodFields
This commit is contained in:
parent
564b1c8268
commit
a322be8574
|
@ -1638,3 +1638,28 @@ class TestSerializerMethodField:
|
||||||
"'ExampleSerializer', because it is the same as the default "
|
"'ExampleSerializer', because it is the same as the default "
|
||||||
"method name. Remove the `method_name` argument."
|
"method name. Remove the `method_name` argument."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_nested_serializer_method_field(self):
|
||||||
|
class NestedSerializer(serializers.Serializer):
|
||||||
|
example_field = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_example_field(self, obj):
|
||||||
|
return 'ran get_example_field(%d)' % obj['example_field']
|
||||||
|
|
||||||
|
class TestSerializer(serializers.Serializer):
|
||||||
|
nested = NestedSerializer()
|
||||||
|
|
||||||
|
input_data = {
|
||||||
|
'nested': {
|
||||||
|
'example_field': 123,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expected_data = {
|
||||||
|
'nested': {
|
||||||
|
'example_field': 'ran get_example_field(123)',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serializer = TestSerializer(data=input_data)
|
||||||
|
assert serializer.is_valid()
|
||||||
|
assert serializer.data == expected_data
|
||||||
|
|
Loading…
Reference in New Issue
Block a user