mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 04:02:35 +03:00
fix formatting issues for list serializer validation fix
This commit is contained in:
parent
c1ee7e75b1
commit
74cb5002f5
|
@ -3,7 +3,6 @@ import pickle
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from collections import ChainMap
|
from collections import ChainMap
|
||||||
from collections.abc import Mapping
|
from collections.abc import Mapping
|
||||||
|
|
||||||
|
@ -844,64 +843,4 @@ class TestMultipleObjectsValidation(unittest.TestCase):
|
||||||
instance=self.objs[:-1],
|
instance=self.objs[:-1],
|
||||||
many=True,
|
many=True,
|
||||||
partial=True,
|
partial=True,
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MyClass(models.Model):
|
|
||||||
name = models.CharField(max_length=100)
|
|
||||||
value = models.CharField(max_length=100, blank=True)
|
|
||||||
|
|
||||||
app_label = "test"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_valid(self):
|
|
||||||
return self.name == 'valid'
|
|
||||||
|
|
||||||
|
|
||||||
class MyClassSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = MyClass
|
|
||||||
fields = ('id', 'name', 'value')
|
|
||||||
|
|
||||||
def validate_value(self, value):
|
|
||||||
if value and not self.instance.is_valid:
|
|
||||||
raise serializers.ValidationError(
|
|
||||||
'Status cannot be set for invalid instance')
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
class TestMultipleObjectsValidation(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.objs = [
|
|
||||||
MyClass(name='valid'),
|
|
||||||
MyClass(name='invalid'),
|
|
||||||
MyClass(name='other'),
|
|
||||||
]
|
|
||||||
|
|
||||||
def test_multiple_objects_are_validated_separately(self):
|
|
||||||
|
|
||||||
serializer = MyClassSerializer(
|
|
||||||
data=[{'value': 'set', 'id': instance.id} for instance in
|
|
||||||
self.objs],
|
|
||||||
instance=self.objs,
|
|
||||||
many=True,
|
|
||||||
partial=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert not serializer.is_valid()
|
|
||||||
assert serializer.errors == [
|
|
||||||
{},
|
|
||||||
{'value': ['Status cannot be set for invalid instance']},
|
|
||||||
{'value': ['Status cannot be set for invalid instance']}
|
|
||||||
]
|
|
||||||
|
|
||||||
def test_exception_raised_when_data_and_instance_length_different(self):
|
|
||||||
|
|
||||||
with self.assertRaises(AssertionError):
|
|
||||||
serializer = MyClassSerializer(
|
|
||||||
data=[{'value': 'set', 'id': instance.id} for instance in
|
|
||||||
self.objs],
|
|
||||||
instance=self.objs[:-1],
|
|
||||||
many=True,
|
|
||||||
partial=True,
|
|
||||||
)
|
)
|
Loading…
Reference in New Issue
Block a user