mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 12:12:19 +03:00
Add tests for unique validation for bulk creation in model serializers
The unique validation in model serializers only check for existing entries on the database. If the serializer is instanced with many=True and receives more than one item in the data argument having the same value for a unique field, the .is_valid() method returns True, but calling .save() will raise an IntegrityError.
This commit is contained in:
parent
ac0f0a1774
commit
0123a2c489
|
@ -118,6 +118,26 @@ class TestUniquenessValidation(TestCase):
|
||||||
serializer = UniquenessIntegerSerializer(data={'integer': 'abc'})
|
serializer = UniquenessIntegerSerializer(data={'integer': 'abc'})
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
|
|
||||||
|
def test_validate_uniqueness_in_bulk_creation(self):
|
||||||
|
data = [{'username': 'existing'}, {'username': 'non-existing'}]
|
||||||
|
serializer = UniquenessSerializer(data=data, many=True)
|
||||||
|
assert not serializer.is_valid()
|
||||||
|
assert serializer.errors == [
|
||||||
|
{'username': ['uniqueness model with this username already exists.']},
|
||||||
|
{},
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_validate_uniqueness_between_data_items_in_bulk_creation(self):
|
||||||
|
data = [{'username': 'non-existing'}, {'username': 'non-existing'}]
|
||||||
|
serializer = UniquenessSerializer(data=data, many=True)
|
||||||
|
assert serializer.is_valid()
|
||||||
|
assert serializer.save()
|
||||||
|
# assert not serializer.is_valid()
|
||||||
|
# assert serializer.errors == [
|
||||||
|
# {'username': ['uniqueness model with this username already exists.']},
|
||||||
|
# {'username': ['uniqueness model with this username already exists.']},
|
||||||
|
# ]
|
||||||
|
|
||||||
|
|
||||||
# Tests for `UniqueTogetherValidator`
|
# Tests for `UniqueTogetherValidator`
|
||||||
# -----------------------------------
|
# -----------------------------------
|
||||||
|
|
Loading…
Reference in New Issue
Block a user