Blame tests/test_utils.py

Packit 792a06
#
Packit 792a06
# Copyright (C) 2013  Red Hat, Inc.
Packit 792a06
#
Packit 792a06
# This copyrighted material is made available to anyone wishing to use,
Packit 792a06
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 792a06
# the GNU General Public License v.2, or (at your option) any later version.
Packit 792a06
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 792a06
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 792a06
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 792a06
# Public License for more details.  You should have received a copy of the
Packit 792a06
# GNU General Public License along with this program; if not, write to the
Packit 792a06
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 792a06
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 792a06
# source code or documentation are not subject to the GNU General Public
Packit 792a06
# License and may only be used or replicated with the express permission of
Packit 792a06
# Red Hat, Inc.
Packit 792a06
#
Packit 792a06
# Red Hat Author(s): Vratislav Podzimek <vpodzime@redhat.com>
Packit 792a06
#
Packit 792a06
Packit 792a06
"""Module with unit tests for the common.py module"""
Packit 792a06
Packit 792a06
import mock
Packit 792a06
import os
Packit 792a06
from collections import namedtuple
Packit 792a06
Packit 792a06
import pytest
Packit 792a06
Packit 792a06
from org_fedora_oscap import utils
Packit 792a06
Packit 792a06
Packit 792a06
@pytest.fixture()
Packit 792a06
def mock_os():
Packit 792a06
    mock_os = mock.Mock()
Packit 792a06
    mock_os.makedirs = mock.Mock()
Packit 792a06
    mock_os.path = mock.Mock()
Packit 792a06
    mock_os.path.isdir = mock.Mock()
Packit 792a06
    return mock_os
Packit 792a06
Packit 792a06
Packit 792a06
def mock_utils_os(mock_os, monkeypatch):
Packit 792a06
    utils_module_symbols = utils.__dict__
Packit 792a06
Packit 792a06
    monkeypatch.setitem(utils_module_symbols, "os", mock_os)
Packit 792a06
Packit 792a06
Packit 792a06
def test_existing_dir(mock_os, monkeypatch):
Packit 792a06
    mock_utils_os(mock_os, monkeypatch)
Packit 792a06
    mock_os.path.isdir.return_value = True
Packit 792a06
Packit 792a06
    utils.ensure_dir_exists("/tmp/test_dir")
Packit 792a06
Packit 792a06
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
Packit 792a06
    assert not mock_os.makedirs.called
Packit 792a06
Packit 792a06
Packit 792a06
def test_nonexisting_dir(mock_os, monkeypatch):
Packit 792a06
    mock_utils_os(mock_os, monkeypatch)
Packit 792a06
    mock_os.path.isdir.return_value = False
Packit 792a06
Packit 792a06
    utils.ensure_dir_exists("/tmp/test_dir")
Packit 792a06
Packit 792a06
    mock_os.path.isdir.assert_called_with("/tmp/test_dir")
Packit 792a06
    mock_os.makedirs.assert_called_with("/tmp/test_dir")
Packit 792a06
Packit 792a06
Packit 792a06
def test_no_dir(mock_os, monkeypatch):
Packit 792a06
    mock_utils_os(mock_os, monkeypatch)
Packit 792a06
    # shouldn't raise an exception
Packit 792a06
    utils.ensure_dir_exists("")
Packit 792a06
Packit 792a06
Packit 792a06
def test_relative_relative():
Packit 792a06
    assert utils.join_paths("foo", "blah") == "foo/blah"
Packit 792a06
Packit 792a06
Packit 792a06
def test_relative_absolute():
Packit 792a06
    assert utils.join_paths("foo", "/blah") == "foo/blah"
Packit 792a06
Packit 792a06
Packit 792a06
def test_absolute_relative():
Packit 792a06
    assert utils.join_paths("/foo", "blah") == "/foo/blah"
Packit 792a06
Packit 792a06
Packit 792a06
def test_absolute_absolute():
Packit 792a06
    assert utils.join_paths("/foo", "/blah") == "/foo/blah"
Packit 792a06
Packit 792a06
Packit 792a06
def test_dict():
Packit 792a06
    dct = {"a": 1, "b": 2}
Packit 792a06
Packit 792a06
    mapped_dct = utils.keep_type_map(str.upper, dct)
Packit 792a06
    assert list(mapped_dct.keys()) == ["A", "B"]
Packit 792a06
    assert isinstance(mapped_dct, dict)
Packit 792a06
Packit 792a06
Packit 792a06
def test_list():
Packit 792a06
    lst = [1, 2, 4, 5]
Packit 792a06
Packit 792a06
    mapped_lst = utils.keep_type_map(lambda x: x ** 2, lst)
Packit 792a06
    assert mapped_lst == [1, 4, 16, 25]
Packit 792a06
    assert isinstance(mapped_lst, list)
Packit 792a06
Packit 792a06
Packit 792a06
def test_tuple():
Packit 792a06
    tpl = (1, 2, 4, 5)
Packit 792a06
Packit 792a06
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, tpl)
Packit 792a06
    assert mapped_tpl == (1, 4, 16, 25)
Packit 792a06
    assert isinstance(mapped_tpl, tuple)
Packit 792a06
Packit 792a06
Packit 792a06
def test_namedtuple():
Packit 792a06
    NT = namedtuple("TestingNT", ["a", "b"])
Packit 792a06
    ntpl = NT(2, 4)
Packit 792a06
Packit 792a06
    mapped_tpl = utils.keep_type_map(lambda x: x ** 2, ntpl)
Packit 792a06
    assert mapped_tpl == NT(4, 16)
Packit 792a06
    assert isinstance(mapped_tpl, tuple)
Packit 792a06
    assert isinstance(mapped_tpl, NT)
Packit 792a06
Packit 792a06
Packit 792a06
def test_set():
Packit 792a06
    st = {1, 2, 4, 5}
Packit 792a06
Packit 792a06
    mapped_st = utils.keep_type_map(lambda x: x ** 2, st)
Packit 792a06
    assert mapped_st == {1, 4, 16, 25}
Packit 792a06
    assert isinstance(mapped_st, set)
Packit 792a06
Packit 792a06
Packit 792a06
def test_str():
Packit 792a06
    stri = "abcd"
Packit 792a06
Packit 792a06
    mapped_stri = utils.keep_type_map(lambda c: chr((ord(c) + 2) % 256), stri)
Packit 792a06
    assert mapped_stri == "cdef"
Packit 792a06
    assert isinstance(mapped_stri, str)
Packit 792a06
Packit 792a06
Packit 792a06
def test_gen():
Packit 792a06
    generator = (el for el in (1, 2, 4, 5))
Packit 792a06
Packit 792a06
    mapped_generator = utils.keep_type_map(lambda x: x ** 2, generator)
Packit 792a06
    assert tuple(mapped_generator) == (1, 4, 16, 25)
Packit 792a06
Packit 792a06
    # any better test for this?
Packit 792a06
    assert "__next__" in dir(mapped_generator)
Packit 792a06
Packit 792a06
Packit 792a06
def test_hash():
Packit 792a06
    file_hash = '87fcda7d9e7a22412e95779e2f8e70f929106c7b27a94f5f8510553ebf4624a6'
Packit 792a06
    hash_obj = utils.get_hashing_algorithm(file_hash)
Packit 792a06
    assert hash_obj.name == "sha256"
Packit 792a06
Packit 792a06
    filepath = os.path.join(os.path.dirname(__file__), 'data', 'file')
Packit 792a06
    computed_hash = utils.get_file_fingerprint(filepath, hash_obj)
Packit 792a06
Packit 792a06
    assert file_hash == computed_hash