test_function.py 2.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
37
            tested_dict = self.function_api.get(self.kegg_function.function_id, params=query_params)
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        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
        }
52
        tested_dict = self.function_api.get(self.eggnog_function.function_id, params=query_params)
53
        self.assertDictEqual(tested_dict, expected_function)