Passed
Pull Request — master (#72)
by Matěj
02:42
created

test_utils   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 76
dl 0
loc 147
rs 8.3157
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
B test_gen() 0 8 5
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 unittest
24
import mock
25
from collections import namedtuple
26
27
import pytest
28
29
from org_fedora_oscap import utils
30
31
32
@pytest.fixture()
33
def mock_os():
34
    mock_os = mock.Mock()
35
    mock_os.makedirs = mock.Mock()
36
    mock_os.path = mock.Mock()
37
    mock_os.path.isdir = mock.Mock()
38
    return mock_os
39
40
41
def mock_utils_os(mock_os, monkeypatch):
42
    utils_module_symbols = utils.__dict__
43
44
    monkeypatch.setitem(utils_module_symbols, "os", mock_os)
45
46
47
def test_existing_dir(mock_os, monkeypatch):
48
    mock_utils_os(mock_os, monkeypatch)
49
    mock_os.path.isdir.return_value = True
50
51
    utils.ensure_dir_exists("/tmp/test_dir")
52
53
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
54
    assert not mock_os.makedirs.called
55
56
57
def test_nonexisting_dir(mock_os, monkeypatch):
58
    mock_utils_os(mock_os, monkeypatch)
59
    mock_os.path.isdir.return_value = False
60
61
    utils.ensure_dir_exists("/tmp/test_dir")
62
63
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
64
    mock_os.makedirs.assert_called_with("/tmp/test_dir")
65
66
67
def test_no_dir(mock_os, monkeypatch):
68
    mock_utils_os(mock_os, monkeypatch)
69
    # shouldn't raise an exception
70
    utils.ensure_dir_exists("")
71
72
73
def test_relative_relative():
74
    assert utils.join_paths("foo", "blah") == "foo/blah"
75
76
77
def test_relative_absolute():
78
    assert utils.join_paths("foo", "/blah") == "foo/blah"
79
80
81
def test_absolute_relative():
82
    assert utils.join_paths("/foo", "blah") == "/foo/blah"
83
84
85
def test_absolute_absolute():
86
    assert utils.join_paths("/foo", "/blah") == "/foo/blah"
87
88
89
def test_dict():
90
    dct = {"a": 1, "b": 2}
91
92
    mapped_dct = utils.keep_type_map(str.upper, dct)
93
    assert list(mapped_dct.keys()) == ["A", "B"]
94
    assert isinstance(mapped_dct, dict)
95
96
97
def test_list():
98
    lst = [1, 2, 4, 5]
99
100
    mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst)
101
    assert mapped_lst == [1, 4, 16, 25]
102
    assert isinstance(mapped_lst, list)
103
104
105
def test_tuple():
106
    tpl = (1, 2, 4, 5)
107
108
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl)
109
    assert mapped_tpl == (1, 4, 16, 25)
110
    assert isinstance(mapped_tpl, tuple)
111
112
113
def test_namedtuple():
114
    NT = namedtuple("TestingNT", ["a", "b"])
115
    ntpl = NT(2, 4)
116
117
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl)
118
    assert mapped_tpl == NT(4, 16)
119
    assert isinstance(mapped_tpl, tuple)
120
    assert isinstance(mapped_tpl, NT)
121
122
123
def test_set():
124
    st = {1, 2, 4, 5}
125
126
    mapped_st = utils.keep_type_map(lambda x: x ** 2, st)
127
    assert mapped_st == {1, 4, 16, 25}
128
    assert isinstance(mapped_st, set)
129
130
131
def test_str():
132
    stri = "abcd"
133
134
    mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri)
135
    assert mapped_stri == "cdef"
136
    assert isinstance(mapped_stri, str)
137
138
139
def test_gen():
140
    gen = (it for it in [1, 2, 4, 5])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable it does not seem to be defined.
Loading history...
141
142
    mapped_gen = utils.keep_type_map(lambda x: x ** 2, gen)
143
    assert tuple(mapped_gen) == tuple([1, 4, 16, 25])
144
145
    # any better test for this?
146
    assert "__next__" in dir(mapped_gen)
147