Test Failed
Push — master ( 0d7af6...292dca )
by Beraldo
04:07 queued 01:46
created

build.tests.models.test_path   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 65
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A TestPath.test_status_case_1() 0 4 1
A TestPath.test_status_case_4() 0 9 1
A TestPath.test_compare_different_paths() 0 18 1
A TestPath._mocked_requests_get_status_case_2() 0 2 1
A TestPath.test_as_dict() 0 10 1
A TestPath.test_compare_same_paths() 0 12 1
A TestPath.test_status_case_2() 0 9 1
A TestPath.test_status_case_3() 0 5 1
A TestPath._mocked_requests_get_status_case_4() 0 2 1
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
from tests.helpers import MockResponse
8
9
# pylint: disable=wrong-import-position
10
sys.path.insert(0, '/var/lib/kytos/napps/..')
11
# pylint: enable=wrong-import-position
12
13
from napps.kytos.mef_eline.models import Path  # NOQA
14
from tests.helpers import get_link_mocked  # NOQA
15
16
17
18
class TestPath(TestCase):
19
    """"Class to test path methods."""
20
    def test_status_case_1(self):
21
        """Test if empty link is DISABLED."""
22
        current_path = Path()
23
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
24
25
    # This method will be used by the mock to replace requests.get
26
    def _mocked_requests_get_status_case_2(*args, **kwargs):
27
        return MockResponse({}, 200)
28
29
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_2)
30
    def test_status_case_2(self, requests_mocked):
31
        """Test if link status is DOWN."""
32
        links = [
33
                 get_link_mocked(),
34
                 get_link_mocked()
35
        ]
36
        current_path = Path(links)
37
        self.assertEqual(current_path.status, EntityStatus.DOWN)
38
39
    def test_status_case_3(self):
40
        """Test if link status is DISABLED."""
41
        links = []
42
        current_path = Path(links)
43
        self.assertEqual(current_path.status, EntityStatus.DISABLED)
44
45
    # This method will be used by the mock to replace requests.get
46
    def _mocked_requests_get_status_case_4(*args, **kwargs):
47
        return MockResponse({'key':'OK'}, 200)
48
49
    @patch('requests.get', side_effect=_mocked_requests_get_status_case_4)
50
    def test_status_case_4(self, requests_mocked):
51
        """Test if link status is UP."""
52
        links = [
53
                 get_link_mocked(),
54
                 get_link_mocked()
55
        ]
56
        current_path = Path(links)
57
        self.assertEqual(current_path.status, EntityStatus.UP)
58
59
    def test_compare_same_paths(self):
60
        """Test compare paths with same links."""
61
        links = [
62
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
63
                                metadata={"s_vlan": 5}),
64
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
65
                                metadata={"s_vlan": 6})
66
            ]
67
68
        path_1 = Path(links)
69
        path_2 = Path(links)
70
        self.assertEqual(path_1, path_2)
71
72
    def test_compare_different_paths(self):
73
        """Test compare paths with different links."""
74
        links_1 = [
75
                get_link_mocked(endpoint_a_port=9, endpoint_b_port=10,
76
                                metadata={"s_vlan": 5}),
77
                get_link_mocked(endpoint_a_port=11, endpoint_b_port=12,
78
                                metadata={"s_vlan": 6})
79
            ]
80
        links_2 = [
81
                get_link_mocked(endpoint_a_port=12, endpoint_b_port=11,
82
                                metadata={"s_vlan": 5}),
83
                get_link_mocked(endpoint_a_port=14, endpoint_b_port=16,
84
                                metadata={"s_vlan": 11})
85
            ]
86
87
        path_1 = Path(links_1)
88
        path_2 = Path(links_2)
89
        self.assertNotEqual(path_1, path_2)
90
91
    def test_as_dict(self):
92
        """Test path as dict."""
93
        links = [
94
                get_link_mocked(link_dict={"id": 3}),
95
                get_link_mocked(link_dict={"id": 2})
96
            ]
97
98
        current_path = Path(links)
99
        expected_dict = [{"id": 3}, {"id": 2}]
100
        self.assertEqual(expected_dict, current_path.as_dict())
101
102
103