Passed
Pull Request — master (#67)
by
unknown
02:05
created

build.tests.helpers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 73
rs 10
c 0
b 0
f 0
wmc 8

6 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 tags() 0 17 3
A get_controller_mock() 0 6 1
A get_interface_mock() 0 6 1
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
def tags(*args, **kwargs):
57
    """Handle tokens from requests."""
58
    test_type = kwargs.get('type') or 'unit'
59
    test_size = kwargs.get('size') or 'small'
60
    env_test_size = os.environ.get("KYTOS_TESTS_SIZE", 'small')
61
    env_test_type = os.environ.get("KYTOS_TESTS_TYPE", 'unit')
62
63
    def inner_func(func):
64
65
        @wraps(func)
66
        def wrapper(*args, **kwargs):
67
            if test_type == env_test_type and test_size == env_test_size:
68
                return func(*args, **kwargs)
69
            return None
70
        return wrapper
71
72
    return inner_func
73