Completed
Push — rhel7-branch ( ce64a6...417774 )
by
unknown
22s queued 11s
created

test_utils.test_hash()   A

Complexity

Conditions 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nop 0
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
import os
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
    generator = (el for el in (1, 2, 4, 5))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable el does not seem to be defined.
Loading history...
141
142
    mapped_generator = utils.keep_type_map(lambda x: x ** 2, generator)
143
    assert tuple(mapped_generator) == (1, 4, 16, 25)
144
145
    # any better test for this?
146
    assert "next" in dir(mapped_generator)
147
148
149
def test_hash():
150
    file_hash = '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6'
151
    hash_obj = utils.get_hashing_algorithm(file_hash)
152
    assert hash_obj.name == "sha256"
153
154
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'file')
155
    computed_hash = utils.get_file_fingerprint(filepath, hash_obj)
156
157
    assert file_hash == computed_hash
158