test_bulk_viewset.py 5.85 KB
Newer Older
1
from requests.exceptions import HTTPError
2
3
from rest_framework.test import APITestCase

4
from metagenedb.apps.catalog.factory import FunctionFactory
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)

55
56
57
58
59
60
61
62
63
    def test_update_existing_function(self):
        function = FunctionFactory()
        data = {
            "function_id": function.function_id,
            "source": function.source,
            "name": "Kegg Test 1"
        }
        self.function_api.put(data, function.function_id)
        self.assertDictEqual(self.function_api.get(function.function_id), data)
Kenzo-Hugo Hillion's avatar
Kenzo-Hugo Hillion committed
64

65
    def test_update_existing_functions(self):
66
        functions = FunctionFactory.create_batch(2)
67
68
        data = [
            {
69
70
71
                "function_id": functions[0].function_id,
                "source": functions[0].source,
                "name": "Test 1"
72
73
            },
            {
74
75
76
                "function_id": functions[1].function_id,
                "source": functions[1].source,
                "name": "Test 2"
77
78
            }
        ]
79
80
        for element in data:
            self.assertNotEqual(self.function_api.get(element['function_id']), element)
81
82
83
        response = self.function_api.put(data)
        self.assertEqual(response.get('created').get('count'), 0)
        self.assertEqual(response.get('updated').get('count'), 2)
84
85
86
        self.assertEqual(self.function_api.get_all()['count'], 2)
        for element in data:
            self.assertDictEqual(self.function_api.get(element['function_id']), element)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    def test_create_through_update_functions(self):
        functions = FunctionFactory.build_batch(2)
        data = [
            {
                "function_id": functions[0].function_id,
                "source": functions[0].source,
                "name": "Test 1"
            },
            {
                "function_id": functions[1].function_id,
                "source": functions[1].source,
                "name": "Test 2"
            }
        ]
        response = self.function_api.put(data)
        self.assertEqual(response.get('created').get('count'), 2)
        self.assertEqual(response.get('updated').get('count'), 0)
        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_create_and_update_functions(self):
        functions = FunctionFactory.create_batch(2)
        new_functions = FunctionFactory.build_batch(2)
        data = [
            {
                "function_id": functions[0].function_id,
                "source": functions[0].source,
                "name": "Test 1"
            },
            {
                "function_id": functions[1].function_id,
                "source": functions[1].source,
                "name": "Test 2"
            },
            {
                "function_id": new_functions[0].function_id,
                "source": new_functions[0].source,
                "name": "New Test 1"
            },
            {
                "function_id": new_functions[1].function_id,
                "source": new_functions[1].source,
                "name": "New Test 2"
            }
        ]
        response = self.function_api.put(data)
        self.assertEqual(response.get('created').get('count'), 2)
        self.assertEqual(response.get('updated').get('count'), 2)
        self.assertEqual(self.function_api.get_all()['count'], 4)
        for element in data:
            self.assertDictEqual(self.function_api.get(element['function_id']), element)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

    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)