Skip to content

Commit

Permalink
Added docustring for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbc committed Feb 21, 2024
1 parent 956b466 commit 8682cbb
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 95 deletions.
2 changes: 1 addition & 1 deletion plexorcist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
274 changes: 180 additions & 94 deletions test_plexorcist.py
Original file line number Diff line number Diff line change
@@ -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'<?xml version="1.0" encoding="UTF-8"?><MediaContainer viewGroup="show"><Video key="12345" viewCount="1" lastViewedAt="123" grandparentTitle="Grandparent" title="Title"><Media><Part size="1024"/></Media></Video></MediaContainer>'
mock_response.content = b'<?xml version="1.0" encoding="UTF-8"?><MediaContainer><Directory title="Cinema" key="1"></Directory><Directory title="Series" key="2"></Directory></MediaContainer>'
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'<?xml version="1.0" encoding="UTF-8"?><MediaContainer><Directory title="Cinema"></Directory><Directory title="Series"></Directory></MediaContainer>'
Expand All @@ -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'<?xml version="1.0" encoding="UTF-8"?><MediaContainer viewGroup="show"><Video viewCount="1" lastViewedAt="123" grandparentTitle="Grandparent" title="Title"><Media><Part size="1024"/></Media></Video></MediaContainer>'
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'<?xml version="1.0" encoding="UTF-8"?><MediaContainer viewGroup="show"><Video key="12345" viewCount="1" lastViewedAt="123" grandparentTitle="Grandparent" title="Title"><Media><Part size="1024"/></Media></Video></MediaContainer>'
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'<?xml version="1.0" encoding="UTF-8"?><MediaContainer><Directory title="Movies" key="1"></Directory><Directory title="TV Shows" key="2"></Directory></MediaContainer>'
mock_response.content = b'<?xml version="1.0" encoding="UTF-8"?><MediaContainer viewGroup="show"><Video viewCount="1" lastViewedAt="123" grandparentTitle="Grandparent" title="Title"><Media><Part size="1024"/></Media></Video></MediaContainer>'
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__":
Expand Down

0 comments on commit 8682cbb

Please sign in to comment.