From 0b17f80ea227b851b182f79076712d71341fdd00 Mon Sep 17 00:00:00 2001 From: Aleksander Koprowski Date: Thu, 14 Sep 2023 17:48:16 +0200 Subject: [PATCH] added_conftest.py_and detect_tests.py --- tests/conftest.py | 58 ++++++++++++++++++++++++++++++++++++ tests/detect_tests.py | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/detect_tests.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..218e644 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,58 @@ +import pytest +from platform import system +from pathlib import Path +from os.path import splitdrive, join +from os import listdir +from random import randint, choices +from string import ascii_lowercase + +from spcache.detect import WIN_PATHS, UNIX_PATHS + + +@pytest.fixture() +def system_fixture(): + """Check and save type of working operational system.""" + return system() + + +@pytest.fixture() +def prepare_paths(): + """Prepare and save paths to detect.""" + check = system() + outcome = WIN_PATHS if check == "Windows" else UNIX_PATHS + return outcome + + +@pytest.fixture() +def create_random_path(): + """Create random path which exist in file system.""" + current_dir = Path(__file__).absolute() + drive, rest = splitdrive(current_dir) + if system() == "Windows": + formatted_drive = join(drive, "\\") + else: + formatted_drive = join(drive, "/") + list_dir = listdir(formatted_drive) + if len(list_dir) > 0: + random_number = randint(0, len(list_dir)) + final_path = Path(formatted_drive + list_dir[random_number]) + else: + final_path = Path(formatted_drive) + return final_path + + +@pytest.fixture() +def create_non_existing_path(): + """Create path which doesn't exist in file system.""" + current_dir = Path(__file__).absolute() + drive, rest = splitdrive(current_dir) + if system() == "Windows": + formatted_drive = join(drive, "\\") + else: + formatted_drive = join(drive, "/") + list_dir = listdir(formatted_drive) + appendix = choices(ascii_lowercase) + for character in appendix: + if character not in list_dir: + final_path = Path(formatted_drive + character) + return final_path diff --git a/tests/detect_tests.py b/tests/detect_tests.py new file mode 100644 index 0000000..8348ca0 --- /dev/null +++ b/tests/detect_tests.py @@ -0,0 +1,68 @@ +""" +tests for detect.py +""" + +import pytest +from pathlib import Path + +import spcache.detect as de +from conftest import system_fixture, create_random_path, create_non_existing_path + + +@pytest.fixture() +def current_dir_fixture(): + """Return current directory.""" + return Path(__file__).absolute() + + +def test_paths_existing(): + """Check whether exists at least one path.""" + assert (len(de.UNIX_PATHS) > 0) or (len(de.WIN_PATHS) > 0) + + +def test_ensure(current_dir_fixture, create_non_existing_path): + """Function checks whether detect.ensure() function works properly with current or false directory.""" + current_dir = current_dir_fixture + false_path = create_non_existing_path + assert de.ensure_file(current_dir) is True + assert de.ensure_file(false_path) is False + + +def test_normalization_current_dir(current_dir_fixture): + """Function checks whether current directory will be properly formatted.""" + current_dir = current_dir_fixture + outcome = de.normalize_path(current_dir) + assert type(outcome) == str + + +def test_normalization_random_path(create_random_path): + """Test normalization by random path.""" + path = create_random_path + outcome = de.normalize_path(path) + assert type(outcome) == str + + +def test_win_strategy(system_fixture): + """Base tests for win_strategy function.""" + if system_fixture == "Windows": + assert type(de.win_strategy()) == str + else: + assert type(de.win_strategy()) is None + assert de.win_strategy() + + +def test_unix_strategy(system_fixture): + """Base tests for unix_strategy function.""" + if system_fixture != "Windows": + assert type(de.unix_strategy()) == str + else: + assert type(de.unix_strategy()) != str + + +def test_detect_prefs_file(system_fixture): + """Check whether detect_prefs_file function apply appropriate option for current operational system.""" + if (len(de.UNIX_PATHS) > 0) or (len(de.WIN_PATHS) > 0): + assert type(de.detect_prefs_file()) == str + else: + assert de.detect_prefs_file() is None + assert de.detect_prefs_file()