1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-09 09:59:51 +01:00
pirate-get/tests/test_print.py

159 lines
5.5 KiB
Python
Raw Permalink Normal View History

2015-09-04 08:29:20 +02:00
#!/usr/bin/env python3
import os
2015-09-04 08:29:20 +02:00
import unittest
2020-05-21 14:21:55 +02:00
import json
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
from pirate.print import Printer
2015-09-04 08:29:20 +02:00
class TestPrint(unittest.TestCase):
@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):
class MockTable:
add_row = MagicMock()
align = {}
mock = MockTable()
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'
}]
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 23:25:37 +02:00
def test_print_results_local(self):
class MockTable:
add_row = MagicMock()
align = {}
mock = MockTable()
printer = Printer(False)
printer.print = MagicMock()
2020-05-21 14:21:55 +02:00
with patch('veryprettytable.VeryPrettyTable',
return_value=mock) as prettytable:
results = [{
2020-05-21 14:21:55 +02:00
'name': 'name1',
'date': '1',
'size': '1',
2020-05-21 14:21:55 +02:00
}, {
'name': 'name2',
'date': '2',
'size': '2',
}]
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')
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')
mock_print.assert_called_once_with(
'\x1b[34mabc', '\x1b[0m',
file=sys.stderr)
2015-09-21 00:01:57 +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',
'date': '1',
'size': '1',
2020-05-21 14:21:55 +02:00
}, {
'name': 'name2',
'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
def test_print_descriptions(self):
printer = Printer(False)
printer.print = MagicMock()
2020-05-21 14:21:55 +02:00
class MockRequest():
add_header = MagicMock()
request_obj = MockRequest()
2020-05-21 14:21:55 +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'}))
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')])
def test_print_file_lists(self):
printer = Printer(False)
printer.print = MagicMock()
2020-05-21 14:21:55 +02:00
class MockRequest():
add_header = MagicMock()
2020-05-21 14:21:55 +02:00
info = MagicMock()
request_obj = MockRequest()
2020-05-21 14:21:55 +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]}]))
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-04 08:29:20 +02:00
if __name__ == '__main__':
unittest.main()