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

build.tests.helpers   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 40.91 %

Importance

Changes 0
Metric Value
wmc 14
eloc 71
dl 45
loc 110
rs 10
c 0
b 0
f 0

8 Functions

Rating   Name   Duplication   Size   Complexity  
A get_connection_mock() 0 8 1
A unit() 15 15 3
A e2e() 15 15 3
A integration() 15 15 3
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

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 pyof.utils import unpack
7
from kytos.core import Controller
8
from kytos.core.config import KytosConfig
9
from kytos.core.connection import Connection, ConnectionState
10
from kytos.core.interface import Interface
11
from kytos.core.switch import Switch
12
13
14
def get_controller_mock():
15
    """Return a controller mock."""
16
    options = KytosConfig().options['daemon']
17
    controller = Controller(options)
18
    controller.log = Mock()
19
    return controller
20
21
22
def get_interface_mock(interface_name, port, *args, **kwargs):
23
    """Return a interface mock."""
24
    switch = get_switch_mock(0x04)
25
    switch.connection = Mock()
26
    iface = Interface(interface_name, port, switch, *args, **kwargs)
27
    return iface
28
29
30
def get_switch_mock(dpid="00:00:00:00:00:00:00:01"):
31
    """Return a switch mock."""
32
    return Switch(dpid)
33
34
35
def get_connection_mock(of_version, target_switch, state=ConnectionState.NEW):
36
    """Return a connection mock."""
37
    connection = Connection(Mock(), Mock(), Mock())
38
    connection.switch = target_switch
39
    connection.state = state
40
    connection.protocol.version = of_version
41
    connection.protocol.unpack = unpack
42
    return connection
43
44
45
def get_kytos_event_mock(**kwargs):
46
    """Return a kytos event mock."""
47
    destination = kwargs.get('destination')
48
    message = kwargs.get('message')
49
    source = kwargs.get('source')
50
51
    event = MagicMock()
52
    event.source = source
53
    event.content = {'destination': destination,
54
                     'message': message}
55
    return event
56
57
58
# pylint: disable=unused-argument
59 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...
60
    """Handle tokens from requests."""
61
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
62
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
63
64
    def inner_func(func):
65
66
        @wraps(func)
67
        def wrapper(*args, **kwargs):
68
            if env_test_type == 'unit' and size == env_test_size:
69
                return func(*args, **kwargs)
70
            return None
71
        return wrapper
72
73
    return inner_func
74
75
76
# pylint: disable=unused-argument
77 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...
78
    """Handle tokens from requests."""
79
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
80
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
81
82
    def inner_func(func):
83
84
        @wraps(func)
85
        def wrapper(*args, **kwargs):
86
            if env_test_type == 'integration' and size == env_test_size:
87
                return func(*args, **kwargs)
88
            return None
89
        return wrapper
90
91
    return inner_func
92
93
94
# pylint: disable=unused-argument
95 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...
96
    """Handle tokens from requests."""
97
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
98
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
99
100
    def inner_func(func):
101
102
        @wraps(func)
103
        def wrapper(*args, **kwargs):
104
            if env_test_type == 'e2e' and size == env_test_size:
105
                return func(*args, **kwargs)
106
            return None
107
        return wrapper
108
109
    return inner_func
110