-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_plexorcist.py
307 lines (243 loc) · 9.6 KB
/
test_plexorcist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/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"
# pylint: disable=protected-access
older_than = plexorcist._set_older_than()
# Assertions
self.assertGreater(older_than, 0)
@patch("plexorcist.utils.Utils.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"?>'
b"<MediaContainer>"
b'<Directory title="Cinema" key="1"></Directory>'
b'<Directory title="Series" key="2"></Directory>'
b"</MediaContainer>"
)
mock_make_request.return_value = mock_response
# Test convert_to_library_ids method
plexorcist = Plexorcist()
plexorcist.config = {"plex_base": "http://example.com", "plex_token": "token"}
library_ids = plexorcist.convert_to_library_ids(["Cinema", "Series"])
# Assertions
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"?>'
b"<MediaContainer>"
b'<Directory title="Cinema"></Directory>'
b'<Directory title="Series"></Directory>'
b"</MediaContainer>"
)
mock_make_request.return_value = mock_response
# Test get_available_libraries method
plexorcist = Plexorcist()
libraries = plexorcist.get_available_libraries()
# Assertions
self.assertEqual(len(libraries), 2)
self.assertEqual(libraries[0]["@title"], "Cinema")
def test_get_library_id_by_name(self):
"""Test for the get_library_id_by_name method"""
# Prepare test data
plexorcist = Plexorcist()
available_libraries = [
{"@title": "Cinema", "@key": "1"},
{"@title": "Series", "@key": "2"},
]
library_id = plexorcist.get_library_id_by_name("Cinema", available_libraries)
# Assertions
self.assertEqual(library_id, 1)
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"},
]
# 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": "Cinema", "@key": "1"},
{"@title": "Series", "@key": "2"},
]
# Test get_library_id_by_name method when library is not found
plexorcist = Plexorcist()
library_id = plexorcist.get_library_id_by_name("Music", available_libraries)
# Assertions
self.assertIsNone(library_id)
@patch("plexorcist.utils.Utils.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'<?xml version="1.0" encoding="UTF-8"?>'
b'<MediaContainer viewGroup="show">'
b'<Video key="1" viewCount="1" lastViewedAt="1" grandparentTitle="GPT" title="Title">'
b"<Media>"
b'<Part size="1024"/>'
b"</Media>"
b"</Video>"
b"</MediaContainer>"
)
mock_make_request.return_value = mock_response
# Test handle_videos method
plexorcist = Plexorcist()
plexorcist.pushbullet = MagicMock()
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()
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()
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"]}
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()
size = plexorcist.get_size(video)
# Assertions
self.assertEqual(size, 0.0)
@patch("plexorcist.utils.Utils.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"?>'
b'<MediaContainer viewGroup="show">'
b'<Video viewCount="1" lastViewedAt="123" grandparentTitle="Grandparent" title="Title">'
b"<Media>"
b'<Part size="1024"/>'
b"</Media>"
b"</Video>"
b"</MediaContainer>"
)
mock_make_request.return_value = mock_response
watched_videos = [
{
"@title": "Title",
"@grandparentTitle": "Grandparent",
"@key": "/path/to/video",
"Media": [{"Part": [{"@size": "1024"}]}],
}
]
# Test delete_videos method
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()
plexorcist.delete_videos(watched_videos, "movie")
# Assertions
mock_make_request.assert_called_once()
@patch("plexorcist.utils.Utils.make_request")
def test_send_notification(self, mock_make_request):
"""Test for the send_notification method
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()
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__":
unittest.main()