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

build.tests.unit.test_search_results   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

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

9 Methods

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