1
0
mirror of https://github.com/spaam/svtplay-dl.git synced 2024-12-05 01:23:44 +01:00
svtplay-dl/lib/svtplay_dl/tests/test_output.py

112 lines
3.6 KiB
Python
Raw Normal View History

#!/usr/bin/python
2013-03-02 21:26:28 +01:00
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
# The unittest framwork doesn't play nice with pylint:
# pylint: disable-msg=C0103
import unittest
2020-09-06 14:19:24 +02:00
from unittest.mock import patch
2019-08-25 00:40:39 +02:00
2018-05-13 13:06:45 +02:00
import svtplay_dl.utils.output
2015-09-15 20:10:32 +02:00
# FIXME: use mock framework instead of this hack
2019-08-25 00:33:51 +02:00
class mockfile:
def __init__(self):
self.content = []
def write(self, string):
self.content.append(string)
def read(self):
return self.content.pop()
2015-09-15 20:10:32 +02:00
class progressTest(unittest.TestCase):
def setUp(self):
self.mockfile = mockfile()
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progress_stream = self.mockfile
2019-08-25 00:27:31 +02:00
@patch("svtplay_dl.utils.output.progressbar")
def test_0_0(self, pbar):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progress(0, 0)
2019-08-31 01:02:59 +02:00
assert not pbar.called
2019-08-25 00:27:31 +02:00
@patch("svtplay_dl.utils.output.progressbar")
def test_0_100(self, pbar):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progress(0, 100)
pbar.assert_any_call(100, 0, "")
2015-09-15 20:10:32 +02:00
class progressbarTest(unittest.TestCase):
def setUp(self):
2018-05-13 13:06:45 +02:00
self.old_termsiz = svtplay_dl.utils.output.get_terminal_size
svtplay_dl.utils.output.get_terminal_size = lambda: (50, 25)
self.mockfile = mockfile()
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progress_stream = self.mockfile
def tearDown(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.get_terminal_size = self.old_termsiz
def test_0_100(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 0)
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[000/100][..........] "
def test_progress_1_100(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 1)
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[001/100][..........] "
def test_progress_2_100(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 2)
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[002/100][..........] "
def test_progress_50_100(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 50)
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[050/100][=====.....] "
def test_progress_100_100(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 100)
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[100/100][==========] "
def test_progress_20_100_msg(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.progressbar(100, 20, "msg")
2019-08-31 01:02:59 +02:00
assert self.mockfile.read() == "\r[020/100][==........] msg"
def test_progress_20_100_termwidth(self):
2018-05-13 13:06:45 +02:00
svtplay_dl.utils.output.get_terminal_size = lambda: (75, 25)
svtplay_dl.utils.output.progressbar(100, 20)
assert self.mockfile.read() == "\r[020/100][=======............................] "
2015-09-15 20:10:32 +02:00
class EtaTest(unittest.TestCase):
2019-08-25 00:27:31 +02:00
@patch("time.time")
def test_eta_0_100(self, mock_time):
mock_time.return_value = float(0)
# Let's make this simple; we'll create something that
# processes one item per second, and make the size be
# 100.
2018-05-13 13:06:45 +02:00
eta = svtplay_dl.utils.output.ETA(100)
2019-08-31 01:02:59 +02:00
assert eta.left == 100 # no progress yet
assert str(eta) == "(unknown)" # no progress yet
2018-01-30 20:11:37 +01:00
mock_time.return_value = float(10) # sleep(10)
eta.update(10)
2019-08-31 01:02:59 +02:00
assert eta.left == 90
assert str(eta) == "0:01:30" # 90 items left, 90s left
mock_time.return_value += 1
2018-01-30 20:11:37 +01:00
eta.increment() # another item completed in one second!
2019-08-31 01:02:59 +02:00
assert eta.left == 89
assert str(eta) == "0:01:29"
mock_time.return_value += 9
2018-01-30 20:11:37 +01:00
eta.increment(9) # another item completed in one second!
2019-08-31 01:02:59 +02:00
assert eta.left == 80
assert str(eta) == "0:01:20"
2018-01-30 20:11:37 +01:00
mock_time.return_value = float(90) # sleep(79)
eta.update(90)
2019-08-31 01:02:59 +02:00
assert eta.left == 10
assert str(eta) == "0:00:10"