From 0c2415bb47554a7e7e4b67bb883ea506fa7ab9f6 Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Thu, 3 Sep 2015 23:29:20 -0700 Subject: [PATCH] test printing results --- pirate/print.py | 4 +++- tests/test_print.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100755 tests/test_print.py diff --git a/pirate/print.py b/pirate/print.py index 04d3a7e..a83b89b 100644 --- a/pirate/print.py +++ b/pirate/print.py @@ -5,6 +5,7 @@ import gzip import colorama import urllib.parse as parse import urllib.request as request +import shutil from io import BytesIO import pirate.data @@ -31,8 +32,9 @@ def print(*args, **kwargs): return builtins.print(*args, **kwargs) +# TODO: extract the name from the search results instead of the magnet link when possible def search_results(results, local=None): - columns = int(os.popen('stty size', 'r').read().split()[1]) + columns = shutil.get_terminal_size((80, 20)).columns cur_color = 'zebra_0' if local: diff --git a/tests/test_print.py b/tests/test_print.py new file mode 100755 index 0000000..438170d --- /dev/null +++ b/tests/test_print.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import unittest +from unittest.mock import patch +from unittest.mock import call + +import pirate.print + + +class TestPrint(unittest.TestCase): + + def test_print_results(self): + with patch('pirate.print.print') as mock: + results = [{ + 'magnet': 'dn=name', + 'seeds': 1, + 'leechers': 2, + 'size': ['3','MiB'], + 'uploaded': 'never' + }] + pirate.print.search_results(results) + actual = mock.call_args_list + expected = [ + call('LINK SEED LEECH RATIO SIZE UPLOAD NAME ', color='header'), + call(' 0 1 2 0.5 3.0 MiB never name ', color='zebra_1'), + ] + self.assertEqual(expected, actual) + +if __name__ == '__main__': + unittest.main()