Commit 0a7c1a08 authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion
Browse files

Add steps to return error if duplicate in list

parent 3c3073ca
Pipeline #14506 passed with stages
in 4 minutes and 25 seconds
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.exceptions import ValidationError, ErrorDetail
from rest_framework.fields import SkipField
from rest_framework.settings import api_settings
from rest_framework.utils import html, model_meta
......@@ -68,6 +68,7 @@ class BulkListSerializer(serializers.ListSerializer):
ret = []
errors = []
primary_keys = set()
for item in data:
try:
if isinstance(self.instance, dict):
......@@ -75,6 +76,10 @@ class BulkListSerializer(serializers.ListSerializer):
validated = self.child.run_validation(item)
else:
validated = self.child.run_validation(item)
if item[lookup_field] not in primary_keys:
primary_keys.add(item[lookup_field])
else:
errors.append(ErrorDetail(item[lookup_field], code='duplicate'))
except ValidationError as exc:
errors.append(exc.detail)
else:
......
......@@ -3,6 +3,7 @@ from unittest import TestCase
from unittest.mock import Mock
from rest_framework.test import APITestCase
from rest_framework.exceptions import ValidationError
from metagenedb.apps.catalog.serializers import FunctionSerializer
from metagenedb.apps.catalog.serializers.bulk_list import BulkListSerializer
......@@ -140,3 +141,26 @@ class TestOperationsBulk(APITestCase):
self.assertEqual(self.function_api.get_all()['count'], 2)
for data in validated_data:
self.assertDictEqual(self.function_api.get(data['function_id']), data)
def test_to_internal_value_duplicate(self):
functions = FunctionFactory.build_batch(2)
data = [
{
"function_id": functions[0].function_id,
"source": functions[0].source,
"name": "Test 1"
},
{
"function_id": functions[0].function_id,
"source": functions[0].source,
"name": "Test 1"
},
{
"function_id": functions[1].function_id,
"source": functions[1].source,
"name": "Test 2"
}
]
serializer = FunctionSerializer(many=True)
with self.assertRaises(ValidationError):
serializer.to_internal_value(data)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment