mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-05-26 01:03:12 +03:00
Add tests for callable attributes raising exceptions
This commit is contained in:
parent
4248a8d3fc
commit
16ffe5e31f
|
@ -93,6 +93,31 @@ class TestSource:
|
||||||
"same as the field name. Remove the `source` keyword argument."
|
"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:
|
class TestReadOnly:
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user