Completed
Pull Request — master (#58)
by
unknown
02:30
created

build.tests.unit.test_results   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TestResults.test_setup() 0 3 1
A TestResults.generate_topology() 0 6 1
A TestResults.get_path() 0 4 1
A TestResults.get_path_constrained() 0 6 1
A TestResults.create_link() 0 7 1
A TestResults.create_switch() 0 4 1
A TestResults.add_interfaces() 0 8 2
A TestResults.setup() 0 8 1
A TestResults.add_metadata_to_link() 0 5 1
1
"""Module to test the KytosGraph in graph.py."""
2
from unittest import TestCase
3
4
import networkx as nx
5
from kytos.core.interface import Interface
6
from kytos.core.link import Link
7
from kytos.core.switch import Switch
8
9
# module under test
10
from graph import KytosGraph
11
12
13
class TestResults(TestCase):
14
    """Tests for the graph class."""
15
16
    def setup(self):
17
        """Setup for most tests"""
18
        switches, links = self.generate_topology()
19
        self.graph = KytosGraph()
20
        self.graph.clear()
21
        self.graph.update_nodes(switches)
22
        self.graph.update_links(links)
23
        self.graph.set_path_function(nx.shortest_simple_paths)
24
25
    def get_path(self, source, destination):
26
        """Return the shortest path"""
27
        results = self.graph.shortest_paths(source, destination)
28
        return results
29
30
    def get_path_constrained(self, source, destination, maximum_misses=None,
31
                             **metrics):
32
        """Return the constrained shortest path"""
33
        return self.graph.constrained_flexible_paths(source, destination,
34
                                                     maximum_misses,
35
                                                     **metrics)
36
37
    def test_setup(self):
38
        """Provides information on default test setup"""
39
        self.setup()
40
41
    @ staticmethod
42
    def generate_topology():
43
        """Generates a predetermined topology"""
44
        switches = {}
45
        links = {}
46
        return (switches, links)
47
48
    @ staticmethod
49
    def create_switch(name, switches):
50
        '''Add a new switch to the list of switches'''
51
        switches[name] = Switch(name)
52
53
    @ staticmethod
54
    def add_interfaces(count, switch, interfaces):
55
        '''Add a new interface to the list of interfaces'''
56
        for i in range(1, count + 1):
57
            str1 = "{}:{}".format(switch.dpid, i)
58
            interface = Interface(str1, i, switch)
59
            interfaces[str1] = interface
60
            switch.update_interface(interface)
61
62
    @ staticmethod
63
    def create_link(interface_a, interface_b, interfaces, links):
64
        '''Add a new link between two interfaces into the list of links'''
65
        compounded = "{}|{}".format(interface_a, interface_b)
66
        final_name = compounded
67
        links[final_name] = Link(
68
            interfaces[interface_a], interfaces[interface_b])
69
70
    @ staticmethod
71
    def add_metadata_to_link(interface_a, interface_b, metrics, links):
72
        '''Add metadata to an existing link in the list of links'''
73
        compounded = "{}|{}".format(interface_a, interface_b)
74
        links[compounded].extend_metadata(metrics)
75