MrS0m30n3
8 years ago
3 changed files with 650 additions and 1 deletions
Split View
Diff Options
@ -0,0 +1,204 @@ |
|||
#!/usr/bin/env python |
|||
# -*- coding: UTF-8 -*- |
|||
|
|||
"""Contains test cases for the DownloadItem object.""" |
|||
|
|||
from __future__ import unicode_literals |
|||
|
|||
import sys |
|||
import os.path |
|||
import unittest |
|||
|
|||
PATH = os.path.realpath(os.path.abspath(__file__)) |
|||
sys.path.insert(0, os.path.dirname(os.path.dirname(PATH))) |
|||
|
|||
try: |
|||
from youtube_dl_gui.downloadmanager import DownloadItem |
|||
except ImportError as error: |
|||
print error |
|||
sys.exit(1) |
|||
|
|||
|
|||
class TestItemInit(unittest.TestCase): |
|||
|
|||
"""Test case for DownloadItem init.""" |
|||
|
|||
def test_init(self): |
|||
url = "url" |
|||
options = ["-f", "flv"] |
|||
|
|||
ditem = DownloadItem(url, options) |
|||
|
|||
self.assertEqual(ditem.stage, "Queued") |
|||
self.assertEqual(ditem.url, url) |
|||
self.assertEqual(ditem.options, options) |
|||
self.assertEqual(ditem.object_id, hash(url + unicode(options))) |
|||
|
|||
self.assertEqual(ditem.path, "") |
|||
self.assertEqual(ditem.filenames, []) |
|||
self.assertEqual(ditem.extensions, []) |
|||
|
|||
self.assertEqual( |
|||
ditem.progress_stats, |
|||
{"filename": url, |
|||
"extension": "-", |
|||
"filesize": "-", |
|||
"percent": "0%", |
|||
"speed": "-", |
|||
"eta": "-", |
|||
"status": "Queued"} |
|||
) |
|||
|
|||
|
|||
class TestGetFiles(unittest.TestCase): |
|||
|
|||
"""Test case for DownloadItem get_files method.""" |
|||
|
|||
def setUp(self): |
|||
self.ditem = DownloadItem("url", ["-f", "flv"]) |
|||
|
|||
def test_get_files(self): |
|||
path = os.path.join("/home", "user", "downloads") |
|||
|
|||
self.ditem.path = path |
|||
self.ditem.filenames = ["file1", "file2"] |
|||
self.ditem.extensions = [".mp4", ".m4a"] |
|||
|
|||
self.assertEqual(self.ditem.get_files(), [os.path.join(path, "file1", ".mp4"), os.path.join(path, "file2", ".m4a")]) |
|||
|
|||
def test_get_files_no_data(self): |
|||
self.assertEqual(self.ditem.get_files(), []) |
|||
|
|||
|
|||
class TestItemComparison(unittest.TestCase): |
|||
|
|||
"""Test case for DownloadItem __eq__ method.""" |
|||
|
|||
def test_equal_true(self): |
|||
ditem1 = DownloadItem("url", ["-f", "flv"]) |
|||
ditem2 = DownloadItem("url", ["-f", "flv"]) |
|||
|
|||
self.assertTrue(ditem1 == ditem2) |
|||
|
|||
def test_equal_false(self): |
|||
ditem1 = DownloadItem("url", ["-f", "flv"]) |
|||
ditem2 = DownloadItem("url2", ["-f", "flv"]) |
|||
|
|||
self.assertFalse(ditem1 == ditem2) |
|||
|
|||
ditem1 = DownloadItem("url", ["-f", "flv"]) |
|||
ditem2 = DownloadItem("url", ["-f", "mp4"]) |
|||
|
|||
self.assertFalse(ditem1 == ditem2) |
|||
|
|||
|
|||
class TestSetItemStage(unittest.TestCase): |
|||
|
|||
"""Test case for DownloadItem stage setter.""" |
|||
|
|||
def setUp(self): |
|||
self.ditem = DownloadItem("url", ["-f", "flv"]) |
|||
|
|||
def test_set_stage_valid(self): |
|||
self.ditem.stage = "Queued" |
|||
self.assertEqual(self.ditem.stage, "Queued") |
|||
|
|||
self.ditem.stage = "Active" |
|||
self.assertEqual(self.ditem.stage, "Active") |
|||
|
|||
self.ditem.stage = "Completed" |
|||
self.assertEqual(self.ditem.stage, "Completed") |
|||
|
|||
self.ditem.stage = "Paused" |
|||
self.assertEqual(self.ditem.stage, "Paused") |
|||
|
|||
def test_set_stage_invalid(self): |
|||
raised = False |
|||
|
|||
try: |
|||
self.ditem.stage = "some other status" |
|||
except ValueError: |
|||
raised = True |
|||
|
|||
self.assertTrue(raised) |
|||
|
|||
|
|||
class TestUpdateStats(unittest.TestCase): |
|||
|
|||
"""Test case for DownloadItem update_stats method.""" |
|||
|
|||
def setUp(self): |
|||
self.ditem = DownloadItem("url", ["-f", "flv"]) |
|||
|
|||
def test_update_stats(self): |
|||
path = os.path.join("/home", "user") |
|||
|
|||
self.ditem.update_stats({"filename": "somefilename", |
|||
"extension": ".mp4", |
|||
"filesize": "9.45MiB", |
|||
"percent": "2.0%", |
|||
"speed": "200.00KiB/s", |
|||
"eta": "00:38", |
|||
"status": "Downloading", |
|||
"path": path}) |
|||
|
|||
self.assertEqual(self.ditem.path, path) |
|||
self.assertEqual(self.ditem.filenames, ["somefilename"]) |
|||
self.assertEqual(self.ditem.extensions, [".mp4"]) |
|||
self.assertEqual( |
|||
self.ditem.progress_stats, |
|||
{"filename": "somefilename", |
|||
"extension": ".mp4", |
|||
"filesize": "9.45MiB", |
|||
"percent": "2.0%", |
|||
"speed": "200.00KiB/s", |
|||
"eta": "00:38", |
|||
"status": "Downloading"} |
|||
) |
|||
|
|||
self.ditem.update_stats({"filename": "someotherfilename", "extension": ".m4a"}) |
|||
|
|||
self.assertEqual(self.ditem.filenames, ["somefilename", "someotherfilename"]) |
|||
self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"]) |
|||
self.assertEqual( |
|||
self.ditem.progress_stats, |
|||
{"filename": "someotherfilename", |
|||
"extension": ".m4a", |
|||
"filesize": "9.45MiB", |
|||
"percent": "2.0%", |
|||
"speed": "200.00KiB/s", |
|||
"eta": "00:38", |
|||
"status": "Downloading"} |
|||
) |
|||
|
|||
def test_update_stats_invalid_input(self): |
|||
self.assertRaises(AssertionError, self.ditem.update_stats, []) |
|||
|
|||
|
|||
class TestDownloadItemPrivate(unittest.TestCase): |
|||
|
|||
"""Test case for private method of the DownloadItem.""" |
|||
|
|||
def test_set_stage(self): |
|||
ditem = DownloadItem("url", ["-f", "flv"]) |
|||
|
|||
active_status = ["Pre Processing", "Downloading", "Post Processing"] |
|||
complete_status = ["Finished", "Error", "Warning", "Stopped", "Already Downloaded", "Filesize Abort"] |
|||
|
|||
for status in active_status: |
|||
ditem._stage = "Queued" |
|||
ditem._set_stage(status) |
|||
self.assertEqual(ditem.stage, "Active") |
|||
|
|||
for status in complete_status: |
|||
ditem._stage = "Active" |
|||
ditem._set_stage(status) |
|||
self.assertEqual(ditem.stage, "Completed") |
|||
|
|||
|
|||
def main(): |
|||
unittest.main() |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
main() |
@ -0,0 +1,231 @@ |
|||
#!/usr/bin/env python |
|||
# -*- coding: UTF-8 -*- |
|||
|
|||
"""Contains test cases for the DownloadList object.""" |
|||
|
|||
import sys |
|||
import os.path |
|||
import unittest |
|||
|
|||
PATH = os.path.realpath(os.path.abspath(__file__)) |
|||
sys.path.insert(0, os.path.dirname(os.path.dirname(PATH))) |
|||
|
|||
try: |
|||
import mock |
|||
from youtube_dl_gui.downloadmanager import DownloadList, synchronized |
|||
except ImportError as error: |
|||
print error |
|||
sys.exit(1) |
|||
|
|||
|
|||
class TestInit(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList init.""" |
|||
|
|||
def test_init(self): |
|||
mocks = [mock.Mock(object_id=0), mock.Mock(object_id=1)] |
|||
|
|||
dlist = DownloadList(mocks) |
|||
self.assertEqual(dlist._items_list, [0, 1]) |
|||
self.assertEqual(dlist._items_dict, {0: mocks[0], 1: mocks[1]}) |
|||
|
|||
def test_init_empty(self): |
|||
dlist = DownloadList() |
|||
self.assertEqual(dlist._items_list, []) |
|||
self.assertEqual(dlist._items_dict, {}) |
|||
|
|||
def test_init_invalid_args(self): |
|||
self.assertRaises(AssertionError, DownloadList, {}) |
|||
self.assertRaises(AssertionError, DownloadList, ()) |
|||
self.assertRaises(AssertionError, DownloadList, False) |
|||
|
|||
|
|||
class TestInsert(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList insert method.""" |
|||
|
|||
def test_insert(self): |
|||
mock_ditem = mock.Mock(object_id=0) |
|||
|
|||
dlist = DownloadList() |
|||
dlist.insert(mock_ditem) |
|||
|
|||
self.assertEqual(dlist._items_list, [0]) |
|||
self.assertEqual(dlist._items_dict, {0: mock_ditem}) |
|||
|
|||
|
|||
class TestRemove(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList remove method.""" |
|||
|
|||
def setUp(self): |
|||
self.mocks = [mock.Mock(object_id=0), mock.Mock(object_id=1), mock.Mock(object_id=2)] |
|||
self.dlist = DownloadList(self.mocks) |
|||
|
|||
def test_remove(self): |
|||
self.assertTrue(self.dlist.remove(1)) |
|||
|
|||
self.assertEqual(self.dlist._items_list, [0, 2]) |
|||
self.assertEqual(self.dlist._items_dict, {0: self.mocks[0], 2: self.mocks[2]}) |
|||
|
|||
def test_remove_not_exist(self): |
|||
self.assertRaises(KeyError, self.dlist.remove, 3) |
|||
|
|||
def test_remove_active(self): |
|||
self.mocks[1].stage = "Active" |
|||
|
|||
self.assertFalse(self.dlist.remove(1)) |
|||
self.assertEqual(self.dlist._items_list, [0, 1, 2]) |
|||
self.assertEqual(self.dlist._items_dict, {0: self.mocks[0], 1: self.mocks[1], 2: self.mocks[2]}) |
|||
|
|||
|
|||
class TestFetchNext(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList fetch_next method.""" |
|||
|
|||
def test_fetch_next(self): |
|||
items_count = 3 |
|||
|
|||
mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(items_count)] |
|||
|
|||
dlist = DownloadList(mocks) |
|||
|
|||
for i in range(items_count): |
|||
self.assertEqual(dlist.fetch_next(), mocks[i]) |
|||
mocks[i].stage = "Active" |
|||
|
|||
self.assertIsNone(dlist.fetch_next()) |
|||
|
|||
for i in range(items_count): |
|||
mocks[i].stage = "Completed" |
|||
|
|||
self.assertIsNone(dlist.fetch_next()) |
|||
|
|||
mocks[1].stage = "Queued" # Re-queue item |
|||
self.assertEqual(dlist.fetch_next(), mocks[1]) |
|||
|
|||
def test_fetch_next_empty_list(self): |
|||
dlist = DownloadList() |
|||
self.assertIsNone(dlist.fetch_next()) |
|||
|
|||
|
|||
class TestMoveUp(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList move_up method.""" |
|||
|
|||
def setUp(self): |
|||
mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(3)] |
|||
self.dlist = DownloadList(mocks) |
|||
|
|||
def test_move_up(self): |
|||
self.dlist.move_up(1) |
|||
self.assertEqual(self.dlist._items_list, [1, 0, 2]) |
|||
|
|||
def test_move_up_already_on_top(self): |
|||
self.dlist.move_up(0) |
|||
self.assertEqual(self.dlist._items_list, [0, 1, 2]) |
|||
|
|||
def test_move_up_not_exist(self): |
|||
self.assertRaises(ValueError, self.dlist.move_up, 666) |
|||
|
|||
|
|||
class TestMoveDown(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList move_down method.""" |
|||
|
|||
def setUp(self): |
|||
mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(3)] |
|||
self.dlist = DownloadList(mocks) |
|||
|
|||
def test_move_down(self): |
|||
self.dlist.move_down(1) |
|||
self.assertEqual(self.dlist._items_list, [0, 2, 1]) |
|||
|
|||
def test_move_down_already_on_bottom(self): |
|||
self.dlist.move_down(2) |
|||
self.assertEqual(self.dlist._items_list, [0, 1, 2]) |
|||
|
|||
def test_move_down_not_exist(self): |
|||
self.assertRaises(ValueError, self.dlist.move_down, 666) |
|||
|
|||
|
|||
class TestGetItem(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList get_item method.""" |
|||
|
|||
def test_get_item(self): |
|||
mocks = [mock.Mock(object_id=i) for i in range(3)] |
|||
dlist = DownloadList(mocks) |
|||
|
|||
self.assertEqual(dlist.get_item(0), mocks[0]) |
|||
self.assertEqual(dlist.get_item(2), mocks[2]) |
|||
|
|||
def test_get_item_not_exist(self): |
|||
dlist = DownloadList() |
|||
self.assertRaises(KeyError, dlist.get_item, 0) |
|||
|
|||
|
|||
class TestGetLength(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList __len__ method.""" |
|||
|
|||
def test_get_length(self): |
|||
dlist = DownloadList([mock.Mock(), mock.Mock()]) |
|||
self.assertEqual(len(dlist), 2) |
|||
|
|||
def test_get_length_empty_list(self): |
|||
dlist = DownloadList() |
|||
self.assertEqual(len(dlist), 0) |
|||
|
|||
|
|||
class TestHasItem(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList has_item method.""" |
|||
|
|||
def setUp(self): |
|||
mock_ditem = mock.Mock(object_id=1337) |
|||
self.dlist = DownloadList([mock_ditem]) |
|||
|
|||
def test_has_item_true(self): |
|||
self.assertTrue(self.dlist.has_item(1337)) |
|||
|
|||
def test_has_item_false(self): |
|||
self.assertFalse(self.dlist.has_item(1000)) |
|||
|
|||
|
|||
class TestGetItems(unittest.TestCase): |
|||
|
|||
"""Test case for the DownloadList get_items method.""" |
|||
|
|||
def test_get_items(self): |
|||
mocks = [mock.Mock() for _ in range(3)] |
|||
dlist = DownloadList(mocks) |
|||
|
|||
self.assertEqual(dlist.get_items(), mocks) |
|||
|
|||
def test_get_items_empty_list(self): |
|||
dlist = DownloadList() |
|||
self.assertEqual(dlist.get_items(), []) |
|||
|
|||
|
|||
class TestSynchronizeDecorator(unittest.TestCase): |
|||
|
|||
def test_synchronize(self): |
|||
mock_func = mock.Mock() |
|||
mock_lock = mock.Mock() |
|||
|
|||
decorated_func = synchronized(mock_lock)(mock_func) |
|||
|
|||
self.assertEqual(decorated_func(1, a=2), mock_func.return_value) |
|||
|
|||
mock_func.assert_called_once_with(1, a=2) |
|||
mock_lock.acquire.assert_called_once() |
|||
mock_lock.release.assert_called_once() |
|||
|
|||
|
|||
def main(): |
|||
unittest.main() |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
main() |
Write
Preview
Loading…
Cancel
Save