From 2ad6b3afdf7f2a8ccff640151d1c7c58a463e09e Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Mon, 20 Jun 2022 15:48:07 -0600 Subject: [PATCH] Add pytest --- src/main.py | 41 +++++--- test/requirements.txt | 1 + test/test_main_.py | 47 +++++++++ test/test_main_cleanup_watched.py | 154 ++++++++++++++++++++++++++++++ 4 files changed, 232 insertions(+), 11 deletions(-) create mode 100644 test/requirements.txt create mode 100644 test/test_main_.py create mode 100644 test/test_main_cleanup_watched.py diff --git a/src/main.py b/src/main.py index 6528b08..177ba5e 100644 --- a/src/main.py +++ b/src/main.py @@ -6,8 +6,6 @@ from src.functions import logger, str_to_bool, search_mapping, generate_library_ from src.plex import Plex from src.jellyfin import Jellyfin - - load_dotenv(override=True) def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_mapping=None): @@ -97,8 +95,7 @@ def cleanup_watched(watched_list_1, watched_list_2, user_mapping=None, library_m return modified_watched_list_1 -def setup_black_white_lists(library_mapping=None): - blacklist_library = os.getenv("BLACKLIST_LIBRARY") +def setup_black_white_lists(blacklist_library: str, whitelist_library: str, blacklist_library_type: str, whitelist_library_type: str, blacklist_users: str, whitelist_users: str, library_mapping=None, user_mapping=None): if blacklist_library: if len(blacklist_library) > 0: blacklist_library = blacklist_library.split(",") @@ -113,10 +110,8 @@ def setup_black_white_lists(library_mapping=None): blacklist_library = blacklist_library + temp_library else: blacklist_library = [] - logger(f"Blacklist Library: {blacklist_library}", 1) - whitelist_library = os.getenv("WHITELIST_LIBRARY") if whitelist_library: if len(whitelist_library) > 0: whitelist_library = whitelist_library.split(",") @@ -133,7 +128,6 @@ def setup_black_white_lists(library_mapping=None): whitelist_library = [] logger(f"Whitelist Library: {whitelist_library}", 1) - blacklist_library_type = os.getenv("BLACKLIST_LIBRARY_TYPE") if blacklist_library_type: if len(blacklist_library_type) > 0: blacklist_library_type = blacklist_library_type.split(",") @@ -142,7 +136,6 @@ def setup_black_white_lists(library_mapping=None): blacklist_library_type = [] logger(f"Blacklist Library Type: {blacklist_library_type}", 1) - whitelist_library_type = os.getenv("WHITELIST_LIBRARY_TYPE") if whitelist_library_type: if len(whitelist_library_type) > 0: whitelist_library_type = whitelist_library_type.split(",") @@ -151,20 +144,34 @@ def setup_black_white_lists(library_mapping=None): whitelist_library_type = [] logger(f"Whitelist Library Type: {whitelist_library_type}", 1) - blacklist_users = os.getenv("BLACKLIST_USERS") if blacklist_users: if len(blacklist_users) > 0: blacklist_users = blacklist_users.split(",") blacklist_users = [x.lower().strip() for x in blacklist_users] + if user_mapping: + temp_users = [] + for user in blacklist_users: + user_other = search_mapping(user_mapping, user) + if user_other: + temp_users.append(user_other) + + blacklist_users = blacklist_users + temp_users else: blacklist_users = [] logger(f"Blacklist Users: {blacklist_users}", 1) - whitelist_users = os.getenv("WHITELIST_USERS") if whitelist_users: if len(whitelist_users) > 0: whitelist_users = whitelist_users.split(",") whitelist_users = [x.lower().strip() for x in whitelist_users] + if user_mapping: + temp_users = [] + for user in whitelist_users: + user_other = search_mapping(user_mapping, user) + if user_other: + temp_users.append(user_other) + + whitelist_users = whitelist_users + temp_users else: whitelist_users = [] else: @@ -330,9 +337,18 @@ def main_loop(): logger(f"Library Mapping: {library_mapping}", 1) # Create (black/white)lists - blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users = setup_black_white_lists(library_mapping) + logger("Creating (black/white)lists", 1) + blacklist_library = os.getenv("BLACKLIST_LIBRARY", None) + whitelist_library = os.getenv("WHITELIST_LIBRARY", None) + blacklist_library_type = os.getenv("BLACKLIST_LIBRARY_TYPE", None) + whitelist_library_type = os.getenv("WHITELIST_LIBRARY_TYPE", None) + blacklist_users = os.getenv("BLACKLIST_USERS", None) + whitelist_users = os.getenv("WHITELIST_USERS", None) + + blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users = setup_black_white_lists(blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users, library_mapping, user_mapping) # Create server connections + logger("Creating server connections", 1) servers = generate_server_connections() for server_1 in servers: @@ -347,8 +363,10 @@ def main_loop(): server_2_connection = server_2[1] # Create users list + logger("Creating users list", 1) server_1_users, server_2_users = setup_users(server_1, server_2, blacklist_users, whitelist_users, user_mapping) + logger("Creating watched lists", 1) args = [[server_1_connection.get_watched, server_1_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping] , [server_2_connection.get_watched, server_2_users, blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, library_mapping]] @@ -394,6 +412,7 @@ def main(): logger(traceback.format_exc(), 2) logger(f"Retrying in {sleep_duration}", log_type=0) + sleep(sleep_duration) except KeyboardInterrupt: logger("Exiting", log_type=0) diff --git a/test/requirements.txt b/test/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/test/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/test/test_main_.py b/test/test_main_.py new file mode 100644 index 0000000..b1c350e --- /dev/null +++ b/test/test_main_.py @@ -0,0 +1,47 @@ +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.main import setup_black_white_lists + +def test_setup_black_white_lists(): + # Simple + blacklist_library = 'library1, library2' + whitelist_library = 'library1, library2' + blacklist_library_type = 'library_type1, library_type2' + whitelist_library_type = 'library_type1, library_type2' + blacklist_users = 'user1, user2' + whitelist_users = 'user1, user2' + + results_blacklist_library, return_whitelist_library, return_blacklist_library_type, return_whitelist_library_type, return_blacklist_users, return_whitelist_users = setup_black_white_lists(blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users) + + assert results_blacklist_library == ['library1', 'library2'] + assert return_whitelist_library == ['library1', 'library2'] + assert return_blacklist_library_type == ['library_type1', 'library_type2'] + assert return_whitelist_library_type == ['library_type1', 'library_type2'] + assert return_blacklist_users == ['user1', 'user2'] + assert return_whitelist_users == ['user1', 'user2'] + + # Library Mapping and user mapping + library_mapping = { "library1": "library3" } + user_mapping = { "user1": "user3" } + + results_blacklist_library, return_whitelist_library, return_blacklist_library_type, return_whitelist_library_type, return_blacklist_users, return_whitelist_users = setup_black_white_lists(blacklist_library, whitelist_library, blacklist_library_type, whitelist_library_type, blacklist_users, whitelist_users, library_mapping, user_mapping) + + assert results_blacklist_library == ['library1', 'library2', 'library3'] + assert return_whitelist_library == ['library1', 'library2', 'library3'] + assert return_blacklist_library_type == ['library_type1', 'library_type2'] + assert return_whitelist_library_type == ['library_type1', 'library_type2'] + assert return_blacklist_users == ['user1', 'user2', 'user3'] + assert return_whitelist_users == ['user1', 'user2', 'user3'] diff --git a/test/test_main_cleanup_watched.py b/test/test_main_cleanup_watched.py new file mode 100644 index 0000000..b010542 --- /dev/null +++ b/test/test_main_cleanup_watched.py @@ -0,0 +1,154 @@ +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.main import cleanup_watched + +tv_shows_watched_list_1 = { + frozenset({("tvdb", "75710"), ("title", "Criminal Minds"), ("imdb", "tt0452046"), ("locations", ("Criminal Minds",)), ("tmdb", "4057")}): { + "Season 1": [ + {'imdb': 'tt0550489', 'tmdb': '282843', 'tvdb': '176357', 'locations': ('Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv',)}, + {'imdb': 'tt0550487', 'tmdb': '282861', 'tvdb': '300385', 'locations': ('Criminal Minds S01E02 Compulsion WEBDL-720p.mkv',)} + ] + } +} + +movies_watched_list_1 = [ + {"imdb":"tt2380307", "tmdb":"354912", 'title': 'Coco', 'locations': ('Coco (2017) Remux-1080p.mkv',)}, + {"tmdbcollection":"448150", "imdb":"tt1431045", "tmdb":"293660", 'title': 'Deadpool', 'locations': ('Deadpool (2016) Remux-1080p.mkv',)}, +] + +tv_shows_watched_list_2 = { + frozenset({("tvdb", "75710"), ("title", "Criminal Minds"), ("imdb", "tt0452046"), ("locations", ("Criminal Minds",)), ("tmdb", "4057")}): { + "Season 1": [ + {'imdb': 'tt0550487', 'tmdb': '282861', 'tvdb': '300385', 'locations': ('Criminal Minds S01E02 Compulsion WEBDL-720p.mkv',)}, + {'imdb': 'tt0550498', 'tmdb': '282865', 'tvdb': '300474', 'locations': ("Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",)} + ] + } +} + +movies_watched_list_2 = [ + {"imdb":"tt2380307", "tmdb":"354912", 'title': 'Coco', 'locations': ('Coco (2017) Remux-1080p.mkv',)}, + {'imdb': 'tt0384793', 'tmdb': '9788', 'tvdb': '9103', 'title': 'Accepted', 'locations': ('Accepted (2006) Remux-1080p.mkv',)} +] + +# Test to see if objects get deleted all the way up to the root. +tv_shows_2_watched_list_1 = { + frozenset({("tvdb", "75710"), ("title", "Criminal Minds"), ("imdb", "tt0452046"), ("locations", ("Criminal Minds",)), ("tmdb", "4057")}): { + "Season 1": [ + {'imdb': 'tt0550489', 'tmdb': '282843', 'tvdb': '176357', 'locations': ('Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv',)}, + ] + } +} + +expected_tv_show_watched_list_1 = { + frozenset({("tvdb", "75710"), ("title", "Criminal Minds"), ("imdb", "tt0452046"), ("locations", ("Criminal Minds",)), ("tmdb", "4057")}): { + "Season 1": [ + {'imdb': 'tt0550489', 'tmdb': '282843', 'tvdb': '176357', 'locations': ('Criminal Minds S01E01 Extreme Aggressor WEBDL-720p.mkv',)} + ] + } +} + +expected_movie_watched_list_1 = [ + {"tmdbcollection":"448150", "imdb":"tt1431045", "tmdb":"293660", 'title': 'Deadpool', 'locations': ('Deadpool (2016) Remux-1080p.mkv',)} +] + +expected_tv_show_watched_list_2 = { + frozenset({("tvdb", "75710"), ("title", "Criminal Minds"), ("imdb", "tt0452046"), ("locations", ("Criminal Minds",)), ("tmdb", "4057")}): { + "Season 1": [ + {'imdb': 'tt0550498', 'tmdb': '282865', 'tvdb': '300474', 'locations': ("Criminal Minds S01E03 Won't Get Fooled Again WEBDL-720p.mkv",)} + ] + } +} + +expected_movie_watched_list_2 = [ + {'imdb': 'tt0384793', 'tmdb': '9788', 'tvdb': '9103', 'title': 'Accepted', 'locations': ('Accepted (2006) Remux-1080p.mkv',)} +] + + +def test_simple_cleanup_watched(): + user_watched_list_1 = { + "user1": { + "TV Shows": tv_shows_watched_list_1, + "Movies": movies_watched_list_1, + "Other Shows": tv_shows_2_watched_list_1 + }, + } + user_watched_list_2 = { + "user1": { + "TV Shows": tv_shows_watched_list_2, + "Movies": movies_watched_list_2, + "Other Shows": tv_shows_2_watched_list_1 + } + } + + expected_watched_list_1 = { + "user1": { + "TV Shows": expected_tv_show_watched_list_1 + , "Movies": expected_movie_watched_list_1 + } + } + + expected_watched_list_2 = { + "user1": { + "TV Shows": expected_tv_show_watched_list_2 + , "Movies": expected_movie_watched_list_2 + } + } + + return_watched_list_1 = cleanup_watched(user_watched_list_1, user_watched_list_2) + return_watched_list_2 = cleanup_watched(user_watched_list_2, user_watched_list_1) + + assert return_watched_list_1 == expected_watched_list_1 + assert return_watched_list_2 == expected_watched_list_2 + + +def test_mapping_cleanup_watched(): + user_watched_list_1 = { + "user1": { + "TV Shows": tv_shows_watched_list_1, + "Movies": movies_watched_list_1, + "Other Shows": tv_shows_2_watched_list_1 + }, + } + user_watched_list_2 = { + "user2": { + "Shows": tv_shows_watched_list_2, + "Movies": movies_watched_list_2, + "Other Shows": tv_shows_2_watched_list_1 + } + } + + expected_watched_list_1 = { + "user1": { + "TV Shows": expected_tv_show_watched_list_1 + , "Movies": expected_movie_watched_list_1 + } + } + + expected_watched_list_2 = { + "user2": { + "Shows": expected_tv_show_watched_list_2 + , "Movies": expected_movie_watched_list_2 + } + } + + user_mapping = { "user1": "user2" } + library_mapping = { "TV Shows": "Shows" } + + return_watched_list_1 = cleanup_watched(user_watched_list_1, user_watched_list_2, user_mapping=user_mapping, library_mapping=library_mapping) + return_watched_list_2 = cleanup_watched(user_watched_list_2, user_watched_list_1, user_mapping=user_mapping, library_mapping=library_mapping) + + assert return_watched_list_1 == expected_watched_list_1 + assert return_watched_list_2 == expected_watched_list_2