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
0a35a932
Commit
0a35a932
authored
Aug 29, 2019
by
Kenzo-Hugo Hillion
♻
Browse files
continue update method for bulklist
parent
c439ea81
Pipeline
#14082
passed with stages
in 1 minute and 51 seconds
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
backend/metagenedb/apps/catalog/factory/__init__.py
View file @
0a35a932
from
.function
import
FunctionFactory
# noqa
from
.taxonomy
import
TaxonomyFactory
# noqa
backend/metagenedb/apps/catalog/factory/function.py
0 → 100644
View file @
0a35a932
from
factory
import
fuzzy
from
factory
import
DjangoModelFactory
,
lazy_attribute
from
faker
import
Factory
from
metagenedb.apps.catalog
import
models
faker
=
Factory
.
create
()
SELECTED_SOURCE
=
[
i
[
0
]
for
i
in
models
.
Function
.
SOURCE_CHOICES
]
class
FunctionFactory
(
DjangoModelFactory
):
class
Meta
:
model
=
models
.
Function
source
=
fuzzy
.
FuzzyChoice
(
SELECTED_SOURCE
)
@
lazy_attribute
def
function_id
(
self
):
return
str
(
faker
.
pyint
())
backend/metagenedb/apps/catalog/factory/taxonomy.py
View file @
0a35a932
...
...
@@ -17,4 +17,4 @@ class TaxonomyFactory(DjangoModelFactory):
@
lazy_attribute
def
tax_id
(
self
):
return
faker
.
pyint
()
return
str
(
faker
.
pyint
()
)
backend/metagenedb/apps/catalog/migrations/0009_meta_
taxonomy_functions
.py
→
backend/metagenedb/apps/catalog/migrations/0009_meta_
unique
.py
View file @
0a35a932
# Generated by Django 2.2.4 on 2019-08-29
09:23
# Generated by Django 2.2.4 on 2019-08-29
13:18
from
django.db
import
migrations
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
...
...
@@ -18,4 +18,9 @@ class Migration(migrations.Migration):
name
=
'taxonomy'
,
options
=
{
'ordering'
:
[
'-tax_id'
],
'verbose_name_plural'
:
'Taxonomy'
},
),
migrations
.
AlterField
(
model_name
=
'function'
,
name
=
'function_id'
,
field
=
models
.
CharField
(
db_index
=
True
,
max_length
=
100
,
unique
=
True
),
),
]
backend/metagenedb/apps/catalog/models/function.py
View file @
0a35a932
...
...
@@ -11,7 +11,7 @@ class Function(models.Model):
(
EGGNOG
,
'EggNOG'
)
]
function_id
=
models
.
CharField
(
max_length
=
100
,
db_index
=
True
)
function_id
=
models
.
CharField
(
max_length
=
100
,
db_index
=
True
,
unique
=
True
)
name
=
models
.
CharField
(
max_length
=
100
)
source
=
models
.
CharField
(
max_length
=
10
,
choices
=
SOURCE_CHOICES
,
default
=
UNDEFINED
)
...
...
backend/metagenedb/apps/catalog/models/gene.py
View file @
0a35a932
...
...
@@ -5,7 +5,7 @@ from .function import Function
class
Gene
(
models
.
Model
):
gene_name
=
models
.
CharField
(
max_length
=
100
,
unique
=
True
)
gene_id
=
models
.
SlugField
(
max_length
=
100
,
unique
=
True
)
gene_id
=
models
.
SlugField
(
max_length
=
100
,
db_index
=
True
,
unique
=
True
)
length
=
models
.
PositiveIntegerField
()
functions
=
models
.
ManyToManyField
(
Function
)
taxonomy
=
models
.
ForeignKey
(
...
...
backend/metagenedb/apps/catalog/serializers/bulk_list.py
View file @
0a35a932
...
...
@@ -9,14 +9,17 @@ class BulkListSerializer(serializers.ListSerializer):
for
field_name
,
relation_info
in
info
.
relations
.
items
():
if
relation_info
.
to_many
:
for
data_item
,
many_to_many_item
in
zip
(
validated_data
,
many_to_many
):
print
(
many_to_many_item
,
data_item
)
if
field_name
in
data_item
:
many_to_many_item
[
field_name
]
=
data_item
.
pop
(
field_name
)
else
:
print
(
"missing key"
)
print
(
many_to_many_item
,
data_item
)
return
many_to_many
def
_get_db_index_fields
(
self
,
info
):
db_index_keys
=
[]
for
field_name
,
field
in
info
.
fields
.
items
():
if
field
.
db_index
:
db_index_keys
.
append
(
field_name
)
return
db_index_keys
def
create
(
self
,
validated_data
):
ModelClass
=
self
.
Meta
.
model
instances
=
ModelClass
.
objects
.
bulk_create
(
...
...
@@ -24,5 +27,16 @@ class BulkListSerializer(serializers.ListSerializer):
)
return
instances
def
update
(
self
,
instances
,
validated_data
):
ModelClass
=
self
.
Meta
.
model
info
=
model_meta
.
get_field_info
(
ModelClass
)
db_index_fields
=
self
.
_get_db_index_fields
(
info
)
id_list
=
[
data
.
pop
(
db_index_fields
[
0
])
for
data
in
validated_data
]
# noqa
instances
=
ModelClass
.
objects
.
bulk_update
(
instances
,
[
'name'
]
)
return
instances
class
Meta
:
model
=
NotImplemented
backend/metagenedb/apps/catalog/serializers/test_bulk_list.py
View file @
0a35a932
...
...
@@ -4,9 +4,10 @@ from unittest.mock import Mock
from
rest_framework.test
import
APITestCase
from
metagenedb.common.utils.mocks.metagenedb
import
MetageneDBCatalogFunctionAPIMock
from
metagenedb.apps.catalog.serializers
import
FunctionSerializer
from
metagenedb.apps.catalog.serializers.bulk_list
import
BulkListSerializer
from
metagenedb.apps.catalog.factory
import
FunctionFactory
from
metagenedb.common.utils.mocks.metagenedb
import
MetageneDBCatalogFunctionAPIMock
class
BulkListSerializerTestExtractManyToMany
(
BulkListSerializer
):
...
...
@@ -18,7 +19,7 @@ class BulkListSerializerTestExtractManyToMany(BulkListSerializer):
pass
class
TestExtractManyToMany
(
TestCase
):
class
BaseTestBulkListSerializerMethods
(
TestCase
):
def
setUp
(
self
):
self
.
data
=
[
...
...
@@ -28,6 +29,9 @@ class TestExtractManyToMany(TestCase):
self
.
bulk_list_serializer
=
BulkListSerializerTestExtractManyToMany
()
self
.
info
=
Mock
()
class
TestExtractManyToMany
(
BaseTestBulkListSerializerMethods
):
def
test_extract_many_to_many
(
self
):
self
.
info
.
relations
=
{
'field1'
:
Mock
(
to_many
=
True
),
...
...
@@ -57,6 +61,27 @@ class TestExtractManyToMany(TestCase):
self
.
assertListEqual
(
ori_list
,
self
.
data
)
class
TestGetDbIndexFields
(
BaseTestBulkListSerializerMethods
):
def
test_get_db_index_fields
(
self
):
self
.
info
.
fields
=
{
'field1'
:
Mock
(
db_index
=
True
),
'field2'
:
Mock
(
db_index
=
False
)
}
expected_keys
=
[
'field1'
]
tested_keys
=
self
.
bulk_list_serializer
.
_get_db_index_fields
(
self
.
info
)
self
.
assertListEqual
(
tested_keys
,
expected_keys
)
def
test_get_db_index_fields_no_keys
(
self
):
self
.
info
.
fields
=
{
'field1'
:
Mock
(
db_index
=
False
),
'field2'
:
Mock
(
db_index
=
False
)
}
expected_keys
=
[]
tested_keys
=
self
.
bulk_list_serializer
.
_get_db_index_fields
(
self
.
info
)
self
.
assertListEqual
(
tested_keys
,
expected_keys
)
class
TestCreateBulk
(
APITestCase
):
"""
We are going to use an Serializer based on a real model since there are interactions with the DB.
...
...
@@ -83,3 +108,22 @@ class TestCreateBulk(APITestCase):
tested_instances
=
serializer
.
create
(
validated_data
)
self
.
assertEqual
(
self
.
function_api
.
get_all
()[
'count'
],
2
)
self
.
assertEqual
(
len
(
tested_instances
),
len
(
validated_data
))
def
test_update_existing_functions
(
self
):
functions
=
FunctionFactory
.
create_batch
(
2
)
validated_data
=
[
{
"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
)
serializer
.
update
(
functions
,
validated_data
)
self
.
assertEqual
(
self
.
function_api
.
get_all
()[
'count'
],
2
)
print
(
self
.
function_api
.
get_all
())
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