Skip to content

Commit

Permalink
add more file.system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sirfoga committed Oct 12, 2018
1 parent ac24617 commit 53d431d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 73 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.coveragerc
docs/_build/
docs/*.html
htmlcov/

### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ fast-install:
$(MAKE) show-installed-version

test:
python3 -m pytest --cov=./
rm -rf htmlcov/
python3 -m pytest --cov=./ --cov-report=html
@echo "\033[95m\nTest report htmlcov/index.html\033[0m"

flake8:
pipenv run flake8 --ignore=E501,F401,E128,E402,E731,F821,E722 hal
Expand Down
8 changes: 8 additions & 0 deletions docs/source/hal.strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ hal.strings package
Submodules
----------

hal.strings.models module
-------------------------

.. automodule:: hal.strings.models
:members:
:undoc-members:
:show-inheritance:

hal.strings.utils module
------------------------

Expand Down
2 changes: 1 addition & 1 deletion hal/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__description__ = 'Your swiss knife to perform fast and easy pythonic stuff'
__url__ = 'https://sirfoga.github.io/pyhal/'
__version__ = '10.2.3'
__build__ = '1e6a8b1cf583364dffb5186d9974302dff6728bc'
__build__ = '9e4365e132a30311e5eb7a0baaa266eb3aa4f3ae'
__author__ = 'Stefano Fogarollo'
__author_email__ = 'sirfoga@protonmail.com'
__license__ = 'MIT'
Expand Down
18 changes: 12 additions & 6 deletions hal/data/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,25 @@ def append(self, val):
"""
return self.insert(val, self.length())

def insert_first(self, val):
"""Insert in head
:param val: Object to insert
:return: True iff insertion completed successfully
"""

self.head = Node(val, next_node=self.head)
return True

def insert(self, val, position=0):
"""Insert in position
:param val: Object to insert
:param position: Index of insertion
:return: bool: True iff insertion completed successfully
"""
if position < 0 or position > self.length():
return False

if position == 0: # at beginning
self.head = Node(val, next_node=self.head)
return True
if position <= 0: # at beginning
return self.insert_first(val)

counter = 0
last_node = self.head
Expand Down
5 changes: 3 additions & 2 deletions hal/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ def __init__(self, dictionary):
"""
self.tests = dictionary

def assert_all(self, func=None, args=None):
def assert_all(self, func=None, *args, **kwargs):
"""Asserts tests
:param func: function to assert
:param args: params in function
:param kwargs: extra params
:return: True iff all tests pass
"""

Expand All @@ -30,7 +31,7 @@ def assert_all(self, func=None, args=None):
tests = self.tests.items()
if func is not None:
tests = [
(func(key, *args), val)
(func(key, *args, **kwargs), val)
for key, val in tests
]

Expand Down
10 changes: 2 additions & 8 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class TestLinkedList:
""" Tests hal.files.models.FileSystem path handlers """

def test_build(self):
"""
:return: bool
True iff FileSystem.fix_raw_path correctly handles raw paths
"""
"""Asserts iff FileSystem.fix_raw_path correctly handles raw paths"""

tests = [
[],
Expand All @@ -41,10 +38,7 @@ def test_build(self):
assert from_and_to_lst(lst) == lst

def test_insertion(self):
"""
:return: bool
True iff FileSystem.fix_raw_path correctly handles raw paths
"""
"""Asserts iff FileSystem.fix_raw_path correctly handles raw paths"""

test_attempts = range(1, 10)

Expand Down
132 changes: 77 additions & 55 deletions tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,32 @@
""" Tests files handling methods """

import os
import random
import shutil
from functools import partial

from hal.files.models.system import fix_raw_path, remove_year, \
remove_brackets, extract_name_max_chars, BAD_CHARS, prettify, ls_dir, \
ls_recurse
remove_brackets, extract_name_max_chars, BAD_CHARS, prettify, ls_recurse, \
is_file, get_parent_folder, list_content, FileSystem, \
RUSSIAN_CHARS
from hal.tests.utils import random_name, BatteryTests


class TestPaths:
""" Tests hal.files.models.FileSystem path handlers """

def test_fix_raw_path(self):
"""
:return: bool
True iff FileSystem.fix_raw_path correctly handles raw paths
"""
"""Asserts iff FileSystem.fix_raw_path correctly handles raw paths"""

tests = {
"//a/b/c": "/a/b/c", # double separators
os.getenv("HOME"): os.getenv("HOME") + "/",
"/a/b/c.txt": "/a/b/c.txt" # files
}
BatteryTests(tests).assert_all(fix_raw_path)

def test_remove_year(self):
"""
:return: bool
True iff FileSystem.remove_year correctly removes years from paths
"""Asserts iff FileSystem.remove_year correctly removes years from
paths
"""

tests = {
Expand All @@ -46,11 +44,8 @@ def test_remove_year(self):
BatteryTests(tests).assert_all(remove_year)

def test_remove_brackets(self):
"""
:return: bool
True iff FileSystem.remove_bracket correctly removes brackets
from paths
"""
"""Asserts iff FileSystem.remove_bracket correctly removes brackets
from paths"""

tests = {
"(": "", # void
Expand All @@ -65,47 +60,89 @@ def test_remove_brackets(self):
BatteryTests(tests).assert_all(remove_brackets)

def test_extract_name_max_chars(self):
"""
:return: bool
True iff FileSystem.extract_name_max_chars correctly extracts
name from paths
"""
"""Asserts iff FileSystem.extract_name_max_chars correctly extracts
name from paths"""

tests = {
"012345678a": "012345678a", # length
"012345678b ": "012345678b",
"012345678c ": "012345678c",
" 012345678d": "012345678d",
" 012345678e": "012345678e",
" 0 12345678e": "0",
"012345678912345678f": "0123456789" # remove
}
BatteryTests(tests).assert_all(
partial(extract_name_max_chars, max_chars=10)
)
BatteryTests(tests).assert_all(extract_name_max_chars, max_chars=10)

def test_prettify(self):
"""
:return: bool
True iff FileSystem.prettify correctly prettifies bad strings
"""
"""Asserts iff FileSystem.prettify correctly prettifies bad strings"""

bad_string = "".join(BAD_CHARS)
tests = {
bad_string: "",
bad_string + bad_string: "",
"a " + BAD_CHARS[0] + " ": "a",
bad_string + "a good string" + bad_string: "a_good_string"
}
BatteryTests(tests).assert_all(partial(prettify, blank="_"))

BatteryTests(tests).assert_all(prettify, blank="_")

def test_is_file(self):
"""Asserts files are files, and folders are not"""

tests = {
os.getenv("HOME"): False,
__file__: True
}

BatteryTests(tests).assert_all(is_file)

def test_is_archive_mac(self):
"""Asserts files are archive from a Mac"""

tests = {
"macosx": True,
__file__: False
}
tests = {
FileSystem(key).is_archive_mac(): val
for key, val in tests.items()
}

BatteryTests(tests).assert_all()

def test_is_russian(self):
"""Asserts files contain russian chars"""

tests = {
"".join([random.choice(RUSSIAN_CHARS)] * 15 + ["fjdhf"]): True,
"".join([random.choice(RUSSIAN_CHARS)] * 2 + ["fjdhf"]): False,
__file__: False
}
tests = {
FileSystem(key).is_russian(): val
for key, val in tests.items()
}

BatteryTests(tests).assert_all()

def test_get_parent_folder(self):
"""Asserts parent folder guessing"""

tests = {
"/a/b/c": "b",
"/a/b/c/": "b",
"/a/b/c//": "b"
}

BatteryTests(tests).assert_all(get_parent_folder)


class TestLs:
""" Tests hal.files.models.FileSystem folders/files functions """

def prepare_temp_files(self):
"""
:return: void
Creates temp file for testing
"""
"""Creates temp file for testing"""

# create folder structure, at the end it will be like
# working_folder/
Expand Down Expand Up @@ -144,10 +181,7 @@ def prepare_temp_files(self):
self._create_temp_files()

def _create_temp_files(self):
"""
:return: void
Creates files/folders structure for tests
"""
"""Creates files/folders structure for tests"""

os.makedirs(self.working_folder) # create folders
os.makedirs(self.inner_folder)
Expand All @@ -156,54 +190,42 @@ def _create_temp_files(self):
open(file, "a").close() # create files

def purge_temp_files(self):
"""
:return: void
Removes all temp files
"""
"""Removes all temp files"""

shutil.rmtree(self.working_folder) # remove main folder

def test_ls_dir(self):
"""
:return: bool
True iff FileSystem.ls_dir correctly list only folders
"""
"""Asserts iff FileSystem.ls_dir correctly list only folders"""

self.prepare_temp_files()
tests = {
self.working_folder: {self.file1, self.file2, self.inner_folder},
self.inner_folder: {self.file11, self.file12}
}

BatteryTests(tests).assert_all(ls_dir)
BatteryTests(tests).assert_all(list_content, recurse=False)
self.purge_temp_files()

def test_ls_recurse(self):
"""
:return: bool
True iff FileSystem.ls_recurse correctly list recursively
"""
"""Asserts iff FileSystem.ls_recurse correctly list recursively"""

self.prepare_temp_files()
tests = {
self.working_folder: {self.file1, self.file2, self.inner_folder,
self.file11, self.file12}
}

BatteryTests(tests).assert_all(ls_recurse)
BatteryTests(tests).assert_all(list_content, recurse=True)
self.purge_temp_files()

def test_ls_hidden(self):
"""
:return: bool
True iff FileSystem.ls correctly list hidden files
"""
"""Asserts iff FileSystem.ls correctly list hidden files"""

self.prepare_temp_files()
tests = {
self.working_folder: {self.file1, self.file2, self.inner_folder,
self.file11, self.file12, self.hidden_file}
}

BatteryTests(tests).assert_all(ls_recurse, {"include_hidden": True})
BatteryTests(tests).assert_all(ls_recurse, include_hidden=True)
self.purge_temp_files()

0 comments on commit 53d431d

Please sign in to comment.