Completed
Push — master ( 8945f9...62acd1 )
by Juan José
22s queued 14s
created

VtsTestCase.test_add_vt_with_empty_id()   A

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
# Copyright (C) 2020 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
from unittest import TestCase
20
21
from ospd.errors import OspdError
22
from ospd.vts import Vts
23
24
25
class VtsTestCase(TestCase):
26
    def test_add_vt(self):
27
        vts = Vts()
28
29
        vts.add('id_1', name='foo')
30
31
        self.assertEqual(len(vts.vts), 1)
32
33
    def test_add_duplicate_vt(self):
34
        vts = Vts()
35
36
        vts.add('id_1', name='foo')
37
38
        with self.assertRaises(OspdError):
39
            vts.add('id_1', name='bar')
40
41
        self.assertEqual(len(vts.vts), 1)
42
43
    def test_add_vt_with_empty_id(self):
44
        vts = Vts()
45
46
        with self.assertRaises(OspdError):
47
            vts.add(None, name='foo')
48
49
        with self.assertRaises(OspdError):
50
            vts.add('', name='foo')
51
52
    def test_add_vt_with_invalid_id(self):
53
        vts = Vts()
54
55
        with self.assertRaises(OspdError):
56
            vts.add('$$$_1', name='foo')
57
58
        self.assertEqual(len(vts.vts), 0)
59
60
    def test_contains(self):
61
        vts = Vts()
62
63
        vts.add('id_1', name='foo')
64
65
        self.assertIn('id_1', vts)
66
67
    def test_get(self):
68
        vts = Vts()
69
70
        vts.add('id_1', name='foo')
71
        vt = vts.get('id_1')
72
73
        self.assertIsNotNone(vt)
74
        self.assertEqual(vt['name'], 'foo')
75
76
        self.assertIsNone(vt.get('bar'))
77
78
    def test_iterator(self):
79
        vts = Vts()
80
81
        vts.add('id_1', name='foo')
82
        vts.add('id_2', name='bar')
83
84
        it = iter(vts)
85
86
        vt_id = next(it)
87
        # Python 3.5 doesn't ensure the order of the retuned keys
88
        self.assertIn(vt_id, ['id_1', 'id_2'])
89
90
        vt_id = next(it)
91
        self.assertIn(vt_id, ['id_1', 'id_2'])
92
93
        with self.assertRaises(StopIteration):
94
            next(it)
95
96
    def test_keys(self):
97
        vts = Vts()
98
99
        vts.add('id_1', name='foo')
100
        vts.add('id_2', name='bar')
101
102
        # use assertCountEqual for Python 3.5 because dict.keys order is
103
        # undefined
104
        self.assertCountEqual(vts.keys(), ['id_1', 'id_2'])
105
106
    def test_getitem(self):
107
        vts = Vts()
108
109
        vts.add('id_1', name='foo')
110
111
        vt = vts['id_1']
112
113
        self.assertEqual(vt['name'], 'foo')
114
115
        with self.assertRaises(KeyError):
116
            vt = vts['foo']
117
118
    def test_copy(self):
119
        vts = Vts()
120
121
        vts.add('id_1', name='foo')
122
        vts.add('id_2', name='bar')
123
124
        vts2 = vts.copy()
125
126
        self.assertIsNot(vts, vts2)
127
        self.assertIsNot(vts.vts, vts2.vts)
128
129
        vta = vts.get('id_1')
130
        vtb = vts2.get('id_1')
131
        self.assertEqual(vta['name'], vtb['name'])
132
        self.assertIsNot(vta, vtb)
133
134
        vta = vts.get('id_2')
135
        vtb = vts2.get('id_2')
136
        self.assertEqual(vta['name'], vtb['name'])
137
        self.assertIsNot(vta, vtb)
138