mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 21:10:13 +03:00
Removed no longer needed tests.
Added tests for using default when default is callable. Fixed field get_attribute(), now uses get_default() instead of default.
This commit is contained in:
parent
27e8f84855
commit
16375f5453
|
@ -443,7 +443,7 @@ class Field(object):
|
|||
return get_attribute(instance, self.source_attrs)
|
||||
except (KeyError, AttributeError) as exc:
|
||||
if self.default is not empty:
|
||||
return self.default
|
||||
return self.get_default()
|
||||
if not self.required:
|
||||
raise SkipField()
|
||||
msg = (
|
||||
|
|
|
@ -313,44 +313,12 @@ class TestNotRequiredOutput:
|
|||
serializer.save()
|
||||
assert serializer.data == {'included': 'abc'}
|
||||
|
||||
@pytest.mark.skipif(True, reason='Disabling pending removal')
|
||||
def test_default_required_output_for_dict(self):
|
||||
"""
|
||||
'default="something"' should require dictionary key.
|
||||
|
||||
We need to handle this as the field will have an implicit
|
||||
'required=False', but it should still have a value.
|
||||
"""
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
omitted = serializers.CharField(default='abc')
|
||||
included = serializers.CharField()
|
||||
|
||||
serializer = ExampleSerializer({'included': 'abc'})
|
||||
with pytest.raises(KeyError):
|
||||
serializer.data
|
||||
|
||||
@pytest.mark.skipif(True, reason='Disabling pending removal')
|
||||
def test_default_required_output_for_object(self):
|
||||
"""
|
||||
'default="something"' should require object attribute.
|
||||
|
||||
We need to handle this as the field will have an implicit
|
||||
'required=False', but it should still have a value.
|
||||
"""
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
omitted = serializers.CharField(default='abc')
|
||||
included = serializers.CharField()
|
||||
|
||||
instance = MockObject(included='abc')
|
||||
serializer = ExampleSerializer(instance)
|
||||
with pytest.raises(AttributeError):
|
||||
serializer.data
|
||||
|
||||
|
||||
class TestDefaultOutput:
|
||||
def setup(self):
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
has_default = serializers.CharField(default='abc')
|
||||
has_default = serializers.CharField(default='x')
|
||||
has_default_callable = serializers.CharField(default=lambda: 'y')
|
||||
no_default = serializers.CharField()
|
||||
self.Serializer = ExampleSerializer
|
||||
|
||||
|
@ -358,31 +326,31 @@ class TestDefaultOutput:
|
|||
"""
|
||||
'default="something"' should be used if dictionary key is missing from input.
|
||||
"""
|
||||
serializer = self.Serializer({'no_default': 'def'})
|
||||
assert serializer.data == {'has_default': 'abc', 'no_default': 'def'}
|
||||
serializer = self.Serializer({'no_default': 'abc'})
|
||||
assert serializer.data == {'has_default': 'x', 'has_default_callable': 'y', 'no_default': 'abc'}
|
||||
|
||||
def test_default_used_for_object(self):
|
||||
"""
|
||||
'default="something"' should be used if object attribute is missing from input.
|
||||
"""
|
||||
instance = MockObject(no_default='def')
|
||||
instance = MockObject(no_default='abc')
|
||||
serializer = self.Serializer(instance)
|
||||
assert serializer.data == {'has_default': 'abc', 'no_default': 'def'}
|
||||
assert serializer.data == {'has_default': 'x', 'has_default_callable': 'y', 'no_default': 'abc'}
|
||||
|
||||
def test_default_not_used_when_in_dict(self):
|
||||
"""
|
||||
'default="something"' should not be used if dictionary key is present in input.
|
||||
"""
|
||||
serializer = self.Serializer({'has_default': 'ghi', 'no_default': 'def'})
|
||||
assert serializer.data == {'has_default': 'ghi', 'no_default': 'def'}
|
||||
serializer = self.Serializer({'has_default': 'def', 'has_default_callable': 'ghi', 'no_default': 'abc'})
|
||||
assert serializer.data == {'has_default': 'def', 'has_default_callable': 'ghi', 'no_default': 'abc'}
|
||||
|
||||
def test_default_not_used_when_in_object(self):
|
||||
"""
|
||||
'default="something"' should not be used if object attribute is present in input.
|
||||
"""
|
||||
instance = MockObject(has_default='ghi', no_default='def')
|
||||
instance = MockObject(has_default='def', has_default_callable='ghi', no_default='abc')
|
||||
serializer = self.Serializer(instance)
|
||||
assert serializer.data == {'has_default': 'ghi', 'no_default': 'def'}
|
||||
assert serializer.data == {'has_default': 'def', 'has_default_callable': 'ghi', 'no_default': 'abc'}
|
||||
|
||||
|
||||
class TestCacheSerializerData:
|
||||
|
|
Loading…
Reference in New Issue
Block a user