Test Failed
Pull Request — master (#277)
by Gleyberson
01:26
created

TestOpenAPI.test_render_template()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
"""kytos.utils.openapi tests."""
2
import unittest
3
from pathlib import Path
4
from unittest.mock import MagicMock, patch
5
6
from kytos.utils.openapi import OpenAPI
7
8
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
class TestOpenAPI(unittest.TestCase):
30
    """Test the class OpenAPI."""
31
32
    @patch('builtins.open')
33
    def setUp(self, mock_open):
34
        """Execute steps before each tests."""
35
        data = MagicMock()
36
        data.read.return_value = '{"username": "kytos", "name": "mef_eline"}'
37
        enter_data = MagicMock()
38
        enter_data.__enter__.return_value = data
39
        mock_open.return_value = enter_data
40
41
        napp_path = Path('')
42
        tpl_path = Path('')
43
        self.open_api = OpenAPI(napp_path, tpl_path)
44
45
    @patch('pathlib.Path.open')
46
    @patch('kytos.utils.openapi.OpenAPI._save')
47
    def test_render_template(self, *args):
48
        """Test render_template method."""
49
        (mock_save, mock_open) = args
50
        open_file = MagicMock()
51
        open_file.read.return_value = MAIN_FILE
52
        mock_open.return_value = open_file
53
54
        self.open_api.render_template()
55
56
        description = 'TODO write/remove the description'
57
        path_dict = {'get': {'summary': 'docstring',
58
                             'description': description}}
59
        expected = {'napp': {'username': 'kytos', 'name': 'mef_eline'},
60
                    'paths': {'/api/kytos/mef_eline/any': path_dict}}
61
        mock_save.assert_called_with(expected)
62
63
    @patch('pathlib.Path.open')
64
    def test_read_napp_info(self, mock_open):
65
        """Test _read_napp_info method."""
66
        open_file = MagicMock()
67
        open_file.read.return_value = '{"info": "ABC"}'
68
        mock_open.return_value = open_file
69
70
        response = self.open_api._read_napp_info()
71
72
        self.assertEqual(response, {'info': 'ABC'})
73
74
    @patch('pathlib.Path.open')
75
    @patch('jinja2.Environment.get_template')
76
    def test_save(self, *args):
77
        """Test _save method."""
78
        (mock_get_template, mock_open) = args
79
        tmpl = MagicMock()
80
        tmpl.render.return_value = 'content'
81
        mock_get_template.return_value = tmpl
82
83
        enter_openapi = MagicMock()
84
        mock_open.return_value.__enter__.return_value = enter_openapi
85
86
        self.open_api._save('context')
87
88
        tmpl.render.assert_called_with('context')
89
        enter_openapi.write.assert_called_with('content')
90