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

continue update method for bulklist

parent c439ea81
Pipeline #14082 passed with stages
in 1 minute and 51 seconds
from .function import FunctionFactory # noqa
from .taxonomy import TaxonomyFactory # noqa
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())
......@@ -17,4 +17,4 @@ class TaxonomyFactory(DjangoModelFactory):
@lazy_attribute
def tax_id(self):
return faker.pyint()
return str(faker.pyint())
# 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),
),
]
......@@ -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)
......
......@@ -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(
......
......@@ -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
......@@ -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())
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