-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
136 lines (105 loc) · 3.85 KB
/
test.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
import json
import os
import re
import unittest
import unittest.mock as mock
import media_subscriptions as ms
from youtube_dl.extractor.common import InfoExtractor
class PlaylistIE(InfoExtractor):
_VALID_URL = r'pl:([\d,]+)'
def _real_extract(self, url):
ns = map(int, re.match(self._VALID_URL, url).group(1).split(','))
return {
'_type': 'playlist',
'id': 'pl',
'title': 'Playlist',
'entries': reversed([{'url': 't:{}'.format(x)} for x in ns]),
}
lasts_filename = 'test_lasts.json'
db_filename = 'test_db.db'
test_files = [db_filename, lasts_filename, lasts_filename + '.backup']
class TestMediaSubs(unittest.TestCase):
def setUp(self):
for f in test_files:
if os.path.exists(f):
os.remove(f)
self.config = ms.build_config()
self.config.add_section('test')
self.dl = ms.SubscriptionDownloader(self.config)
self.dl.db_filename = db_filename
self.dl.lasts_filename = lasts_filename
self.dl.add_info_extractor(PlaylistIE())
self.dl.run_youtube_dl = mock.Mock()
self.dl_mock = self.dl.download_entry = mock.Mock(side_effect=self.dl.download_entry)
def tearDown(self):
for f in test_files:
if os.path.exists(f):
os.remove(f)
def download_subs(self, *ns):
self.config['test']['url'] = 'pl:{}'.format(','.join(map(str, ns)))
self.dl.download_subscriptions(['test'])
def entry(self, n):
return 'test', {'url': 't:{}'.format(n)}, self.config['test']
def call_entry(self, n):
return mock.call(*self.entry(n))
def test_subscriptions(self):
entry = self.entry
call_entry = self.call_entry
dl_mock = self.dl_mock
self.download_subs(1)
dl_mock.assert_called_with(*entry(1))
dl_mock.reset_mock()
self.download_subs(1)
dl_mock.assert_not_called()
dl_mock.reset_mock()
self.download_subs(1, 2, 3)
dl_mock.assert_has_calls([call_entry(2), call_entry(3)])
def test_first_time(self):
'''Test first time download
It must only download the most recent video
'''
entry = self.entry
dl_mock = self.dl_mock
self.download_subs(1, 2, 3, 4)
dl_mock.assert_called_once_with(*entry(4))
def test_json_migration(self):
"""Test json to sqlite database migration"""
with open(lasts_filename, 'wt') as f:
json.dump({'test': 't:3'}, f)
entry = self.entry
dl_mock = self.dl_mock
self.download_subs(2, 3, 4)
dl_mock.assert_called_once_with(*entry(4))
def test_deleted(self):
"""Test with deleded videos"""
entry = self.entry
call_entry = self.call_entry
dl_mock = self.dl_mock
self.download_subs(1)
dl_mock.assert_called_once_with(*entry(1))
self.download_subs(1, 2, 3)
dl_mock.assert_has_calls([call_entry(2), call_entry(3)])
dl_mock.reset_mock()
self.download_subs(1, 2, 4)
dl_mock.assert_called_once_with(*entry(4))
def test_clean_db(self):
call_entry = self.call_entry
dl_mock = self.dl_mock
self.download_subs(1)
self.download_subs(*list(range(20)))
dl_mock.reset_mock()
self.dl.clean_db(['test'])
self.download_subs(*list(range(22)))
dl_mock.assert_has_calls([call_entry(20), call_entry(21)])
def test_delete(self):
entry = self.entry
dl_mock = self.dl_mock
self.download_subs(1, 2, 3, 4)
dl_mock.assert_called_once_with(*entry(4))
dl_mock.reset_mock()
dl_mock.reset_mock()
self.dl.delete(['test'])
self.download_subs(1, 2, 3, 4)
dl_mock.assert_called_once_with(*entry(4))
if __name__ == '__main__':
unittest.main()