TestOpenAPI.test_read_napp_info()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 2
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""kytos.utils.openapi tests."""
2 1
import unittest
3 1
from pathlib import Path
4 1
from unittest.mock import MagicMock, patch
5
6 1
from kytos.utils.openapi import OpenAPI
7
8 1
MAIN_FILE = '''
9
    from kytos.core import KytosNApp, log
10
11
    class Main(KytosNApp):
12
        def setup(self):
13
            pass
14
15
        def execute(self):
16
            pass
17
18
        def shutdown(self):
19
            pass
20
21
        @rest("/any", methods=["GET"])
22
        def any(self):
23
            """docstring"""
24
            pass
25
'''
26
27
28
# pylint: disable=arguments-differ, protected-access
29 1
class TestOpenAPI(unittest.TestCase):
30
    """Test the class OpenAPI."""
31
32 1
    @patch('builtins.open')
33 1
    def setUp(self, mock_open):
34
        """Execute steps before each tests."""
35 1
        data = MagicMock()
36 1
        data.read.return_value = '{"username": "kytos", "name": "mef_eline"}'
37 1
        enter_data = MagicMock()
38 1
        enter_data.__enter__.return_value = data
39 1
        mock_open.return_value = enter_data
40
41 1
        napp_path = Path('')
42 1
        tpl_path = Path('')
43 1
        self.open_api = OpenAPI(napp_path, tpl_path)
44
45 1
    @patch('pathlib.Path.open')
46 1
    @patch('kytos.utils.openapi.OpenAPI._save')
47 1
    def test_render_template(self, *args):
48
        """Test render_template method."""
49 1
        (mock_save, mock_open) = args
50 1
        open_file = MagicMock()
51 1
        open_file.read.return_value = MAIN_FILE
52 1
        mock_open.return_value = open_file
53
54 1
        self.open_api.render_template()
55
56 1
        description = 'TODO write/remove the description'
57 1
        path_dict = {'get': {'summary': 'docstring',
58
                             'description': description}}
59 1
        expected = {'napp': {'username': 'kytos', 'name': 'mef_eline'},
60
                    'paths': {'/api/kytos/mef_eline/any': path_dict}}
61 1
        mock_save.assert_called_with(expected)
62
63 1
    @patch('pathlib.Path.open')
64 1
    def test_read_napp_info(self, mock_open):
65
        """Test _read_napp_info method."""
66 1
        open_file = MagicMock()
67 1
        open_file.read.return_value = '{"info": "ABC"}'
68 1
        mock_open.return_value = open_file
69
70 1
        response = self.open_api._read_napp_info()
71
72 1
        self.assertEqual(response, {'info': 'ABC'})
73
74 1
    @patch('pathlib.Path.open')
75 1
    @patch('jinja2.Environment.get_template')
76 1
    def test_save(self, *args):
77
        """Test _save method."""
78 1
        (mock_get_template, mock_open) = args
79 1
        tmpl = MagicMock()
80 1
        tmpl.render.return_value = 'content'
81 1
        mock_get_template.return_value = tmpl
82
83 1
        enter_openapi = MagicMock()
84 1
        mock_open.return_value.__enter__.return_value = enter_openapi
85
86 1
        self.open_api._save('context')
87
88 1
        tmpl.render.assert_called_with('context')
89
        enter_openapi.write.assert_called_with('content')
90