Passed
Pull Request — master (#72)
by Matěj
01:09
created

test_utils   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 42
eloc 75
dl 0
loc 146
rs 8.295
c 0
b 0
f 0

16 Functions

Rating   Name   Duplication   Size   Complexity  
A test_list() 0 6 4
A mock_utils_os() 0 4 1
A test_relative_relative() 0 2 2
A test_absolute_absolute() 0 2 2
A test_dict() 0 6 3
A test_no_dir() 0 4 1
A test_tuple() 0 6 4
A mock_os() 0 7 1
A test_gen() 0 8 4
A test_existing_dir() 0 8 2
A test_set() 0 6 4
A test_nonexisting_dir() 0 8 1
A test_str() 0 6 4
B test_namedtuple() 0 8 5
A test_absolute_relative() 0 2 2
A test_relative_absolute() 0 2 2

How to fix   Complexity   

Complexity

Complex classes like test_utils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#
2
# Copyright (C) 2013  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
# Red Hat Author(s): Vratislav Podzimek <[email protected]>
19
#
20
21
"""Module with unit tests for the common.py module"""
22
23
import mock
24
from collections import namedtuple
25
26
import pytest
27
28
from org_fedora_oscap import utils
29
30
31
@pytest.fixture()
32
def mock_os():
33
    mock_os = mock.Mock()
34
    mock_os.makedirs = mock.Mock()
35
    mock_os.path = mock.Mock()
36
    mock_os.path.isdir = mock.Mock()
37
    return mock_os
38
39
40
def mock_utils_os(mock_os, monkeypatch):
41
    utils_module_symbols = utils.__dict__
42
43
    monkeypatch.setitem(utils_module_symbols, "os", mock_os)
44
45
46
def test_existing_dir(mock_os, monkeypatch):
47
    mock_utils_os(mock_os, monkeypatch)
48
    mock_os.path.isdir.return_value = True
49
50
    utils.ensure_dir_exists("/tmp/test_dir")
51
52
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
53
    assert not mock_os.makedirs.called
54
55
56
def test_nonexisting_dir(mock_os, monkeypatch):
57
    mock_utils_os(mock_os, monkeypatch)
58
    mock_os.path.isdir.return_value = False
59
60
    utils.ensure_dir_exists("/tmp/test_dir")
61
62
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
63
    mock_os.makedirs.assert_called_with("/tmp/test_dir")
64
65
66
def test_no_dir(mock_os, monkeypatch):
67
    mock_utils_os(mock_os, monkeypatch)
68
    # shouldn't raise an exception
69
    utils.ensure_dir_exists("")
70
71
72
def test_relative_relative():
73
    assert utils.join_paths("foo", "blah") == "foo/blah"
74
75
76
def test_relative_absolute():
77
    assert utils.join_paths("foo", "/blah") == "foo/blah"
78
79
80
def test_absolute_relative():
81
    assert utils.join_paths("/foo", "blah") == "/foo/blah"
82
83
84
def test_absolute_absolute():
85
    assert utils.join_paths("/foo", "/blah") == "/foo/blah"
86
87
88
def test_dict():
89
    dct = {"a": 1, "b": 2}
90
91
    mapped_dct = utils.keep_type_map(str.upper, dct)
92
    assert list(mapped_dct.keys()) == ["A", "B"]
93
    assert isinstance(mapped_dct, dict)
94
95
96
def test_list():
97
    lst = [1, 2, 4, 5]
98
99
    mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst)
100
    assert mapped_lst == [1, 4, 16, 25]
101
    assert isinstance(mapped_lst, list)
102
103
104
def test_tuple():
105
    tpl = (1, 2, 4, 5)
106
107
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl)
108
    assert mapped_tpl == (1, 4, 16, 25)
109
    assert isinstance(mapped_tpl, tuple)
110
111
112
def test_namedtuple():
113
    NT = namedtuple("TestingNT", ["a", "b"])
114
    ntpl = NT(2, 4)
115
116
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl)
117
    assert mapped_tpl == NT(4, 16)
118
    assert isinstance(mapped_tpl, tuple)
119
    assert isinstance(mapped_tpl, NT)
120
121
122
def test_set():
123
    st = {1, 2, 4, 5}
124
125
    mapped_st = utils.keep_type_map(lambda x: x ** 2, st)
126
    assert mapped_st == {1, 4, 16, 25}
127
    assert isinstance(mapped_st, set)
128
129
130
def test_str():
131
    stri = "abcd"
132
133
    mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri)
134
    assert mapped_stri == "cdef"
135
    assert isinstance(mapped_stri, str)
136
137
138
def test_gen():
139
    gen = (1, 2, 4, 5)
140
141
    mapped_gen = utils.keep_type_map(lambda x: x ** 2, gen)
142
    assert tuple(mapped_gen) == (1, 4, 16, 25)
143
144
    # any better test for this?
145
    assert "__next__" in dir(mapped_gen)
146