Skip to content
Snippets Groups Projects
Commit 94f11eb5 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Prevent IntegrityError on duplicate ML classes on CreateClassifications

parent c5813c9a
No related branches found
No related tags found
1 merge request!1005Prevent IntegrityError on duplicate ML classes on CreateClassifications
......@@ -477,6 +477,15 @@ class ClassificationsSerializer(serializers.Serializer):
'worker_version': ['This field XOR classifier field must be set to create classifications']
})
ml_class_names = [
classification['ml_class']
for classification in data['classifications']
]
if len(ml_class_names) != len(set(ml_class_names)):
raise ValidationError({
'classifications': ['Duplicated ML classes are not allowed from the same source or worker version.']
})
return data
def create(self, validated_data):
......
......@@ -248,3 +248,22 @@ class TestBulkClassification(FixtureAPITestCase):
('catte', 0.85, True),
],
)
def test_bulk_classification_no_duplicates(self):
"""
Test the bulk classification API prevents creating classifications with duplicate ML classes
"""
self.client.force_login(self.user)
with self.assertNumQueries(4):
response = self.client.post(
reverse('api:classification-bulk'),
format='json',
data=self.create_classifications_data([
{"class_name": 'dog', "confidence": 0.99},
{"class_name": 'dog', "confidence": 0.99},
])
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(response.json(), {
'classifications': ['Duplicated ML classes are not allowed from the same source or worker version.']
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment