2015-09-04 08:29:20 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-03-10 15:39:23 +01:00
|
|
|
import os
|
2015-09-04 08:29:20 +02:00
|
|
|
import unittest
|
2020-05-21 14:21:55 +02:00
|
|
|
import json
|
2020-05-22 10:35:42 +02:00
|
|
|
import sys
|
2015-09-04 08:29:20 +02:00
|
|
|
|
2020-05-21 14:21:55 +02:00
|
|
|
from unittest.mock import patch, call, MagicMock
|
2015-09-20 23:14:00 +02:00
|
|
|
from pirate.print import Printer
|
2015-09-04 08:29:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestPrint(unittest.TestCase):
|
2020-03-10 15:39:23 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
# needed to display the results table
|
|
|
|
os.environ['COLUMNS'] = '80'
|
2015-09-04 08:29:20 +02:00
|
|
|
|
2015-09-20 23:25:37 +02:00
|
|
|
def test_print_results_remote(self):
|
2015-09-20 08:29:12 +02:00
|
|
|
class MockTable:
|
|
|
|
add_row = MagicMock()
|
|
|
|
align = {}
|
|
|
|
mock = MockTable()
|
2015-09-20 23:14:00 +02:00
|
|
|
printer = Printer(False)
|
|
|
|
printer.print = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('veryprettytable.VeryPrettyTable',
|
|
|
|
return_value=mock) as prettytable:
|
2015-09-04 08:29:20 +02:00
|
|
|
results = [{
|
2020-05-21 14:21:55 +02:00
|
|
|
'name': 'name',
|
|
|
|
'seeders': 1,
|
2015-09-04 08:29:20 +02:00
|
|
|
'leechers': 2,
|
2020-05-21 14:21:55 +02:00
|
|
|
'size': '3.0 MiB',
|
2015-09-04 08:29:20 +02:00
|
|
|
'uploaded': 'never'
|
|
|
|
}]
|
2015-09-20 23:14:00 +02:00
|
|
|
printer.search_results(results)
|
2020-05-21 14:21:55 +02:00
|
|
|
prettytable.assert_called_once_with([
|
|
|
|
'LINK', 'SEED', 'LEECH', 'RATIO',
|
|
|
|
'SIZE', 'UPLOAD', 'NAME'])
|
|
|
|
mock.add_row.assert_has_calls([
|
|
|
|
call([0, 1, 2, '0.5', '3.0 MiB', 'never', 'name'])])
|
2015-09-20 08:29:12 +02:00
|
|
|
|
2015-09-20 23:25:37 +02:00
|
|
|
def test_print_results_local(self):
|
2015-09-20 08:29:12 +02:00
|
|
|
class MockTable:
|
|
|
|
add_row = MagicMock()
|
|
|
|
align = {}
|
|
|
|
mock = MockTable()
|
2015-09-20 23:14:00 +02:00
|
|
|
printer = Printer(False)
|
|
|
|
printer.print = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('veryprettytable.VeryPrettyTable',
|
|
|
|
return_value=mock) as prettytable:
|
2015-09-20 08:29:12 +02:00
|
|
|
results = [{
|
2020-05-21 14:21:55 +02:00
|
|
|
'name': 'name1',
|
2018-05-28 07:46:14 +02:00
|
|
|
'date': '1',
|
|
|
|
'size': '1',
|
2020-05-21 14:21:55 +02:00
|
|
|
}, {
|
|
|
|
'name': 'name2',
|
2018-05-28 07:46:14 +02:00
|
|
|
'date': '2',
|
|
|
|
'size': '2',
|
2015-09-20 08:29:12 +02:00
|
|
|
}]
|
2015-09-20 23:14:00 +02:00
|
|
|
printer.search_results(results, local=True)
|
2020-05-21 14:21:55 +02:00
|
|
|
prettytable.assert_called_once_with(
|
|
|
|
['LINK', 'DATE', 'SIZE', 'NAME'])
|
|
|
|
mock.add_row.assert_has_calls(
|
|
|
|
[call([0, '1', '1', 'name1']), call([1, '2', '2', 'name2'])])
|
2015-09-20 23:47:20 +02:00
|
|
|
|
2015-09-21 00:01:57 +02:00
|
|
|
def test_print_color(self):
|
|
|
|
printer = Printer(False)
|
|
|
|
with patch('pirate.print.builtins.print') as mock_print:
|
|
|
|
printer.print('abc', color='zebra_1')
|
2020-05-22 10:35:42 +02:00
|
|
|
mock_print.assert_called_once_with(
|
|
|
|
'abc',
|
|
|
|
file=sys.stderr)
|
2015-09-21 00:01:57 +02:00
|
|
|
printer = Printer(True)
|
|
|
|
with patch('pirate.print.builtins.print') as mock_print:
|
|
|
|
printer.print('abc', color='zebra_1')
|
2020-05-22 10:35:42 +02:00
|
|
|
mock_print.assert_called_once_with(
|
|
|
|
'\x1b[34mabc', '\x1b[0m',
|
|
|
|
file=sys.stderr)
|
2015-09-21 00:01:57 +02:00
|
|
|
|
2018-05-28 07:46:14 +02:00
|
|
|
def test_print_results_local2(self):
|
2015-09-20 23:47:20 +02:00
|
|
|
class MockTable:
|
|
|
|
add_row = MagicMock()
|
|
|
|
align = {}
|
|
|
|
mock = MockTable()
|
|
|
|
printer = Printer(True)
|
|
|
|
printer.print = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
with patch('veryprettytable.VeryPrettyTable',
|
|
|
|
return_value=mock) as prettytable:
|
2015-09-20 23:47:20 +02:00
|
|
|
results = [{
|
2020-05-21 14:21:55 +02:00
|
|
|
'name': 'name1',
|
2018-05-28 07:46:14 +02:00
|
|
|
'date': '1',
|
|
|
|
'size': '1',
|
2020-05-21 14:21:55 +02:00
|
|
|
}, {
|
|
|
|
'name': 'name2',
|
2018-05-28 07:46:14 +02:00
|
|
|
'date': '2',
|
|
|
|
'size': '2',
|
2015-09-20 23:47:20 +02:00
|
|
|
}]
|
|
|
|
printer.search_results(results, local=True)
|
2020-05-21 14:21:55 +02:00
|
|
|
prettytable.assert_called_once_with(
|
|
|
|
['LINK', 'DATE', 'SIZE', 'NAME'])
|
|
|
|
mock.add_row.assert_has_calls([
|
|
|
|
call([0, '1', '1', 'name1']),
|
|
|
|
call([1, '2', '2', 'name2'], fore_color='blue')])
|
2015-09-04 08:29:20 +02:00
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
def test_print_descriptions(self):
|
|
|
|
printer = Printer(False)
|
|
|
|
printer.print = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
class MockRequest():
|
|
|
|
add_header = MagicMock()
|
|
|
|
request_obj = MockRequest()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
class MockResponse():
|
2020-05-21 14:21:55 +02:00
|
|
|
read = MagicMock(return_value=json.dumps(
|
|
|
|
{'name': 'cool torrent',
|
|
|
|
'descr': 'A fake torrent.\n'}))
|
2015-09-20 23:40:41 +02:00
|
|
|
info = MagicMock()
|
|
|
|
response_obj = MockResponse()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
|
|
|
with patch('urllib.request.Request', return_value=request_obj):
|
|
|
|
with patch('urllib.request.urlopen',
|
|
|
|
return_value=response_obj):
|
|
|
|
printer.descriptions([0], [{'id': '1', 'name': 'name'}],
|
|
|
|
'example.com', 9)
|
|
|
|
printer.print.assert_has_calls([
|
|
|
|
call('Description for "name":', color='zebra_1'),
|
|
|
|
call('A fake torrent.\n', color='zebra_0')])
|
2015-09-20 23:40:41 +02:00
|
|
|
|
|
|
|
def test_print_file_lists(self):
|
|
|
|
printer = Printer(False)
|
|
|
|
printer.print = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
class MockRequest():
|
|
|
|
add_header = MagicMock()
|
2020-05-21 14:21:55 +02:00
|
|
|
info = MagicMock()
|
2015-09-20 23:40:41 +02:00
|
|
|
request_obj = MockRequest()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
class MockResponse():
|
2020-05-21 14:21:55 +02:00
|
|
|
read = MagicMock(return_value=json.dumps(
|
|
|
|
[{'name': ['readme.txt'], 'size': [16]},
|
|
|
|
{'name': ['a.mkv'], 'size': [677739464]},
|
|
|
|
{'name': ['b.nfo'], 'size': [61]}]))
|
2015-09-20 23:40:41 +02:00
|
|
|
info = MagicMock()
|
|
|
|
response_obj = MockResponse()
|
2020-05-21 14:21:55 +02:00
|
|
|
|
|
|
|
with patch('urllib.request.Request',
|
|
|
|
return_value=request_obj):
|
|
|
|
with patch('urllib.request.urlopen',
|
|
|
|
return_value=response_obj):
|
|
|
|
printer.file_lists([0], [{'id': '1', 'name': 'name'}],
|
|
|
|
'example.com', 9)
|
|
|
|
printer.print.assert_has_calls([
|
|
|
|
call('Files in name:', color='zebra_1'),
|
|
|
|
call(' 16 B readme.txt', color='zebra_0'),
|
|
|
|
call(' 646.3 MiB a.mkv', color='zebra_1'),
|
|
|
|
call(' 61 B b.nfo', color='zebra_0')])
|
|
|
|
|
2015-09-20 23:40:41 +02:00
|
|
|
|
2015-09-04 08:29:20 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|