Completed
Pull Request — master (#54)
by Gleyberson
02:02
created

TestGraph.test_update_links()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nop 2
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
"""Test Graph methods."""
2
from unittest import TestCase
3
from unittest.mock import MagicMock, call, patch
4
5
from napps.kytos.pathfinder.graph import KytosGraph
6
from tests.helpers import get_topology_mock
7
8
9
class TestGraph(TestCase):
10
    """Tests for the Main class."""
11
12
    @patch('networkx.Graph')
13
    def setUp(self, mock_graph):
14
        """Execute steps before each tests."""
15
        self.mock_graph = mock_graph.return_value
16
        self.kytos_graph = KytosGraph()
17
18
    def test_clear(self):
19
        """Test clear."""
20
        self.kytos_graph.clear()
21
22
        self.mock_graph.clear.assert_called()
23
24
    @patch('napps.kytos.pathfinder.graph.KytosGraph.update_links')
25
    @patch('napps.kytos.pathfinder.graph.KytosGraph.update_nodes')
26
    def test_update_topology(self, *args):
27
        """Test update topology."""
28
        (mock_update_nodes, mock_update_links) = args
29
        topology = get_topology_mock()
30
        self.kytos_graph.update_topology(topology)
31
32
        self.mock_graph.clear.assert_called()
33
        mock_update_nodes.assert_called_with(topology.switches)
34
        mock_update_links.assert_called_with(topology.links)
35
36
    def test_update_nodes(self):
37
        """Test update nodes."""
38
        topology = get_topology_mock()
39
        self.kytos_graph.update_nodes(topology.switches)
40
        switch = topology.switches["00:00:00:00:00:00:00:01"]
41
42
        calls = [call(switch.id)]
43
        calls += [call(interface.id)
44
                  for interface in switch.interfaces.values()]
45
        self.mock_graph.add_node.assert_has_calls(calls)
46
47
        calls = [call(switch.id, interface.id)
48
                 for interface in switch.interfaces.values()]
49
        self.mock_graph.add_edge.assert_has_calls(calls)
50
51
    @patch('napps.kytos.pathfinder.graph.KytosGraph._set_default_metadata')
52
    def test_update_links(self, mock_set_default_metadata):
53
        """Test update nodes."""
54
        topology = get_topology_mock()
55
        self.kytos_graph.update_links(topology.links)
56
57
        keys = []
58
        all_metadata = [link.metadata for link in topology.links.values()]
59
        for metadata in all_metadata:
60
            keys.extend(key for key in metadata.keys())
61
        mock_set_default_metadata.assert_called_with(keys)
62
63
    def test_remove_switch_hops(self):
64
        """Test remove switch hops."""
65
        circuit = {"hops": ["00:00:00:00:00:00:00:01:1",
66
                            "00:00:00:00:00:00:00:01",
67
                            "00:00:00:00:00:00:00:01:2"]}
68
69
        self.kytos_graph._remove_switch_hops(circuit)
70
71
        expected_circuit = {"hops": ["00:00:00:00:00:00:00:01:1",
72
                                     "00:00:00:00:00:00:00:01:2"]}
73
        self.assertEqual(circuit, expected_circuit)
74
75
    @patch('networkx.shortest_simple_paths', return_value=["any"])
76
    def test_shortest_paths(self, mock_shortest_simple_paths):
77
        """Test shortest paths."""
78
        source, dest = "00:00:00:00:00:00:00:01:1", "00:00:00:00:00:00:00:02:2"
79
        shortest_paths = self.kytos_graph.shortest_paths(source, dest)
80
81
        mock_shortest_simple_paths.assert_called_with(self.kytos_graph.graph,
82
                                                      source, dest, None)
83
        self.assertEqual(shortest_paths, ["any"])
84