From 8682cbb3f1fbf3886d1e54c8b88fafdafd8c1cd8 Mon Sep 17 00:00:00 2001 From: Stefan Cosma Date: Wed, 21 Feb 2024 21:58:41 +0200 Subject: [PATCH] Added docustring for each test --- plexorcist.py | 2 +- test_plexorcist.py | 274 +++++++++++++++++++++++++++++---------------- 2 files changed, 181 insertions(+), 95 deletions(-) diff --git a/plexorcist.py b/plexorcist.py index d6825f1..256c3a8 100755 --- a/plexorcist.py +++ b/plexorcist.py @@ -239,7 +239,7 @@ def filter_videos(self, videos): # Check if video was watched and / or is older than def is_watched_video(video): return ( - type(video) is dict + isinstance(video) and video.get("@viewCount") and int(video["@viewCount"]) >= 1 and ( diff --git a/test_plexorcist.py b/test_plexorcist.py index 79857ff..35f9d9a 100644 --- a/test_plexorcist.py +++ b/test_plexorcist.py @@ -1,27 +1,59 @@ +#!/usr/bin/env python +"""Test the main Plexorcist execution file!""" + import unittest from unittest.mock import MagicMock, patch from plexorcist import Plexorcist class TestPlexorcist(unittest.TestCase): + """The main test class for unit tests + + Args: + unittest (module): Single test cases + """ + + def test_set_older_than(self): + """Test for the _set_older_than method""" + + # Test set_older_than method + plexorcist = Plexorcist() + plexorcist.config_file = MagicMock() + plexorcist.config_file.get.return_value = "1d" + older_than = plexorcist._set_older_than() + + # Assertions + self.assertGreater(older_than, 0) @patch("plexorcist.utils.Utils.make_request") - def test_handle_videos(self, mock_make_request): + def test_convert_to_library_ids(self, mock_make_request): + """Test for the conver_to_library_ids + + Args: + mock_make_request (method): Mock the make_request method + """ + # Prepare test data mock_response = MagicMock() - mock_response.content = b'' + mock_response.content = b'' mock_make_request.return_value = mock_response - # Test handle_videos method + # Test convert_to_library_ids method plexorcist = Plexorcist() - plexorcist.pushbullet = MagicMock() - plexorcist.handle_videos(mock_response) + plexorcist.config = {"plex_base": "http://example.com", "plex_token": "token"} + library_ids = plexorcist.convert_to_library_ids(["Cinema", "Series"]) # Assertions - mock_make_request.assert_called_once() + self.assertEqual(library_ids, [1, 2]) @patch("plexorcist.utils.Utils.make_request") def test_get_available_libraries(self, mock_make_request): + """Test for the get_available_libraries method + + Args: + mock_make_request (method): Mock the make_request method + """ + # Prepare test data mock_response = MagicMock() mock_response.content = b'' @@ -35,155 +67,209 @@ def test_get_available_libraries(self, mock_make_request): self.assertEqual(len(libraries), 2) self.assertEqual(libraries[0]["@title"], "Cinema") - @patch("plexorcist.utils.Utils.make_request") - def test_delete_videos(self, mock_make_request): - # Prepare test data - mock_response = MagicMock() - mock_response.content = b'' - mock_make_request.return_value = mock_response + def test_get_library_id_by_name(self): + """Test for the get_library_id_by_name method""" - # Test delete_videos method + # Prepare test data plexorcist = Plexorcist() - plexorcist.config = { - "plex_base": "http://example.com", - "plex_token": "token", - "ifttt_webhook": "", - "i18n": { - "removed": "Removed {0} videos, reclaimed {1} GB", - "notification": "Notification sent", - "ifttt_error": "IFTTT webhook error", - }, - "whitelist": ["Whitelisted Title"], - } - plexorcist.pushbullet = MagicMock() - - watched_videos = [ - { - "@title": "Title", - "@grandparentTitle": "Grandparent", - "@key": "/path/to/video", - "Media": [{"Part": [{"@size": "1024"}]}], - } + available_libraries = [ + {"@title": "Cinema", "@key": "1"}, + {"@title": "Series", "@key": "2"}, ] - plexorcist.delete_videos(watched_videos, "movie") + library_id = plexorcist.get_library_id_by_name("Cinema", available_libraries) # Assertions - mock_make_request.assert_called_once() + self.assertEqual(library_id, 1) - def test_filter_videos(self): - # Test filter_videos method - plexorcist = Plexorcist() - videos = [ - {"@viewCount": "1", "@lastViewedAt": "123"}, - {"@viewCount": "0", "@lastViewedAt": "0"}, + def test_get_library_id_by_name_found(self): + """Test for the get_library_id_by_name when name is found""" + + # Prepare test data + available_libraries = [ + {"@title": "Cinema", "@key": "1"}, + {"@title": "Series", "@key": "2"}, ] - watched_videos = plexorcist.filter_videos(videos) - self.assertEqual(len(watched_videos), 1) - def test_get_library_id_by_name(self): - # Test get_library_id_by_name method + # Test get_library_id_by_name method when library is found plexorcist = Plexorcist() + library_id = plexorcist.get_library_id_by_name("Cinema", available_libraries) + + # Assertions + self.assertEqual(library_id, 1) + + def test_get_library_id_by_name_not_found(self): + """Test for the get_library_id_by_name when name is not found""" + + # Prepare test data available_libraries = [ - {"@title": "Movies", "@key": "1"}, - {"@title": "TV Shows", "@key": "2"}, + {"@title": "Cinema", "@key": "1"}, + {"@title": "Series", "@key": "2"}, ] - library_id = plexorcist.get_library_id_by_name("Movies", available_libraries) - self.assertEqual(library_id, 1) - def test_set_older_than(self): - # Test set_older_than method + # Test get_library_id_by_name method when library is not found plexorcist = Plexorcist() - plexorcist.config_file = MagicMock() - plexorcist.config_file.get.return_value = "1d" - older_than = plexorcist._set_older_than() - self.assertGreater(older_than, 0) + library_id = plexorcist.get_library_id_by_name("Music", available_libraries) + + # Assertions + self.assertIsNone(library_id) @patch("plexorcist.utils.Utils.make_request") - def test_send_notification(self, mock_make_request): + def test_handle_videos(self, mock_make_request): + """Test for the handle_videos method + + Args: + mock_make_request (method): Mock the make_request method + """ + # Prepare test data mock_response = MagicMock() - mock_response.content = b"" + mock_response.content = b'' mock_make_request.return_value = mock_response - # Test send_notification method + # Test handle_videos method plexorcist = Plexorcist() - plexorcist.config = { - "ifttt_webhook": "https://ifttt.com/webhook", - "i18n": { - "removed": "Removed {0} videos, reclaimed {1} GB", - "notification": "Notification sent", - "ifttt_error": "IFTTT webhook error", - }, - } plexorcist.pushbullet = MagicMock() - plexorcist.send_notification(["Video 1", "Video 2"], 0.5) + plexorcist.handle_videos(mock_response) # Assertions mock_make_request.assert_called_once() + def test_filter_videos(self): + """Test for the filter_videos method""" + + # Prepare test data + videos = [ + {"@viewCount": "1", "@lastViewedAt": "123"}, + {"@viewCount": "0", "@lastViewedAt": "0"}, + ] + + # Test filter_videos method + plexorcist = Plexorcist() + watched_videos = plexorcist.filter_videos(videos) + + # Assertions + self.assertEqual(len(watched_videos), 1) + def test_get_title_show(self): + """Test for the get_title method for media type show""" + + # Prepare test data + video = {"@grandparentTitle": "Grandparent", "@title": "Title"} + # Test get_title method for show media type plexorcist = Plexorcist() - video = {"@grandparentTitle": "Grandparent", "@title": "Title"} title = plexorcist.get_title(video, "show") + + # Assertions self.assertEqual(title, "Grandparent - Title") def test_get_title_movie(self): + """Test for get_title method for media type movie""" + + # Prepare test data + video = {"@title": "Title", "@grandparentTitle": "Series 1"} + # Test get_title method for movie media type plexorcist = Plexorcist() - video = {"@title": "Title", "@grandparentTitle": "Series 1"} title = plexorcist.get_title(video, "show") + + # Assertions self.assertEqual(title, "Series 1 - Title") def test_is_whitelisted(self): + """Test for the is_whitelisted method""" + + # Prepare test data + video = {"@title": "Title"} + # Test is_whitelisted method plexorcist = Plexorcist() plexorcist.config = {"whitelist": ["Whitelisted Title"]} - video = {"@title": "Title"} is_whitelisted = plexorcist.is_whitelisted(video, "show") + + # Assertions self.assertFalse(is_whitelisted) def test_get_size(self): + """Test for the get_size method""" + + # Prepare test data + video = {"Media": [{"Part": [{"@size": "1024"}]}]} + # Test get_size method plexorcist = Plexorcist() - video = {"Media": [{"Part": [{"@size": "1024"}]}]} size = plexorcist.get_size(video) + + # Assertions self.assertEqual(size, 0.0) @patch("plexorcist.utils.Utils.make_request") - def test_convert_to_library_ids(self, mock_make_request): + def test_delete_videos(self, mock_make_request): + """Test for the delete_videos method + + Args: + mock_make_request (method): Mock the make_request method + """ + # Prepare test data mock_response = MagicMock() - mock_response.content = b'' + mock_response.content = b'' mock_make_request.return_value = mock_response + watched_videos = [ + { + "@title": "Title", + "@grandparentTitle": "Grandparent", + "@key": "/path/to/video", + "Media": [{"Part": [{"@size": "1024"}]}], + } + ] - # Test convert_to_library_ids method + # Test delete_videos method plexorcist = Plexorcist() - plexorcist.config = {"plex_base": "http://example.com", "plex_token": "token"} - library_ids = plexorcist.convert_to_library_ids(["Movies", "TV Shows"]) + plexorcist.config = { + "plex_base": "http://example.com", + "plex_token": "token", + "ifttt_webhook": "", + "i18n": { + "removed": "Removed {0} videos, reclaimed {1} GB", + "notification": "Notification sent", + "ifttt_error": "IFTTT webhook error", + }, + "whitelist": ["Whitelisted Title"], + } + plexorcist.pushbullet = MagicMock() + plexorcist.delete_videos(watched_videos, "movie") # Assertions - self.assertEqual(library_ids, [1, 2]) + mock_make_request.assert_called_once() - def test_get_library_id_by_name_found(self): - # Test get_library_id_by_name method when library is found - plexorcist = Plexorcist() - available_libraries = [ - {"@title": "Movies", "@key": "1"}, - {"@title": "TV Shows", "@key": "2"}, - ] - library_id = plexorcist.get_library_id_by_name("Movies", available_libraries) - self.assertEqual(library_id, 1) + @patch("plexorcist.utils.Utils.make_request") + def test_send_notification(self, mock_make_request): + """Test for the send_notification method - def test_get_library_id_by_name_not_found(self): - # Test get_library_id_by_name method when library is not found + Args: + mock_make_request (method): Mock the make_request method + """ + # Prepare test data + mock_response = MagicMock() + mock_response.content = b"" + mock_make_request.return_value = mock_response + + # Test send_notification method plexorcist = Plexorcist() - available_libraries = [ - {"@title": "Movies", "@key": "1"}, - {"@title": "TV Shows", "@key": "2"}, - ] - library_id = plexorcist.get_library_id_by_name("Music", available_libraries) - self.assertIsNone(library_id) + plexorcist.config = { + "ifttt_webhook": "https://ifttt.com/webhook", + "i18n": { + "removed": "Removed {0} videos, reclaimed {1} GB", + "notification": "Notification sent", + "ifttt_error": "IFTTT webhook error", + }, + } + plexorcist.pushbullet = MagicMock() + plexorcist.send_notification(["Video 1", "Video 2"], 0.5) + + # Assertions + mock_make_request.assert_called_once() if __name__ == "__main__":