From 3e15120e2ab5ec32587483aa1063e8aab8258e1d Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Wed, 8 Mar 2023 23:17:54 -0700 Subject: [PATCH] Fix library whitelist, add library tests --- src/black_white.py | 1 - src/library.py | 32 +++-- src/plex.py | 1 - test/test_library.py | 302 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 317 insertions(+), 19 deletions(-) create mode 100644 test/test_library.py diff --git a/src/black_white.py b/src/black_white.py index 7763ecc..6b142c0 100644 --- a/src/black_white.py +++ b/src/black_white.py @@ -11,7 +11,6 @@ def setup_black_white_lists( library_mapping=None, user_mapping=None, ): - blacklist_library, blacklist_library_type, blacklist_users = setup_black_lists( blacklist_library, blacklist_library_type, diff --git a/src/library.py b/src/library.py index 7e53d1a..65cb3e3 100644 --- a/src/library.py +++ b/src/library.py @@ -23,7 +23,6 @@ def check_skip_logic( library_type, blacklist_library, blacklist_library_type, - library_mapping, library_other, ) skip_reason_white = check_whitelist_logic( @@ -31,7 +30,6 @@ def check_skip_logic( library_type, whitelist_library, whitelist_library_type, - library_mapping, library_other, ) @@ -53,7 +51,6 @@ def check_blacklist_logic( library_type, blacklist_library, blacklist_library_type, - library_mapping=None, library_other=None, ): skip_reason = None @@ -90,7 +87,6 @@ def check_whitelist_logic( library_type, whitelist_library, whitelist_library_type, - library_mapping=None, library_other=None, ): skip_reason = None @@ -107,26 +103,28 @@ def check_whitelist_logic( # if whitelist is not empty and library is not in whitelist if len(whitelist_library) > 0: - if library_title.lower() not in [x.lower() for x in whitelist_library]: - if skip_reason: - skip_reason = ( - skip_reason - + " and " - + f"{library_title} is not in whitelist_library" - ) - else: - skip_reason = f"{library_title} is not in whitelist_library" - if library_other: - if library_other.lower() not in [x.lower() for x in whitelist_library]: + if library_title.lower() not in [ + x.lower() for x in whitelist_library + ] and library_other.lower() not in [x.lower() for x in whitelist_library]: if skip_reason: skip_reason = ( skip_reason + " and " - + f"{library_other} is not in whitelist_library" + + f"{library_title} is not in whitelist_library" ) else: - skip_reason = f"{library_other} is not in whitelist_library" + skip_reason = f"{library_title} is not in whitelist_library" + else: + if library_title.lower() not in [x.lower() for x in whitelist_library]: + if skip_reason: + skip_reason = ( + skip_reason + + " and " + + f"{library_title} is not in whitelist_library" + ) + else: + skip_reason = f"{library_title} is not in whitelist_library" return skip_reason diff --git a/src/plex.py b/src/plex.py index 8302e9e..9fbb94d 100644 --- a/src/plex.py +++ b/src/plex.py @@ -15,7 +15,6 @@ from src.library import ( ) - # Bypass hostname validation for ssl. Taken from https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186 class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=..., **pool_kwargs): diff --git a/test/test_library.py b/test/test_library.py new file mode 100644 index 0000000..2ffd1da --- /dev/null +++ b/test/test_library.py @@ -0,0 +1,302 @@ +import sys +import os + +# getting the name of the directory +# where the this file is present. +current = os.path.dirname(os.path.realpath(__file__)) + +# Getting the parent directory name +# where the current directory is present. +parent = os.path.dirname(current) + +# adding the parent directory to +# the sys.path. +sys.path.append(parent) + +from src.functions import ( + search_mapping, +) + +from src.library import ( + check_skip_logic, + check_blacklist_logic, + check_whitelist_logic, + show_title_dict, + episode_title_dict, + movies_title_dict, + generate_library_guids_dict, +) + +blacklist_library = ["TV Shows"] +whitelist_library = ["Movies"] +blacklist_library_type = ["episodes"] +whitelist_library_type = ["movies"] +library_mapping = {"Shows": "TV Shows", "Movie": "Movies"} + +show_list = { + frozenset( + { + ("locations", ("The Last of Us",)), + ("tmdb", "100088"), + ("imdb", "tt3581920"), + ("tvdb", "392256"), + ("title", "The Last of Us"), + } + ): { + "Season 1": [ + { + "imdb": "tt11957006", + "tmdb": "2181581", + "tvdb": "8444132", + "locations": ( + "The Last of Us - S01E01 - When You're Lost in the Darkness WEBDL-1080p.mkv", + ), + } + ] + } +} +movie_list = [ + { + "title": "Coco", + "imdb": "tt2380307", + "tmdb": "354912", + "locations": ("Coco (2017) Remux-2160p.mkv", "Coco (2017) Remux-1080p.mkv"), + } +] + +show_titles = { + "imdb": ["tt3581920"], + "locations": ["The Last of Us"], + "tmdb": ["100088"], + "tvdb": ["392256"], +} +episode_titles = { + "imdb": ["tt11957006"], + "locations": [ + "The Last of Us - S01E01 - When You're Lost in the Darkness WEBDL-1080p.mkv" + ], + "tmdb": ["2181581"], + "tvdb": ["8444132"], +} +movie_titles = { + "imdb": ["tt2380307"], + "locations": ["Coco (2017) Remux-2160p.mkv", "Coco (2017) Remux-1080p.mkv"], + "title": ["coco"], + "tmdb": ["354912"], +} + + +def test_check_skip_logic(): + # Failes + library_title = "Test" + library_type = "movies" + skip_reason = check_skip_logic( + library_title, + library_type, + blacklist_library, + whitelist_library, + blacklist_library_type, + whitelist_library_type, + library_mapping, + ) + + assert skip_reason == "Test is not in whitelist_library" + + library_title = "Shows" + library_type = "episodes" + skip_reason = check_skip_logic( + library_title, + library_type, + blacklist_library, + whitelist_library, + blacklist_library_type, + whitelist_library_type, + library_mapping, + ) + + assert ( + skip_reason + == "episodes is in blacklist_library_type and TV Shows is in blacklist_library and " + + "episodes is not in whitelist_library_type and Shows is not in whitelist_library" + ) + + # Passes + library_title = "Movie" + library_type = "movies" + skip_reason = check_skip_logic( + library_title, + library_type, + blacklist_library, + whitelist_library, + blacklist_library_type, + whitelist_library_type, + library_mapping, + ) + + assert skip_reason == None + + +def test_check_blacklist_logic(): + # Fails + library_title = "Shows" + library_type = "episodes" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_blacklist_logic( + library_title, + library_type, + blacklist_library, + blacklist_library_type, + library_other, + ) + + assert ( + skip_reason + == "episodes is in blacklist_library_type and TV Shows is in blacklist_library" + ) + + library_title = "TV Shows" + library_type = "episodes" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_blacklist_logic( + library_title, + library_type, + blacklist_library, + blacklist_library_type, + library_other, + ) + + assert ( + skip_reason + == "episodes is in blacklist_library_type and TV Shows is in blacklist_library" + ) + + # Passes + library_title = "Movie" + library_type = "movies" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_blacklist_logic( + library_title, + library_type, + blacklist_library, + blacklist_library_type, + library_other, + ) + + assert skip_reason == None + + library_title = "Movies" + library_type = "movies" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_blacklist_logic( + library_title, + library_type, + blacklist_library, + blacklist_library_type, + library_other, + ) + + assert skip_reason == None + + +def test_check_whitelist_logic(): + # Fails + library_title = "Shows" + library_type = "episodes" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_whitelist_logic( + library_title, + library_type, + whitelist_library, + whitelist_library_type, + library_other, + ) + + assert ( + skip_reason + == "episodes is not in whitelist_library_type and Shows is not in whitelist_library" + ) + + library_title = "TV Shows" + library_type = "episodes" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_whitelist_logic( + library_title, + library_type, + whitelist_library, + whitelist_library_type, + library_other, + ) + + assert ( + skip_reason + == "episodes is not in whitelist_library_type and TV Shows is not in whitelist_library" + ) + + # Passes + library_title = "Movie" + library_type = "movies" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_whitelist_logic( + library_title, + library_type, + whitelist_library, + whitelist_library_type, + library_other, + ) + + assert skip_reason == None + + library_title = "Movies" + library_type = "movies" + library_other = search_mapping(library_mapping, library_title) + skip_reason = check_whitelist_logic( + library_title, + library_type, + whitelist_library, + whitelist_library_type, + library_other, + ) + + assert skip_reason == None + + +def test_show_title_dict(): + show_titles_dict = show_title_dict(show_list) + + assert show_titles_dict == show_titles + + +def test_episode_title_dict(): + episode_titles_dict = episode_title_dict(show_list) + + assert episode_titles_dict == episode_titles + + +def test_movies_title_dict(): + movies_titles_dict = movies_title_dict(movie_list) + + assert movies_titles_dict == movie_titles + + +def test_generate_library_guids_dict(): + # Test with shows + ( + show_titles_dict, + episode_titles_dict, + movies_titles_dict, + ) = generate_library_guids_dict(show_list) + + assert show_titles_dict == show_titles + assert episode_titles_dict == episode_titles + assert movies_titles_dict == {} + + # Test with movies + ( + show_titles_dict, + episode_titles_dict, + movies_titles_dict, + ) = generate_library_guids_dict(movie_list) + + assert show_titles_dict == {} + assert episode_titles_dict == {} + assert movies_titles_dict == movie_titles