mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Merge pull request #1112 from tamakisquare/issue-1111
Test case and fix for issue 1111
This commit is contained in:
commit
6a40202a64
|
@ -262,10 +262,13 @@ class BaseSerializer(WritableField):
|
|||
for field_name, field in self.fields.items():
|
||||
if field_name in self._errors:
|
||||
continue
|
||||
|
||||
source = field.source or field_name
|
||||
if self.partial and source not in attrs:
|
||||
continue
|
||||
try:
|
||||
validate_method = getattr(self, 'validate_%s' % field_name, None)
|
||||
if validate_method:
|
||||
source = field.source or field_name
|
||||
attrs = validate_method(attrs, source)
|
||||
except ValidationError as err:
|
||||
self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages)
|
||||
|
|
|
@ -511,6 +511,33 @@ class CustomValidationTests(TestCase):
|
|||
self.assertFalse(serializer.is_valid())
|
||||
self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']})
|
||||
|
||||
def test_partial_update(self):
|
||||
"""
|
||||
Make sure that validate_email isn't called when partial=True and email
|
||||
isn't found in data.
|
||||
"""
|
||||
initial_data = {
|
||||
'email': 'tom@example.com',
|
||||
'content': 'A test comment',
|
||||
'created': datetime.datetime(2012, 1, 1)
|
||||
}
|
||||
|
||||
serializer = self.CommentSerializerWithFieldValidator(data=initial_data)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
instance = serializer.object
|
||||
|
||||
new_content = 'An *updated* test comment'
|
||||
partial_data = {
|
||||
'content': new_content
|
||||
}
|
||||
|
||||
serializer = self.CommentSerializerWithFieldValidator(instance=instance,
|
||||
data=partial_data,
|
||||
partial=True)
|
||||
self.assertEqual(serializer.is_valid(), True)
|
||||
instance = serializer.object
|
||||
self.assertEqual(instance.content, new_content)
|
||||
|
||||
|
||||
class PositiveIntegerAsChoiceTests(TestCase):
|
||||
def test_positive_integer_in_json_is_correctly_parsed(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user