forked from mtshrmn/horrible-downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
87 lines (74 loc) · 3.16 KB
/
tests.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
from httmock import urlmatch, HTTMock
import unittest
from HorribleDownloader import Parser, cmd
from urllib.parse import parse_qs
import itertools
@urlmatch(scheme="https", netloc="horriblesubs.info", path="/current-season/")
def current_season_mock(url, request):
with open("html-mocks/shows.html", "r") as html:
return html.read()
@urlmatch(scheme="https", netloc="horriblesubs.info", path="/shows/")
def shows_mock(url, request):
with open("html-mocks/shows.html", "r") as html:
return html.read()
@urlmatch(scheme="https", netloc="horriblesubs.info", path="/api.php")
def api_mock(url, request):
query = parse_qs(url.query)
if query["type"][0] == "show":
with open("html-mocks/api-show.html", "r") as html:
# Enen no Shouboutai episodes 1-4
# showID 1274
return html.read()
elif query["type"][0] == "batch":
with open("html-mocks/api-batch.html", "r") as html:
# One Punch Man batch 1-12
# return batch 351
return html.read()
@urlmatch(scheme="https", netloc="horriblesubs.info", path="/api.php")
def showid_mock(url, request):
return "var hs_showid = 123456789"
class MockTest(unittest.TestCase):
def setUp(self):
self.parser = Parser()
def test_current_season(self):
with HTTMock(current_season_mock):
current = self.parser.current_shows
self.assertEqual(list(current), ["Test 1", "Test 2", "Test 3", "Test 4"])
def test_shows_list(self):
with HTTMock(shows_mock):
shows = self.parser.shows
self.assertEqual(shows, {
'Test 1': 'test-1',
'Test 2': 'test-2',
'Test 3': 'test-3',
'Test 4': 'test-4'
})
def test_get_episodes(self):
with HTTMock(api_mock):
episodes = self.parser.get_episodes("test", limit=4)
self.assertEqual(len(episodes), 4)
batch = self.parser.get_batches("test")
self.assertEqual(batch[0]["title"].lower(), "one-punch man")
def test_show_id(self):
with HTTMock(showid_mock):
showid = self.parser._get_show_id("doesn't matter")
self.assertTrue(showid, 123456789)
class CMDTest(unittest.TestCase):
def test_quality_verification(self):
for r in range(1, 3):
for qualities in itertools.combinations(["480", "720", "1080"], r):
self.assertTrue(cmd.verify_quality(qualities))
def test_episode_filter_generation(self):
queries = ["1,2,3,4", "1,3,5-7", "=<3,9>"]
episodes = [[1, 3, 4.5, 5], [0.5, 1, 2, 5, 6], [0, 0.1, 0.5, 2.9, 3, 5, 9, 10.5]]
answers = [[1, 3], [1, 5, 6], [0, 0.1, 0.5, 2.9, 3, 10.5]]
for q, e, a in zip(queries, episodes, answers):
f = cmd.generate_episode_filter(q)
filtered = list(filter(f, e))
self.assertEqual(filtered, a)
def test_dict_flat(self):
o = {"foo": [1, 2, 3, 4, 5],
"bar": ["a", "b", "c", "d"],
"baz": [6, "e"]}
o_flat = cmd.flatten_dict(o)
self.assertEqual(o_flat, [5, 4, 3, 2, 1, "d", "c", "b", "a", "e", 6])