Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Metagenomics
metagenedb
Commits
0a7c1a08
Commit
0a7c1a08
authored
Sep 11, 2019
by
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
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/apps/catalog/serializers/bulk_list.py
View file @
0a7c1a08
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
:
...
...
backend/metagenedb/apps/catalog/serializers/test_bulk_list.py
View file @
0a7c1a08
...
...
@@ -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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment