From 16ffe5e31f80058389139fe5dae5184cc22319a6 Mon Sep 17 00:00:00 2001 From: Evan Heidtmann Date: Thu, 26 Feb 2015 08:34:14 -0800 Subject: [PATCH] Add tests for callable attributes raising exceptions --- tests/test_fields.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index ab3418bd6..7f5f81029 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -93,6 +93,31 @@ class TestSource: "same as the field name. Remove the `source` keyword argument." ) + def test_callable_source(self): + class ExampleSerializer(serializers.Serializer): + example_field = serializers.CharField(source='example_callable') + + class ExampleInstance(object): + def example_callable(self): + return 'example callable value' + + serializer = ExampleSerializer(ExampleInstance()) + assert serializer.data['example_field'] == 'example callable value' + + def test_callable_source_raises(self): + class ExampleSerializer(serializers.Serializer): + example_field = serializers.CharField(source='example_callable', read_only=True) + + class ExampleInstance(object): + def example_callable(self): + raise AttributeError('method call failed') + + with pytest.raises(ValueError) as exc_info: + serializer = ExampleSerializer(ExampleInstance()) + serializer.data.items() + + assert 'method call failed' in str(exc_info.value) + class TestReadOnly: def setup(self):