build.tests.unit.test_controller   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 103
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMaintenanceController.test_bootstrap_indexes() 0 10 1
A TestMaintenanceController.test_update_window() 0 12 1
A TestMaintenanceController.test_get_window_1() 0 6 1
A TestMaintenanceController.test_insert_window() 0 11 1
A TestMaintenanceController.setup_method() 0 26 1
A TestMaintenanceController.test_get_window_2() 0 6 1
A TestMaintenanceController.test_get_windows() 0 6 1
1
"""Module to test MaintenanceController"""
2
3 1
from unittest.mock import MagicMock, patch, call
4
5 1
from datetime import datetime, timedelta
6 1
import pytz
7
8 1
from napps.kytos.maintenance.controllers import MaintenanceController
9 1
from napps.kytos.maintenance.models import MaintenanceWindow, MaintenanceWindows
10
11 1
class TestMaintenanceController:
12
    """Test the MaintenanceController Class"""
13
14 1
    def setup_method(self) -> None:
15 1
        self.controller = MaintenanceController(MagicMock())
16 1
        self.now = datetime.now(pytz.utc)
17 1
        self.window_dict = {
18
            'id': 'Test Window',
19
            'description': '',
20
            'start': self.now + timedelta(hours=1),
21
            'end': self.now + timedelta(hours=2),
22
            'status': 'pending',
23
            'switches': [],
24
            'interfaces': [],
25
            'links': [],
26
            'updated_at': self.now - timedelta(days=1),
27
            'inserted_at': self.now - timedelta(days=1),
28
        }
29 1
        self.window = MaintenanceWindow.model_construct(
30
            id = 'Test Window',
31
            description = '',
32
            start = self.now + timedelta(hours=1),
33
            end = self.now + timedelta(hours=2),
34
            status = 'pending',
35
            switches = [],
36
            interfaces = [],
37
            links = [],
38
            updated_at = self.now - timedelta(days=1),
39
            inserted_at = self.now - timedelta(days=1),
40
        )
41
42 1
    def test_bootstrap_indexes(self) -> None:
43
        """Check that the proper indexes were bootstrapped"""
44 1
        self.controller.bootstrap_indexes()
45 1
        windows = self.controller.windows
46 1
        expected_indexes = [
47
            call("maintenance.windows", [("id", 1)], unique=True),
48
        ]
49 1
        mock = self.controller.mongo.bootstrap_index
50 1
        indexes = mock.call_args_list
51 1
        assert indexes == expected_indexes
52
53 1
    @patch('napps.kytos.maintenance.controllers.datetime')
54 1
    def test_insert_window(self, dt_class):
55
        """Test inserting a window."""
56 1
        now_func = dt_class.now
57 1
        now_func.return_value = self.now
58 1
        self.controller.insert_window(self.window)
59 1
        self.controller.windows.insert_one.assert_called_once_with(
60
            {
61
                **self.window_dict,
62
                'inserted_at': self.now,
63
                'updated_at': self.now,
64
            }
65
        )
66
67 1
    def test_update_window(self):
68
        """Test updating a window."""
69 1
        self.controller.update_window(self.window)
70 1
        dict_copy = self.window_dict.copy()
71 1
        del dict_copy['inserted_at']
72 1
        del dict_copy['updated_at']
73 1
        self.controller.windows.update_one.assert_called_once_with(
74
            {'id': self.window.id},
75
            [{
76
                '$set': {
77
                    **dict_copy,
78
                    'updated_at': '$$NOW',
79
                },
80
            }],
81
        )
82
83 1
    def test_get_window_1(self):
84
        """Test getting a window that exists."""
85 1
        mw_id = 'Test Window'
86 1
        self.controller.windows.find_one.return_value = self.window_dict
87 1
        result = self.controller.get_window(mw_id)
88 1
        assert result == self.window
89
90 1
    def test_get_window_2(self):
91
        """Test getting a window that does not exist."""
92 1
        mw_id = 'Test Window'
93 1
        self.controller.windows.find_one.return_value = None
94 1
        result = self.controller.get_window(mw_id)
95 1
        assert result == None
96
97 1
    def test_get_windows(self):
98
        """Test getting the set of windows."""
99 1
        self.controller.windows.find.return_value = [self.window_dict]
100 1
        expected = MaintenanceWindows.model_construct(root = [self.window])
101 1
        result = self.controller.get_windows()
102
        assert result == expected
103