From 720437cfcb3b7172a0cd0587948d5b808b997430 Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Sun, 25 Sep 2016 12:58:54 +0300 Subject: [PATCH] Add reset method in DownloadItem --- tests/test_ditem.py | 65 +++++++++++++++++++++++++++++++ youtube_dl_gui/downloadmanager.py | 35 ++++++++++------- 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/tests/test_ditem.py b/tests/test_ditem.py index 66cd758..56ad85c 100644 --- a/tests/test_ditem.py +++ b/tests/test_ditem.py @@ -200,6 +200,71 @@ class TestDownloadItemPrivate(unittest.TestCase): self.assertEqual(ditem.stage, "Completed") +class TestReset(unittest.TestCase): + + """Test case for the DownloadItem reset method.""" + + def setUp(self): + self.ditem = DownloadItem("url", ["-f", "flv"]) + + def test_reset_error_status(self): + self.ditem._stage = "Completed" + self.ditem.path = os.path.join("/home", "user") + self.ditem.filenames = ["file1", "file2", "file"] + self.ditem.extensions = [".mp4", ".m4a", ".mp4"] + self.ditem.progress_stats = { + "filename": "file", + "extension": ".mp4", + "filsize": "9.45MiB", + "percent": "100%", + "speed": "-", + "eta": "00:00", + "status": "Error" + } + + self.ditem.reset() + + self.assertEqual(self.ditem._stage, "Queued") + self.assertEqual(self.ditem.path, "") + self.assertEqual(self.ditem.filenames, []) + self.assertEqual(self.ditem.extensions, []) + self.assertEqual( + self.ditem.progress_stats, + {"filename": "url", + "extension": "-", + "filesize": "-", + "percent": "0%", + "speed": "-", + "eta": "-", + "status": "Queued"} + ) + + def test_reset_paused_stage(self): + self.ditem._stage = "Paused" + # No need to change filanames, extension, etc + # since everything in pause state has the default value + + self.ditem.reset() + self.assertEqual(self.ditem._stage, "Queued") + + def test_reset_active_stage(self): + self.ditem._stage = "Active" + self.ditem.path = os.path.join("/home", "user") + self.ditem.filenames = ["file1"] + self.ditem.extensions = [".mp4"] + self.ditem.progress_stats = { + "filename": "file1", + "extension": ".mp4", + "filsize": "9.45MiB", + "percent": "75.5%", + "speed": "200.00KiB/s", + "eta": "00:10", + "status": "Downloading" + } + + self.assertRaises(RuntimeError, self.ditem.reset) + + def main(): unittest.main() diff --git a/youtube_dl_gui/downloadmanager.py b/youtube_dl_gui/downloadmanager.py index e38ba54..05446f1 100644 --- a/youtube_dl_gui/downloadmanager.py +++ b/youtube_dl_gui/downloadmanager.py @@ -89,23 +89,9 @@ class DownloadItem(object): def __init__(self, url, options): self.url = url self.options = options - self._stage = self.STAGES[0] - self.object_id = hash(url + to_string(options)) - self.path = "" - self.filenames = [] - self.extensions = [] - - self.progress_stats = { - "filename": url, - "extension": "-", - "filesize": "-", - "percent": "0%", - "speed": "-", - "eta": "-", - "status": self.stage - } + self.reset() @property def stage(self): @@ -127,6 +113,25 @@ class DownloadItem(object): self._stage = value + def reset(self): + if hasattr(self, "_stage") and self._stage == self.STAGES[1]: + raise RuntimeError("Cannot reset an 'Active' item") + + self._stage = self.STAGES[0] + self.path = "" + self.filenames = [] + self.extensions = [] + + self.progress_stats = { + "filename": self.url, + "extension": "-", + "filesize": "-", + "percent": "0%", + "speed": "-", + "eta": "-", + "status": self.stage + } + def get_files(self): """Returns a list that contains all the system files bind to this object.""" files = []