mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-04 20:40:14 +03:00
Add failing test case to highlight side effects caused by calling repr in __init__
This commit is contained in:
parent
07efbdb45e
commit
59841c0c44
|
@ -132,6 +132,31 @@ class TestModelSerializer(TestCase):
|
||||||
msginitial = 'Cannot use ModelSerializer with Abstract Models.'
|
msginitial = 'Cannot use ModelSerializer with Abstract Models.'
|
||||||
assert str(excinfo.exception).startswith(msginitial)
|
assert str(excinfo.exception).startswith(msginitial)
|
||||||
|
|
||||||
|
def test_repr_side_effects(self):
|
||||||
|
"""
|
||||||
|
Test that modifying ModelSerializer.fields in __init__ while calling
|
||||||
|
`repr` does not have side-effects (issue #3196)
|
||||||
|
"""
|
||||||
|
|
||||||
|
class TestSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = OneFieldModel
|
||||||
|
fields = ('char_field',)
|
||||||
|
|
||||||
|
def __init__(self, instance=None, data=serializers.empty, **kwargs):
|
||||||
|
super(TestSerializer, self).__init__(instance, data, **kwargs)
|
||||||
|
repr(self)
|
||||||
|
self.fields.pop('char_field')
|
||||||
|
|
||||||
|
instance = OneFieldModel.objects.create(char_field='value')
|
||||||
|
|
||||||
|
new_data = {'char_field': 'new_value'}
|
||||||
|
serializer = TestSerializer(instance, new_data)
|
||||||
|
|
||||||
|
serializer.is_valid()
|
||||||
|
|
||||||
|
self.assertDictEqual(serializer.validated_data, {})
|
||||||
|
|
||||||
|
|
||||||
class TestRegularFieldMappings(TestCase):
|
class TestRegularFieldMappings(TestCase):
|
||||||
def test_regular_fields(self):
|
def test_regular_fields(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user