From c439ea81642aa80c4914bbc6a1006424f0a1f68d Mon Sep 17 00:00:00 2001 From: Kenzo-Hugo Hillion <kenzo-hugo.hillion1@pasteur.fr> Date: Thu, 29 Aug 2019 11:24:30 +0200 Subject: [PATCH] test creation for bulklistserializer --- backend/metagenedb/api/catalog/urls.py | 6 ++-- ...omy.py => 0009_meta_taxonomy_functions.py} | 6 +++- .../apps/catalog/models/function.py | 3 ++ .../apps/catalog/serializers/bulk_list.py | 2 +- .../catalog/serializers/test_bulk_list.py | 32 +++++++++++++++++++ .../common/utils/mocks/metagenedb.py | 5 +++ 6 files changed, 49 insertions(+), 5 deletions(-) rename backend/metagenedb/apps/catalog/migrations/{0009_meta_taxonomy.py => 0009_meta_taxonomy_functions.py} (65%) diff --git a/backend/metagenedb/api/catalog/urls.py b/backend/metagenedb/api/catalog/urls.py index da5e3c9..b635181 100644 --- a/backend/metagenedb/api/catalog/urls.py +++ b/backend/metagenedb/api/catalog/urls.py @@ -5,9 +5,9 @@ from metagenedb.api.catalog.views import FunctionViewSet, GeneViewSet, TaxonomyV api_router = DefaultRouter() -api_router.register(r'functions', FunctionViewSet, base_name='functions') -api_router.register(r'genes', GeneViewSet, base_name='genes') -api_router.register(r'taxonomy', TaxonomyViewSet, base_name='taxonomy') +api_router.register(r'functions', FunctionViewSet, basename='functions') +api_router.register(r'genes', GeneViewSet, basename='genes') +api_router.register(r'taxonomy', TaxonomyViewSet, basename='taxonomy') urlpatterns = [ diff --git a/backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy.py b/backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy_functions.py similarity index 65% rename from backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy.py rename to backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy_functions.py index 88c90ac..68adb09 100644 --- a/backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy.py +++ b/backend/metagenedb/apps/catalog/migrations/0009_meta_taxonomy_functions.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.4 on 2019-08-27 11:05 +# Generated by Django 2.2.4 on 2019-08-29 09:23 from django.db import migrations @@ -10,6 +10,10 @@ class Migration(migrations.Migration): ] operations = [ + migrations.AlterModelOptions( + name='function', + options={'ordering': ['-function_id']}, + ), migrations.AlterModelOptions( name='taxonomy', options={'ordering': ['-tax_id'], 'verbose_name_plural': 'Taxonomy'}, diff --git a/backend/metagenedb/apps/catalog/models/function.py b/backend/metagenedb/apps/catalog/models/function.py index de43a73..634f54d 100644 --- a/backend/metagenedb/apps/catalog/models/function.py +++ b/backend/metagenedb/apps/catalog/models/function.py @@ -18,6 +18,9 @@ class Function(models.Model): def __str__(self): return self.function_id + class Meta: + ordering = ['-function_id'] + class KeggOrthology(Function): SOURCE = 'kegg' diff --git a/backend/metagenedb/apps/catalog/serializers/bulk_list.py b/backend/metagenedb/apps/catalog/serializers/bulk_list.py index 4643630..ccbb73b 100644 --- a/backend/metagenedb/apps/catalog/serializers/bulk_list.py +++ b/backend/metagenedb/apps/catalog/serializers/bulk_list.py @@ -1,5 +1,5 @@ from rest_framework import serializers -from rest_framework.utils import model_meta +from rest_framework.utils import model_meta # noqa class BulkListSerializer(serializers.ListSerializer): diff --git a/backend/metagenedb/apps/catalog/serializers/test_bulk_list.py b/backend/metagenedb/apps/catalog/serializers/test_bulk_list.py index 9c39a1a..12b3318 100644 --- a/backend/metagenedb/apps/catalog/serializers/test_bulk_list.py +++ b/backend/metagenedb/apps/catalog/serializers/test_bulk_list.py @@ -2,6 +2,10 @@ from copy import deepcopy from unittest import TestCase 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 @@ -51,3 +55,31 @@ class TestExtractManyToMany(TestCase): tested_list = self.bulk_list_serializer._extract_many_to_many(self.data, self.info) self.assertListEqual(tested_list, expected_list) self.assertListEqual(ori_list, self.data) + + +class TestCreateBulk(APITestCase): + """ + We are going to use an Serializer based on a real model since there are interactions with the DB. + We are using the FunctionListSerializer for the moment (through FunctionSerializer(many=True)). + """ + + def setUp(self): + self.function_api = MetageneDBCatalogFunctionAPIMock(self.client) + + def test_create_functions(self): + validated_data = [ + { + "function_id": "k_test1", + "source": "kegg", + "name": "Kegg Test 1" + }, + { + "function_id": "k_test2", + "source": "kegg", + "name": "Kegg Test 2" + } + ] + serializer = FunctionSerializer(many=True) + tested_instances = serializer.create(validated_data) + self.assertEqual(self.function_api.get_all()['count'], 2) + self.assertEqual(len(tested_instances), len(validated_data)) diff --git a/backend/metagenedb/common/utils/mocks/metagenedb.py b/backend/metagenedb/common/utils/mocks/metagenedb.py index d9fbaed..d4a84f4 100644 --- a/backend/metagenedb/common/utils/mocks/metagenedb.py +++ b/backend/metagenedb/common/utils/mocks/metagenedb.py @@ -51,3 +51,8 @@ class MetageneDBCatalogGeneAPIMock(MetageneDBAPIMock): class MetageneDBCatalogTaxonomyAPIMock(MetageneDBAPIMock): KEY_ID = 'gene_id' REVERSE_PATH = 'catalog:v1:taxonomy' + + +class MetageneDBCatalogFunctionAPIMock(MetageneDBAPIMock): + KEY_ID = 'function_id' + REVERSE_PATH = 'catalog:v1:functions' -- GitLab