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

Add tests for custom retrieve function

parent 51407c65
Pipeline #18460 passed with stages
in 2 minutes and 12 seconds
from rest_framework.test import APITestCase
import mock
from metagenedb.apps.catalog.factory import FunctionFactory
from metagenedb.common.utils.mocks.metagenedb import MetageneDBCatalogFunctionAPIMock
class TestFunctionViewSet(APITestCase):
def setUp(self):
self.function_api = MetageneDBCatalogFunctionAPIMock(self.client)
self.kegg_function = FunctionFactory.create(source='kegg')
self.eggnog_function = FunctionFactory.create(source='eggnog')
def test_retrieve(self):
for function in [self.kegg_function, self.eggnog_function]:
expected_function = {
'function_id': function.function_id,
'name': function.name,
'source': function.source
}
self.assertDictEqual(self.function_api.get(function.function_id), expected_function)
def test_retrieve_detailed_available(self):
query_params = {
'detailed': 'true'
}
class_to_mock = 'metagenedb.api.catalog.views.function.GetFunctionExternalInfo'
detailed_kegg = {
'function_id': self.kegg_function.function_id,
'name': self.kegg_function.name,
'details': 'some details'
}
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)
self.assertDictEqual(tested_dict, detailed_kegg)
def test_retrieve_detailed_unavailable(self):
"""
eggnog is not available so it is a good example and should return the DB value.
"""
query_params = {
'detailed': 'true'
}
expected_function = {
'function_id': self.eggnog_function.function_id,
'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)
self.assertDictEqual(tested_dict, expected_function)
......@@ -25,8 +25,8 @@ class MetageneDBAPIMock(MetageneDBCatalogGeneAPI):
return self.client.get(f"{url}?{query_params}").json()
return self.client.get(f"{url}").json()
def get(self, entry_id):
response = self.client.get(reverse(f'{self.reverse_path}-detail', kwargs={self.KEY_ID: entry_id}))
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:
raise HTTPError
return response.json()
......
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