2013-03-02 00:00:32 +01:00
|
|
|
#!/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 -*-
|
2013-03-02 22:01:25 +01:00
|
|
|
|
|
|
|
# The unittest framwork doesn't play nice with pylint:
|
|
|
|
# pylint: disable-msg=C0103
|
|
|
|
|
2013-03-02 00:00:32 +01:00
|
|
|
from __future__ import absolute_import
|
|
|
|
import unittest
|
2013-03-17 19:55:19 +01:00
|
|
|
import svtplay_dl.output
|
2013-03-23 19:22:01 +01:00
|
|
|
from mock import patch
|
2013-03-02 00:00:32 +01:00
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2013-03-23 19:21:09 +01:00
|
|
|
# FIXME: use mock framework instead of this hack
|
2013-03-02 00:00:32 +01:00
|
|
|
class mockfile(object):
|
|
|
|
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
|
|
|
|
2013-04-19 18:12:36 +02:00
|
|
|
class progressTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.mockfile = mockfile()
|
|
|
|
svtplay_dl.output.progress_stream = self.mockfile
|
|
|
|
|
|
|
|
@patch('svtplay_dl.output.progressbar')
|
|
|
|
def test_0_0(self, pbar):
|
|
|
|
svtplay_dl.output.progress(0, 0)
|
|
|
|
self.assertFalse(pbar.called)
|
|
|
|
|
|
|
|
@patch('svtplay_dl.output.progressbar')
|
|
|
|
def test_0_100(self, pbar):
|
|
|
|
svtplay_dl.output.progress(0, 100)
|
|
|
|
pbar.assert_any_call(100, 0, "")
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2013-03-02 00:00:32 +01:00
|
|
|
class progressbarTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2014-07-24 18:39:15 +02:00
|
|
|
self.old_termsiz = svtplay_dl.output.get_terminal_size
|
|
|
|
svtplay_dl.output.get_terminal_size = lambda: (50, 25)
|
|
|
|
|
2013-03-02 00:00:32 +01:00
|
|
|
self.mockfile = mockfile()
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progress_stream = self.mockfile
|
2013-03-02 00:00:32 +01:00
|
|
|
|
2014-07-24 18:39:15 +02:00
|
|
|
def tearDown(self):
|
|
|
|
svtplay_dl.output.get_terminal_size = self.old_termsiz
|
|
|
|
|
2013-03-02 00:00:32 +01:00
|
|
|
def test_0_100(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 0)
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[000/100][..........] "
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-02 00:00:32 +01:00
|
|
|
|
|
|
|
def test_progress_1_100(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 1)
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[001/100][..........] "
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-02 00:00:32 +01:00
|
|
|
|
|
|
|
def test_progress_2_100(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 2)
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[002/100][..........] "
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-02 00:00:32 +01:00
|
|
|
|
|
|
|
def test_progress_50_100(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 50)
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[050/100][=====.....] "
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-02 00:00:32 +01:00
|
|
|
|
|
|
|
def test_progress_100_100(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 100)
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[100/100][==========] "
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-02 00:00:32 +01:00
|
|
|
|
|
|
|
def test_progress_20_100_msg(self):
|
2013-03-17 19:55:19 +01:00
|
|
|
svtplay_dl.output.progressbar(100, 20, "msg")
|
2013-03-02 00:00:32 +01:00
|
|
|
self.assertEqual(
|
2016-03-28 21:11:32 +02:00
|
|
|
self.mockfile.read(), "\r[020/100][==........] msg"
|
2013-03-23 19:21:09 +01:00
|
|
|
)
|
2013-03-23 19:22:01 +01:00
|
|
|
|
2014-07-24 18:45:46 +02:00
|
|
|
def test_progress_20_100_termwidth(self):
|
|
|
|
svtplay_dl.output.get_terminal_size = lambda: (75, 25)
|
|
|
|
svtplay_dl.output.progressbar(100, 20)
|
|
|
|
self.assertEqual(
|
|
|
|
self.mockfile.read(),
|
2016-03-28 21:11:32 +02:00
|
|
|
"\r[020/100][=======............................] "
|
2014-07-24 18:45:46 +02:00
|
|
|
)
|
|
|
|
|
2015-09-15 20:10:32 +02:00
|
|
|
|
2013-03-23 19:22:01 +01:00
|
|
|
class EtaTest(unittest.TestCase):
|
|
|
|
@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.
|
|
|
|
eta = svtplay_dl.output.ETA(100)
|
2018-01-30 20:11:37 +01:00
|
|
|
self.assertEqual(eta.left, 100) # no progress yet
|
|
|
|
self.assertEqual(str(eta), "(unknown)") # no progress yet
|
2013-03-23 19:22:01 +01:00
|
|
|
|
2018-01-30 20:11:37 +01:00
|
|
|
mock_time.return_value = float(10) # sleep(10)
|
2013-03-23 19:22:01 +01:00
|
|
|
eta.update(10)
|
|
|
|
self.assertEqual(eta.left, 90)
|
2018-01-30 20:11:37 +01:00
|
|
|
self.assertEqual(str(eta), "0:01:30") # 90 items left, 90s left
|
2013-03-23 19:22:01 +01:00
|
|
|
|
|
|
|
mock_time.return_value += 1
|
2018-01-30 20:11:37 +01:00
|
|
|
eta.increment() # another item completed in one second!
|
2013-03-23 19:22:01 +01:00
|
|
|
self.assertEqual(eta.left, 89)
|
|
|
|
self.assertEqual(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!
|
2013-03-23 19:22:01 +01:00
|
|
|
self.assertEqual(eta.left, 80)
|
|
|
|
self.assertEqual(str(eta), "0:01:20")
|
|
|
|
|
2018-01-30 20:11:37 +01:00
|
|
|
mock_time.return_value = float(90) # sleep(79)
|
2013-03-23 19:22:01 +01:00
|
|
|
eta.update(90)
|
|
|
|
self.assertEqual(eta.left, 10)
|
|
|
|
self.assertEqual(str(eta), "0:00:10")
|