Completed
Pull Request — master (#69)
by Gleyberson
01:58
created

build.tests.helpers   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 42.06 %

Importance

Changes 0
Metric Value
wmc 14
eloc 68
dl 45
loc 107
rs 10
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A get_connection_mock() 0 6 1
A get_kytos_event_mock() 0 11 1
A get_switch_mock() 0 3 1
A get_controller_mock() 0 6 1
A get_interface_mock() 0 6 1
A unit() 15 15 3
A e2e() 15 15 3
A integration() 15 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
"""Module to help to create tests."""
2
import os
3
from functools import wraps
4
from unittest.mock import MagicMock, Mock
5
6
from kytos.core import Controller
7
from kytos.core.config import KytosConfig
8
from kytos.core.connection import Connection
9
from kytos.core.interface import Interface
10
from kytos.core.switch import Switch
11
12
13
def get_controller_mock():
14
    """Return a controller mock."""
15
    options = KytosConfig().options['daemon']
16
    controller = Controller(options)
17
    controller.log = Mock()
18
    return controller
19
20
21
def get_interface_mock(interface_name, port, *args, **kwargs):
22
    """Return a interface mock."""
23
    switch = get_switch_mock(0x04)
24
    switch.connection = Mock()
25
    iface = Interface(interface_name, port, switch, *args, **kwargs)
26
    return iface
27
28
29
def get_switch_mock(dpid):
30
    """Return a switch mock."""
31
    return Switch(dpid)
32
33
34
def get_connection_mock(of_version, target_switch):
35
    """Return a connection mock."""
36
    connection = Connection(Mock(), Mock(), Mock())
37
    connection.switch = target_switch
38
    connection.protocol.version = of_version
39
    return connection
40
41
42
def get_kytos_event_mock(**kwargs):
43
    """Return a kytos event mock."""
44
    destination = kwargs.get('destination')
45
    message = kwargs.get('message')
46
    source = kwargs.get('source')
47
48
    event = MagicMock()
49
    event.source = source
50
    event.content = {'destination': destination,
51
                     'message': message}
52
    return event
53
54
55
# pylint: disable=unused-argument
56 View Code Duplication
def unit(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
57
    """Handle tokens from requests."""
58
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
59
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
60
61
    def inner_func(func):
62
63
        @wraps(func)
64
        def wrapper(*args, **kwargs):
65
            if env_test_type == 'unit' and size == env_test_size:
66
                return func(*args, **kwargs)
67
            return None
68
        return wrapper
69
70
    return inner_func
71
72
73
# pylint: disable=unused-argument
74 View Code Duplication
def integration(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
75
    """Handle tokens from requests."""
76
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
77
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
78
79
    def inner_func(func):
80
81
        @wraps(func)
82
        def wrapper(*args, **kwargs):
83
            if env_test_type == 'integration' and size == env_test_size:
84
                return func(*args, **kwargs)
85
            return None
86
        return wrapper
87
88
    return inner_func
89
90
91
# pylint: disable=unused-argument
92 View Code Duplication
def e2e(size='small'):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
93
    """Handle tokens from requests."""
94
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
95
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
96
97
    def inner_func(func):
98
99
        @wraps(func)
100
        def wrapper(*args, **kwargs):
101
            if env_test_type == 'e2e' and size == env_test_size:
102
                return func(*args, **kwargs)
103
            return None
104
        return wrapper
105
106
    return inner_func
107