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:
Mariana Bedran Lesche 2019-01-07 16:04:16 -03:00 committed by mari
parent ac0f0a1774
commit 0123a2c489

View File

@ -118,6 +118,26 @@ class TestUniquenessValidation(TestCase):
serializer = UniquenessIntegerSerializer(data={'integer': 'abc'})
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`
# -----------------------------------