Passed
Pull Request — master (#58)
by
unknown
02:11
created

get_topology_with_metadata_mock()   B

Complexity

Conditions 5

Size

Total Lines 79
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 71
nop 0
dl 0
loc 79
rs 7.4824
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""Module to help to create tests."""
2
from unittest.mock import MagicMock
3
4
from kytos.lib.helpers import (get_interface_mock, get_link_mock,
5
                               get_switch_mock)
6
7
8
def get_topology_mock():
9
    """Create a default topology."""
10
    switch_a = get_switch_mock("00:00:00:00:00:00:00:01", 0x04)
11
    switch_b = get_switch_mock("00:00:00:00:00:00:00:02", 0x04)
12
    switch_c = get_switch_mock("00:00:00:00:00:00:00:03", 0x01)
13
14
    interface_a1 = get_interface_mock("s1-eth1", 1, switch_a)
15
    interface_a2 = get_interface_mock("s1-eth2", 2, switch_a)
16
17
    interface_b1 = get_interface_mock("s2-eth1", 1, switch_b)
18
    interface_b2 = get_interface_mock("s2-eth2", 2, switch_b)
19
20
    interface_c1 = get_interface_mock("s3-eth1", 1, switch_c)
21
    interface_c2 = get_interface_mock("s3-eth2", 2, switch_c)
22
23
    switch_a.interfaces = {interface_a1.id: interface_a1,
24
                           interface_a2.id: interface_a2}
25
    switch_b.interfaces = {interface_b1.id: interface_b1,
26
                           interface_b2.id: interface_b2}
27
    switch_c.interfaces = {interface_c1.id: interface_c1,
28
                           interface_c2.id: interface_c2}
29
30
    link_1 = get_link_mock(interface_a1, interface_b1)
31
    link_2 = get_link_mock(interface_a2, interface_c1)
32
    link_3 = get_link_mock(interface_b2, interface_c2)
33
34
    topology = MagicMock()
35
    topology.links = {"1": link_1, "2": link_2, "3": link_3}
36
    topology.switches = {switch_a.dpid: switch_a,
37
                         switch_b.dpid: switch_b,
38
                         switch_c.dpid: switch_c}
39
    return topology
40
41
42
def get_topology_with_metadata_mock():
43
    """Create a topology with metadata."""
44
    switches = {}
45
    interfaces = {}
46
    links = {}
47
    i = 0
48
49
    switches_to_interface_counts = {"S1": 2, "S2": 2, "S3": 6, "S4": 2,
50
                                    "S5": 6, "S6": 5, "S7": 2, "S8": 8,
51
                                    "S9": 4, "S10": 3, "S11": 3,
52
                                    "User1": 4, "User2": 2,
53
                                    "User3": 2, "User4": 3}
54
55
    links_to_interfaces = [["S1:1", "S2:1"], ["S1:2", "User1:1"],
56
                           ["S2:2", "User4:1"], ["S3:1", "S5:1"],
57
                           ["S3:2", "S7:1"], ["S3:3", "S8:1"],
58
                           ["S3:4", "S11:1"],
59
                           ["S3:5", "User3:1"], ["S3:6", "User4:2"],
60
                           ["S4:1", "S5:2"], ["S4:2", "User1:2"],
61
                           ["S5:3", "S6:1"],
62
                           ["S5:4", "S6:2"], ["S5:5", "S8:2"],
63
                           ["S5:6", "User1:3"], ["S6:3", "S9:1"],
64
                           ["S6:4", "S9:2"], ["S6:5", "S10:1"],
65
                           ["S7:2", "S8:3"],
66
                           ["S8:4", "S9:3"], ["S8:5", "S9:4"],
67
                           ["S8:6", "S10:2"],
68
                           ["S8:7", "S11:2"], ["S8:8", "User3:2"],
69
                           ["S10:3", "User2:1"], ["S11:3", "User2:2"],
70
                           ["User1:4", "User4:3"]]
71
72
    links_to_metadata = [
73
        {"reliability": 5, "bandwidth": 100, "delay": 105},
74
        {"reliability": 5, "bandwidth": 100, "delay": 1},
75
        {"reliability": 5, "bandwidth": 100, "delay": 10},
76
        {"reliability": 5, "bandwidth": 10, "delay": 112},
77
        {"reliability": 5, "bandwidth": 100, "delay": 1},
78
        {"reliability": 5, "bandwidth": 100, "delay": 1},
79
        {"reliability": 3, "bandwidth": 100, "delay": 6},
80
        {"reliability": 5, "bandwidth": 100, "delay": 1},
81
        {"reliability": 5, "bandwidth": 100, "delay": 10},
82
        {"reliability": 1, "bandwidth": 100, "delay": 30, "ownership": "A"},
83
        {"reliability": 3, "bandwidth": 100, "delay": 110, "ownership": "A"},
84
        {"reliability": 1, "bandwidth": 100, "delay": 40},
85
        {"reliability": 3, "bandwidth": 100, "delay": 40, "ownership": "A"},
86
        {"reliability": 5, "bandwidth": 100, "delay": 112},
87
        {"reliability": 3, "bandwidth": 100, "delay": 60},
88
        {"reliability": 3, "bandwidth": 100, "delay": 60},
89
        {"reliability": 5, "bandwidth": 100, "delay": 62},
90
        {"bandwidth": 100, "delay": 108, "ownership": "A"},
91
        {"reliability": 5, "bandwidth": 100, "delay": 1},
92
        {"reliability": 3, "bandwidth": 100, "delay": 32},
93
        {"reliability": 3, "bandwidth": 100, "delay": 110},
94
        {"reliability": 5, "bandwidth": 100, "ownership": "A"},
95
        {"reliability": 3, "bandwidth": 100, "delay": 7},
96
        {"reliability": 5, "bandwidth": 100, "delay": 1},
97
        {"reliability": 3, "bandwidth": 100, "delay": 10, "ownership": "A"},
98
        {"reliability": 3, "bandwidth": 100, "delay": 6},
99
        {"reliability": 5, "bandwidth": 10, "delay": 105}]
100
101
    for switch in switches_to_interface_counts:
102
        switches[switch] = get_switch_mock(switch)
103
104
    for key, value in switches_to_interface_counts.items():
105
        switches[key].interfaces = {}
106
        for interface in _get_interfaces(value, switches[key]):
107
            switches[key].interfaces[interface.id] = interface
108
            interfaces[interface.id] = interface
109
110
    for interfaces_str in links_to_interfaces:
111
        interface_a = interfaces[interfaces_str[0]]
112
        interface_b = interfaces[interfaces_str[1]]
113
        links[str(i)] = get_link_mock(interface_a, interface_b)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
114
        links[str(i)].metadata = links_to_metadata[i]
115
        i += 1
116
117
    topology = MagicMock()
118
    topology.links = links
119
    topology.switches = switches
120
    return topology
121
122
123
def _get_interfaces(count, switch):
124
    """Add a new interface to the list of interfaces."""
125
    for i in range(1, count + 1):
126
        yield get_interface_mock("", i, switch)
127
128
129
def get_filter_links_fake(links, metadata=True, **metrics):
130
    """Get test links with optional metadata."""
131
    # pylint: disable=unused-argument
132
    filtered_links = ["a", "b", "c"]
133
    filtered_links_without_metadata = filtered_links[0:2]
134
    if not metadata:
135
        return filtered_links_without_metadata
136
    return filtered_links
137
138
# pylint: enable=unused-argument
139
140
141
def get_test_filter_function():
142
    """Get minimum filter function."""
143
    return lambda x: (lambda y: y >= x)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable x does not seem to be defined.
Loading history...
144