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

add tests for wrong query params

parent 1ba5dc9a
Pipeline #18554 passed with stages
in 2 minutes and 22 seconds
from requests.exceptions import HTTPError
from rest_framework.test import APITestCase
from metagenedb.apps.catalog.factory import FunctionFactory
......@@ -136,3 +137,21 @@ class TestOperationsBulkViewSet(APITestCase):
self.assertEqual(self.function_api.get_all()['count'], 4)
for element in data:
self.assertDictEqual(self.function_api.get(element['function_id']), element)
def test_get_item(self):
function = FunctionFactory.create(name="Test")
response = self.function_api.get(function.function_id)
self.assertEqual(response['name'], 'Test')
# Use wrong query params, expect 422 returned
fake_qparams = {'qparam': 'fake'}
with self.assertRaises(HTTPError):
response = self.function_api.get(function.function_id, params=fake_qparams)
def test_get_items(self):
FunctionFactory.create_batch(5)
response = self.function_api.get_all()
self.assertEqual(response['count'], 5)
# Use wrong query params, expect 422 returned
fake_qparams = {'qparam': 'fake'}
with self.assertRaises(HTTPError):
response = self.function_api.get_all(params=fake_qparams)
......@@ -34,7 +34,7 @@ class TestFunctionViewSet(APITestCase):
}
with mock.patch(class_to_mock) as MockGetFunctionExternalInfo:
MockGetFunctionExternalInfo.return_value.get_details.return_value = detailed_kegg
tested_dict = self.function_api.get(self.kegg_function.function_id, query_params=query_params)
tested_dict = self.function_api.get(self.kegg_function.function_id, params=query_params)
self.assertDictEqual(tested_dict, detailed_kegg)
def test_retrieve_detailed_unavailable(self):
......@@ -49,5 +49,5 @@ class TestFunctionViewSet(APITestCase):
'name': self.eggnog_function.name,
'source': self.eggnog_function.source
}
tested_dict = self.function_api.get(self.eggnog_function.function_id, query_params=query_params)
tested_dict = self.function_api.get(self.eggnog_function.function_id, params=query_params)
self.assertDictEqual(tested_dict, expected_function)
......@@ -2,7 +2,6 @@ from requests.exceptions import HTTPError
from bioapi import MetageneDBCatalogGeneAPI
from django.urls import reverse
from django.utils.http import urlencode
class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
......@@ -13,27 +12,27 @@ class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
KEY_ID = ''
BASE_REVERSE = 'api'
REVERSE_PATH = ''
BAD_REQUESTS = range(400, 452)
def __init__(self, client):
self.client = client
self.reverse_path = ':'.join([self.BASE_REVERSE, self.REVERSE_PATH])
def get_all(self, params=None):
url = reverse(f'{self.reverse_path}-list')
if params is not None:
query_params = urlencode(params)
return self.client.get(f"{url}?{query_params}").json()
return self.client.get(f"{url}").json()
def get(self, entry_id, query_params=None):
response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}), query_params)
if response.status_code == 404:
response = self.client.get(reverse(f'{self.reverse_path}-list'), params)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
def get(self, entry_id, params=None):
response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}), params)
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
def post(self, data):
response = self.client.post(reverse(f'{self.reverse_path}-list'), data, format='json')
if response.status_code == 400:
if response.status_code in self.BAD_REQUESTS:
raise HTTPError
return response.json()
......
Markdown is supported
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