Passed
Pull Request — master (#96)
by Jose
03:07
created

TestStoreHouse.test_save_flow()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
"""Module to test the storehouse client."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, PropertyMock, patch
4
5
from kytos.lib.helpers import get_controller_mock
6
7
8
# pylint: disable=too-many-public-methods
9
class TestStoreHouse(TestCase):
10
    """Test the Main class."""
11
12
    # pylint: disable=too-many-public-methods
13
    def setUp(self):
14
        """Execute steps before each tests.
15
16
        Set the server_name_url_url from kytos/flow_manager
17
        """
18
        self.server_name_url = 'http://localhost:8181/api/kytos/flow_manager'
19
20
        patch('kytos.core.helpers.run_on_thread', lambda x: x).start()
21
        # pylint: disable=import-outside-toplevel
22
        from napps.kytos.flow_manager.storehouse import StoreHouse
23
        self.addCleanup(patch.stopall)
24
25
        self.napp = StoreHouse(get_controller_mock())
26
27
    @patch('napps.kytos.flow_manager.storehouse.StoreHouse.get_stored_box')
28
    def test_get_data(self, mock_get_stored_box):
29
        """Test get_data."""
30
        mock_box = MagicMock()
31
        box_data = MagicMock()
32
        mock_get_stored_box.return_value = True
33
        type(box_data).data = PropertyMock(side_effect=[{}, "box"])
34
        type(mock_box).data = PropertyMock(return_value=box_data)
35
        self.napp.box = mock_box
36
        response = self.napp.get_data()
37
        self.assertEqual(response.data, {})
38
39
        response = self.napp.get_data()
40
        self.assertEqual(response.data, "box")
41
42
    @patch('napps.kytos.flow_manager.storehouse.KytosEvent')
43
    @patch('kytos.core.buffers.KytosEventBuffer.put')
44
    def test_create_box(self, *args):
45
        """Test create_box."""
46
        (mock_buffers_put, mock_event) = args
47
        self.napp.create_box()
48
        mock_event.assert_called()
49
        mock_buffers_put.assert_called()
50
51
    # pylint: disable = protected-access
52
    @patch('napps.kytos.flow_manager.storehouse.StoreHouse.get_stored_box')
53
    @patch('napps.kytos.flow_manager.storehouse.StoreHouse.create_box')
54
    def test_get_or_create_a_box_from_list_of_boxes(self, *args):
55
        """Test create_box."""
56
        (mock_create_box, mock_get_stored_box) = args
57
        mock_event = MagicMock()
58
        mock_data = MagicMock()
59
        mock_error = MagicMock()
60
        self.napp._get_or_create_a_box_from_list_of_boxes(mock_event,
61
                                                          mock_data,
62
                                                          mock_error)
63
        mock_get_stored_box.assert_called()
64
        self.napp._get_or_create_a_box_from_list_of_boxes(mock_event,
65
                                                          None,
66
                                                          mock_error)
67
        mock_create_box.assert_called()
68
69
    @patch('napps.kytos.flow_manager.storehouse.KytosEvent')
70
    @patch('kytos.core.buffers.KytosEventBuffer.put')
71
    def test_get_stored_box(self, *args):
72
        """Test get_stored_box."""
73
        (mock_buffers_put, mock_event) = args
74
        mock_box = MagicMock()
75
        self.napp.get_stored_box(mock_box)
76
        mock_event.assert_called()
77
        mock_buffers_put.assert_called()
78
79
    @patch('napps.kytos.flow_manager.storehouse.KytosEvent')
80
    @patch('kytos.core.buffers.KytosEventBuffer.put')
81
    def test_save_flow(self, *args):
82
        """Test save_status."""
83
        (mock_buffers_put, mock_event) = args
84
        mock_status = MagicMock()
85
        self.napp.save_flow(mock_status)
86
        mock_event.assert_called()
87
        mock_buffers_put.assert_called()
88