You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.1 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """Contains test cases for the DownloadList object."""
  4. import sys
  5. import os.path
  6. import unittest
  7. PATH = os.path.realpath(os.path.abspath(__file__))
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(PATH)))
  9. try:
  10. import mock
  11. from youtube_dl_gui.downloadmanager import DownloadList, synchronized
  12. except ImportError as error:
  13. print error
  14. sys.exit(1)
  15. class TestInit(unittest.TestCase):
  16. """Test case for the DownloadList init."""
  17. def test_init(self):
  18. mocks = [mock.Mock(object_id=0), mock.Mock(object_id=1)]
  19. dlist = DownloadList(mocks)
  20. self.assertEqual(dlist._items_list, [0, 1])
  21. self.assertEqual(dlist._items_dict, {0: mocks[0], 1: mocks[1]})
  22. def test_init_empty(self):
  23. dlist = DownloadList()
  24. self.assertEqual(dlist._items_list, [])
  25. self.assertEqual(dlist._items_dict, {})
  26. def test_init_invalid_args(self):
  27. self.assertRaises(AssertionError, DownloadList, {})
  28. self.assertRaises(AssertionError, DownloadList, ())
  29. self.assertRaises(AssertionError, DownloadList, False)
  30. class TestInsert(unittest.TestCase):
  31. """Test case for the DownloadList insert method."""
  32. def test_insert(self):
  33. mock_ditem = mock.Mock(object_id=0)
  34. dlist = DownloadList()
  35. dlist.insert(mock_ditem)
  36. self.assertEqual(dlist._items_list, [0])
  37. self.assertEqual(dlist._items_dict, {0: mock_ditem})
  38. class TestRemove(unittest.TestCase):
  39. """Test case for the DownloadList remove method."""
  40. def setUp(self):
  41. self.mocks = [mock.Mock(object_id=0), mock.Mock(object_id=1), mock.Mock(object_id=2)]
  42. self.dlist = DownloadList(self.mocks)
  43. def test_remove(self):
  44. self.assertTrue(self.dlist.remove(1))
  45. self.assertEqual(self.dlist._items_list, [0, 2])
  46. self.assertEqual(self.dlist._items_dict, {0: self.mocks[0], 2: self.mocks[2]})
  47. def test_remove_not_exist(self):
  48. self.assertRaises(KeyError, self.dlist.remove, 3)
  49. def test_remove_active(self):
  50. self.mocks[1].stage = "Active"
  51. self.assertFalse(self.dlist.remove(1))
  52. self.assertEqual(self.dlist._items_list, [0, 1, 2])
  53. self.assertEqual(self.dlist._items_dict, {0: self.mocks[0], 1: self.mocks[1], 2: self.mocks[2]})
  54. class TestFetchNext(unittest.TestCase):
  55. """Test case for the DownloadList fetch_next method."""
  56. def test_fetch_next(self):
  57. items_count = 3
  58. mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(items_count)]
  59. dlist = DownloadList(mocks)
  60. for i in range(items_count):
  61. self.assertEqual(dlist.fetch_next(), mocks[i])
  62. mocks[i].stage = "Active"
  63. self.assertIsNone(dlist.fetch_next())
  64. for i in range(items_count):
  65. mocks[i].stage = "Completed"
  66. self.assertIsNone(dlist.fetch_next())
  67. mocks[1].stage = "Queued" # Re-queue item
  68. self.assertEqual(dlist.fetch_next(), mocks[1])
  69. def test_fetch_next_empty_list(self):
  70. dlist = DownloadList()
  71. self.assertIsNone(dlist.fetch_next())
  72. class TestMoveUp(unittest.TestCase):
  73. """Test case for the DownloadList move_up method."""
  74. def setUp(self):
  75. mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(3)]
  76. self.dlist = DownloadList(mocks)
  77. def test_move_up(self):
  78. self.assertTrue(self.dlist.move_up(1))
  79. self.assertEqual(self.dlist._items_list, [1, 0, 2])
  80. def test_move_up_already_on_top(self):
  81. self.assertFalse(self.dlist.move_up(0))
  82. self.assertEqual(self.dlist._items_list, [0, 1, 2])
  83. def test_move_up_not_exist(self):
  84. self.assertRaises(ValueError, self.dlist.move_up, 666)
  85. class TestMoveDown(unittest.TestCase):
  86. """Test case for the DownloadList move_down method."""
  87. def setUp(self):
  88. mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(3)]
  89. self.dlist = DownloadList(mocks)
  90. def test_move_down(self):
  91. self.assertTrue(self.dlist.move_down(1))
  92. self.assertEqual(self.dlist._items_list, [0, 2, 1])
  93. def test_move_down_already_on_bottom(self):
  94. self.assertFalse(self.dlist.move_down(2))
  95. self.assertEqual(self.dlist._items_list, [0, 1, 2])
  96. def test_move_down_not_exist(self):
  97. self.assertRaises(ValueError, self.dlist.move_down, 666)
  98. class TestGetItem(unittest.TestCase):
  99. """Test case for the DownloadList get_item method."""
  100. def test_get_item(self):
  101. mocks = [mock.Mock(object_id=i) for i in range(3)]
  102. dlist = DownloadList(mocks)
  103. self.assertEqual(dlist.get_item(0), mocks[0])
  104. self.assertEqual(dlist.get_item(2), mocks[2])
  105. def test_get_item_not_exist(self):
  106. dlist = DownloadList()
  107. self.assertRaises(KeyError, dlist.get_item, 0)
  108. class TestGetLength(unittest.TestCase):
  109. """Test case for the DownloadList __len__ method."""
  110. def test_get_length(self):
  111. dlist = DownloadList([mock.Mock(), mock.Mock()])
  112. self.assertEqual(len(dlist), 2)
  113. def test_get_length_empty_list(self):
  114. dlist = DownloadList()
  115. self.assertEqual(len(dlist), 0)
  116. class TestHasItem(unittest.TestCase):
  117. """Test case for the DownloadList has_item method."""
  118. def setUp(self):
  119. mock_ditem = mock.Mock(object_id=1337)
  120. self.dlist = DownloadList([mock_ditem])
  121. def test_has_item_true(self):
  122. self.assertTrue(self.dlist.has_item(1337))
  123. def test_has_item_false(self):
  124. self.assertFalse(self.dlist.has_item(1000))
  125. class TestGetItems(unittest.TestCase):
  126. """Test case for the DownloadList get_items method."""
  127. def test_get_items(self):
  128. mocks = [mock.Mock() for _ in range(3)]
  129. dlist = DownloadList(mocks)
  130. self.assertEqual(dlist.get_items(), mocks)
  131. def test_get_items_empty_list(self):
  132. dlist = DownloadList()
  133. self.assertEqual(dlist.get_items(), [])
  134. class TestClear(unittest.TestCase):
  135. """Test case for the DownloadList clear method."""
  136. def test_clear(self):
  137. dlist = DownloadList([mock.Mock() for _ in range(3)])
  138. self.assertEqual(len(dlist), 3)
  139. dlist.clear()
  140. self.assertEqual(len(dlist), 0)
  141. class TestChangeStage(unittest.TestCase):
  142. """Test case for the DownloadList change_stage method."""
  143. def setUp(self):
  144. self.mocks = [mock.Mock(object_id=i, stage="Queued") for i in range(3)]
  145. self.dlist = DownloadList(self.mocks)
  146. def test_change_stage(self):
  147. self.dlist.change_stage(0, "Active")
  148. self.assertEqual(self.mocks[0].stage, "Active")
  149. def test_change_stage_id_not_exist(self):
  150. self.assertRaises(KeyError, self.dlist.change_stage, 3, "Active")
  151. class TestSynchronizeDecorator(unittest.TestCase):
  152. def test_synchronize(self):
  153. mock_func = mock.Mock()
  154. mock_lock = mock.Mock()
  155. decorated_func = synchronized(mock_lock)(mock_func)
  156. self.assertEqual(decorated_func(1, a=2), mock_func.return_value)
  157. mock_func.assert_called_once_with(1, a=2)
  158. mock_lock.acquire.assert_called_once()
  159. mock_lock.release.assert_called_once()
  160. def main():
  161. unittest.main()
  162. if __name__ == '__main__':
  163. main()