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

add test for creation through API for function as list or element

parent d955fc90
Pipeline #14169 passed with stages
in 1 minute and 44 seconds
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
class BulkViewSet(ModelViewSet):
def _created_payload(self, serializer, request):
if isinstance(request.data, list):
return {
'path': request.path_info,
'created': {
'count': len(serializer.data)
}
}
return serializer.data
def create(self, request, *args, **kwargs):
if isinstance(request.data, list):
serializer = self.get_serializer(data=request.data, many=True)
else:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(
self._created_payload(serializer, request),
status=status.HTTP_201_CREATED, headers=headers
)
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from metagenedb.api.catalog.filters import FunctionFilter
from metagenedb.apps.catalog.models import Function
from metagenedb.apps.catalog.serializers import FunctionSerializer
from .bulk_viewset import BulkViewSet
class FunctionViewSet(ModelViewSet):
class FunctionViewSet(BulkViewSet):
queryset = Function.objects.all()
serializer_class = FunctionSerializer
lookup_field = 'function_id'
filterset_class = FunctionFilter
def create(self, request, *args, **kwargs):
if isinstance(request.data, list):
serializer = self.get_serializer(data=request.data, many=True)
else:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
from rest_framework.test import APITestCase
from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogFunctionAPIMock
class TestOperationsBulkViewSet(APITestCase):
"""
We are testing the different functions through the API directly through the mock redirecting
requests to the test database.
The extent is a bit more than a unittest since it is not just involving concerned methods.
"""
def setUp(self):
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client)
def test_create_function(self):
data = {
"function_id": 'k_test1',
"source": "kegg",
"name": "Kegg Test 1"
}
response = self.function_api.post(data)
self.assertDictEqual(response, data)
self.assertEqual(self.function_api.get_all()['count'], 1)
self.assertDictEqual(self.function_api.get(data['function_id']), data)
def test_create_functions(self):
data = [
{
"function_id": "k_test1",
"source": "kegg",
"name": "Kegg Test 1"
},
{
"function_id": "k_test2",
"source": "kegg",
"name": "Kegg Test 2"
}
]
expected_response = {
'path': '/api/catalog/v1/functions/',
'created': {
'count': len(data)
}
}
response = self.function_api.post(data)
self.assertDictEqual(response, expected_response)
self.assertEqual(self.function_api.get_all()['count'], 2)
for element in data:
self.assertDictEqual(self.function_api.get(element['function_id']), element)
def test_update_existing_functions(self):
pass
......@@ -93,7 +93,7 @@ class TestGetKeyFields(BaseTestBulkListSerializerMethods):
class TestOperationsBulk(APITestCase):
"""
We are going to use an Serializer based on a real model since there are interactions with the DB.
We are going to use a 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)).
"""
......
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