Test Failed
Push — master ( 437722...e67027 )
by Antonio
03:54 queued 11s
created

TestPath.test_status_case_6()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 2
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
1
"""Module to test the Path class."""
2
import sys
3
from unittest import TestCase
4
from unittest.mock import patch
5
6
from kytos.core.common import EntityStatus
7
8
# pylint: disable=wrong-import-position
9
sys.path.insert(0, '/var/lib/kytos/napps/..')
10
# pylint: enable=wrong-import-position
11
from napps.kytos.mef_eline.models import Path  # NOQA pycodestyle
12
from tests.helpers import MockResponse, get_link_mocked  # NOQA pycodestyle
13
14
15
class TestPath(TestCase):
16
    """Class to test path methods."""
17
18
    def test_status_case_1(self):
19
        """Test if empty link is DISABLED."""
20
        current_path = Path()
21
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
22
23
    # This method will be used by the mock to replace requests.get
24
    def _mocked_requests_get_status_case_2(self):
25
        # pylint: disable=no-self-use
26
        return MockResponse({'links': {'abc': {'active': False,
27
                                               'enabled': True},
28
                                       'def': {'active': True,
29
                                               'enabled': True}}}, 200)
30
31
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_2)
32
    def test_status_case_2(self, requests_mocked):
33
        # pylint: disable=unused-argument
34
        """Test if link status is DOWN."""
35
        link1 = get_link_mocked()
36
        link2 = get_link_mocked()
37
        link1.id = 'def'
38
        link2.id = 'abc'
39
        links = [link1, link2]
40
        current_path = Path(links)
41
        self.assertEqual(current_path.status, EntityStatus.DOWN)
42
43
    def test_status_case_3(self):
44
        """Test if link status is DISABLED."""
45
        links = []
46
        current_path = Path(links)
47
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
48
49
    # This method will be used by the mock to replace requests.get
50
    def _mocked_requests_get_status_case_4(self):
51
        # pylint: disable=no-self-use
52
        return MockResponse({'links': {'abc': {'active': True,
53
                                               'enabled': True},
54
                                       'def': {'active': True,
55
                                               'enabled': True}}}, 200)
56
57
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_4)
58
    def test_status_case_4(self, requests_mocked):
59
        # pylint: disable=unused-argument
60
        """Test if link status is UP."""
61
        link1 = get_link_mocked()
62
        link2 = get_link_mocked()
63
        link1.id = 'def'
64
        link2.id = 'abc'
65
        links = [link1, link2]
66
        current_path = Path(links)
67
        self.assertEqual(current_path.status, EntityStatus.UP)
68
69
    # This method will be used by the mock to replace requests.get
70
    def _mocked_requests_get_status_case_5(self):
71
        # pylint: disable=no-self-use
72
        return MockResponse({'links': {'abc': {'active': True,
73
                                               'enabled': True},
74
                                       'def': {'active': False,
75
                                               'enabled': False}}}, 200)
76
77
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_5)
78
    def test_status_case_5(self, requests_mocked):
79
        # pylint: disable=unused-argument
80
        """Test if link status is UP."""
81
        link1 = get_link_mocked()
82
        link2 = get_link_mocked()
83
        link1.id = 'def'
84
        link2.id = 'abc'
85
        links = [link1, link2]
86
        current_path = Path(links)
87
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
88
89
    # This method will be used by the mock to replace requests.get
90
    def _mocked_requests_get_status_case_6(self):
91
        # pylint: disable=no-self-use
92
        return MockResponse({'links': {'abc': {'active': False,
93
                                               'enabled': False},
94
                                       'def': {'active': False,
95
                                               'enabled': True}}}, 200)
96
97
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_6)
98
    def test_status_case_6(self, requests_mocked):
99
        # pylint: disable=unused-argument
100
        """Test if link status is UP."""
101
        link1 = get_link_mocked()
102
        link2 = get_link_mocked()
103
        link1.id = 'def'
104
        link2.id = 'abc'
105
        links = [link1, link2]
106
        current_path = Path(links)
107
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
108
109
    def test_compare_same_paths(self):
110
        """Test compare paths with same links."""
111
        links = [
112
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
113
                                metadata={"s_vlan": 5}),
114
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
115
                                metadata={"s_vlan": 6})
116
            ]
117
118
        path_1 = Path(links)
119
        path_2 = Path(links)
120
        self.assertEqual(path_1, path_2)
121
122
    def test_compare_different_paths(self):
123
        """Test compare paths with different links."""
124
        links_1 = [
125
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
126
                                metadata={"s_vlan": 5}),
127
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
128
                                metadata={"s_vlan": 6})
129
            ]
130
        links_2 = [
131
                get_link_mocked(endpoint_a_port=12, endpoint_b_port=11,
132
                                metadata={"s_vlan": 5}),
133
                get_link_mocked(endpoint_a_port=14, endpoint_b_port=16,
134
                                metadata={"s_vlan": 11})
135
            ]
136
137
        path_1 = Path(links_1)
138
        path_2 = Path(links_2)
139
        self.assertNotEqual(path_1, path_2)
140
141
    def test_as_dict(self):
142
        """Test path as dict."""
143
        links = [
144
                get_link_mocked(link_dict={"id": 3}),
145
                get_link_mocked(link_dict={"id": 2})
146
            ]
147
148
        current_path = Path(links)
149
        expected_dict = [{"id": 3}, {"id": 2}]
150
        self.assertEqual(expected_dict, current_path.as_dict())
151