From 2ca4ad1e77cf917b9f9e7ca46528754dd3360e7f Mon Sep 17 00:00:00 2001 From: Eddy Hintze Date: Mon, 27 Jan 2020 18:51:59 -0500 Subject: [PATCH] Added tests. Fixed include & exclude being switched --- .gitlab-ci.yml | 8 +++- humblebundle_downloader/download_library.py | 28 +++++++----- tests/__init__.py | 0 tests/test_download_library.py | 49 +++++++++++++++++++++ 4 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_download_library.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index aeeb73b..6fe31fd 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,4 +1,4 @@ -image: python:3 +image: python:3.7-alpine stages: - test @@ -13,6 +13,12 @@ flake8: - pip install tox - tox -e flake8 +pytest: + stage: test + script: + - pip install tox + - tox -e py37 + pypi-package: stage: package only: diff --git a/humblebundle_downloader/download_library.py b/humblebundle_downloader/download_library.py index 5cb5861..1779c5c 100644 --- a/humblebundle_downloader/download_library.py +++ b/humblebundle_downloader/download_library.py @@ -25,10 +25,7 @@ class DownloadLibrary: def __init__(self, cookie_path, library_path, progress_bar=False, ext_include=None, ext_exclude=None, platform_include=None, purchase_keys=None, trove=False, update=False): - - with open(cookie_path, 'r') as f: - self.account_cookies = f.read().strip() - + self.cookie_path = cookie_path self.library_path = library_path self.progress_bar = progress_bar self.ext_include = [] if ext_include is None else list(map(str.lower, ext_include)) # noqa: E501 @@ -40,19 +37,23 @@ class DownloadLibrary: self.platform_include = list(map(str.lower, platform_include)) self.cache_file = os.path.join(library_path, '.cache.json') + self.purchase_keys = purchase_keys + self.trove = trove + self.update = update + + def start(self): + with open(self.cookie_path, 'r') as f: + self.account_cookies = f.read().strip() + self.cache_data = self._load_cache_data(self.cache_file) + self.purchase_keys = self.purchase_keys if self.purchase_keys else self._get_purchase_keys() # noqa: E501 - self.purchase_keys = purchase_keys if purchase_keys else self._get_purchase_keys() # noqa: E501 - - if trove is True: + if self.trove is True: logger.info("Checking Humble Trove...") self.trove_products = self._get_trove_products() else: self.trove_products = [] - self.update = update - - def start(self): for product in self.trove_products: title = _clean_name(product['human-name']) self._process_trove_product(title, product) @@ -329,5 +330,8 @@ class DownloadLibrary: def _should_download_file_type(self, ext): ext = ext.lower() - return ((self.ext_include and ext.lower() not in self.ext_include) - or (self.ext_exclude and ext.lower() in self.ext_exclude)) + if self.ext_include != []: + return ext in self.ext_include + elif self.ext_exclude != []: + return ext not in self.ext_exclude + return True diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_download_library.py b/tests/test_download_library.py new file mode 100644 index 0000000..46bd440 --- /dev/null +++ b/tests/test_download_library.py @@ -0,0 +1,49 @@ +from humblebundle_downloader.download_library import DownloadLibrary + + +def test_include_logic_has_values(): + dl = DownloadLibrary( + 'fake_cookie_path', + 'fake_library_path', + ext_include=['pdf', 'EPub'], + ) + assert dl._should_download_file_type('pdf') is True + assert dl._should_download_file_type('df') is False + assert dl._should_download_file_type('ePub') is True + assert dl._should_download_file_type('mobi') is False + + +def test_include_logic_empty(): + dl = DownloadLibrary( + 'fake_cookie_path', + 'fake_library_path', + ext_include=[], + ) + assert dl._should_download_file_type('pdf') is True + assert dl._should_download_file_type('df') is True + assert dl._should_download_file_type('EPub') is True + assert dl._should_download_file_type('mobi') is True + + +def test_exclude_logic_has_values(): + dl = DownloadLibrary( + 'fake_cookie_path', + 'fake_library_path', + ext_exclude=['pdf', 'EPub'], + ) + assert dl._should_download_file_type('pdf') is False + assert dl._should_download_file_type('df') is True + assert dl._should_download_file_type('ePub') is False + assert dl._should_download_file_type('mobi') is True + + +def test_exclude_logic_empty(): + dl = DownloadLibrary( + 'fake_cookie_path', + 'fake_library_path', + ext_exclude=[], + ) + assert dl._should_download_file_type('pdf') is True + assert dl._should_download_file_type('df') is True + assert dl._should_download_file_type('EPub') is True + assert dl._should_download_file_type('mobi') is True