mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-09 09:59:51 +01:00
fix mode handling and test
This commit is contained in:
parent
c54c19ac01
commit
8eefc63226
@ -84,15 +84,24 @@ def parse_page(page):
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def remote(printer, category, sort, mode, terms, mirror, timeout):
|
def build_request_path(mode, category, terms):
|
||||||
# special query when no terms
|
if mode == 'search':
|
||||||
if not terms:
|
|
||||||
if category == 0:
|
|
||||||
category = 'all'
|
|
||||||
query = '/precompiled/data_top100_{}.json'.format(category)
|
|
||||||
else:
|
|
||||||
query = '/q.php?q={}&cat={}'.format(' '.join(terms), category)
|
query = '/q.php?q={}&cat={}'.format(' '.join(terms), category)
|
||||||
|
elif mode == 'top':
|
||||||
|
cat = 'all' if category == 0 else category
|
||||||
|
query = '/precompiled/data_top100_{}.json'.format(cat)
|
||||||
|
elif mode == 'recent':
|
||||||
|
query = '/precompiled/data_top100_recent.json'
|
||||||
|
elif mode == 'browse':
|
||||||
|
raise NotImplementedError
|
||||||
|
else:
|
||||||
|
raise Exception('Invalid mode', mode)
|
||||||
|
|
||||||
|
return parse.quote(query, '?=&/')
|
||||||
|
|
||||||
|
|
||||||
|
def remote(printer, category, sort, mode, terms, mirror, timeout):
|
||||||
|
query = build_request_path(mode, category, terms)
|
||||||
# Catch the Ctrl-C exception and exit cleanly
|
# Catch the Ctrl-C exception and exit cleanly
|
||||||
try:
|
try:
|
||||||
req = request.Request(
|
req = request.Request(
|
||||||
|
@ -63,26 +63,27 @@ class TestTorrent(unittest.TestCase):
|
|||||||
self.assertEqual(99, sort)
|
self.assertEqual(99, sort)
|
||||||
|
|
||||||
def test_request_path(self):
|
def test_request_path(self):
|
||||||
# the args are (page, category, sort, mode, terms)
|
# the args are (mode, category, terms)
|
||||||
tests = [
|
succeed = [
|
||||||
((0, 100, 10, 'browse', []), '/browse/100/0/10'),
|
(('recent', 0, []), '/precompiled/data_top100_recent.json'),
|
||||||
((0, 0, 10, 'browse', []), '/browse/100/0/10'),
|
(('recent', 100, []), '/precompiled/data_top100_recent.json'),
|
||||||
((0, 0, 10, 'recent', []), '/top/48hall'),
|
(('top', 0, []), '/precompiled/data_top100_all.json'),
|
||||||
((0, 100, 10, 'recent', []), '/top/48h100'),
|
(('top', 100, []), '/precompiled/data_top100_100.json'),
|
||||||
((0, 100, 10, 'top', []), '/top/100'),
|
(('search', 100, ['abc']), '/q.php?q=abc&cat=100'),
|
||||||
((0, 0, 10, 'top', []), '/top/all'),
|
(('search', 100, ['abc', 'def']), '/q.php?q=abc%20def&cat=100'),
|
||||||
((0, 100, 10, 'search', ['abc']), '/search/abc/0/10/100'),
|
(('search', 100, ['\u1234']), '/q.php?q=%E1%88%B4&cat=100'),
|
||||||
((0, 100, 10, 'search', ['abc', 'def']), '/search/abc+def/0/10/100'),
|
|
||||||
((0, 100, 10, 'search', [u'\u1234']), '/search/%E1%88%B4/0/10/100'),
|
|
||||||
((0, 100, 10, 'asdf', []), Exception),
|
|
||||||
]
|
]
|
||||||
for test in tests:
|
fail = [
|
||||||
if test[1] != Exception:
|
(('browse', 100, []), NotImplementedError),
|
||||||
path = pirate.torrent.build_request_path(*test[0])
|
(('browse', 0, []), NotImplementedError),
|
||||||
self.assertEqual(test[1], path)
|
(('asdf', 100, []), Exception),
|
||||||
else:
|
]
|
||||||
with self.assertRaises(test[1]):
|
for inp, out in succeed:
|
||||||
pirate.torrent.build_request_path(test[0])
|
path = pirate.torrent.build_request_path(*inp)
|
||||||
|
self.assertEqual(out, path)
|
||||||
|
for inp, out, in fail:
|
||||||
|
with self.assertRaises(out):
|
||||||
|
pirate.torrent.build_request_path(*inp)
|
||||||
|
|
||||||
@patch('pirate.torrent.get_torrent')
|
@patch('pirate.torrent.get_torrent')
|
||||||
def test_save_torrents(self, get_torrent):
|
def test_save_torrents(self, get_torrent):
|
||||||
@ -146,7 +147,7 @@ class TestTorrent(unittest.TestCase):
|
|||||||
with patch('urllib.request.Request', return_value=req_obj) as req:
|
with patch('urllib.request.Request', return_value=req_obj) as req:
|
||||||
with patch('urllib.request.urlopen', return_value=res_obj) as res:
|
with patch('urllib.request.urlopen', return_value=res_obj) as res:
|
||||||
results = pirate.torrent.remote(
|
results = pirate.torrent.remote(
|
||||||
MagicMock(Printer), 100, 10, 'browse',
|
MagicMock(Printer), 100, 10, 'top',
|
||||||
[], 'http://example.com', 9)
|
[], 'http://example.com', 9)
|
||||||
req.assert_called_once_with(
|
req.assert_called_once_with(
|
||||||
'http://example.com/precompiled/data_top100_100.json',
|
'http://example.com/precompiled/data_top100_100.json',
|
||||||
|
Loading…
Reference in New Issue
Block a user