Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions tests/detect_tests.py
Original file line number Diff line number Diff line change
@@ -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()