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.

273 lines
8.1 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. """Contains test cases for the DownloadItem object."""
  4. from __future__ import unicode_literals
  5. import sys
  6. import os.path
  7. import unittest
  8. PATH = os.path.realpath(os.path.abspath(__file__))
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(PATH)))
  10. try:
  11. from youtube_dl_gui.downloadmanager import DownloadItem
  12. except ImportError as error:
  13. print error
  14. sys.exit(1)
  15. class TestItemInit(unittest.TestCase):
  16. """Test case for DownloadItem init."""
  17. def test_init(self):
  18. url = "url"
  19. options = ["-f", "flv"]
  20. ditem = DownloadItem(url, options)
  21. self.assertEqual(ditem.stage, "Queued")
  22. self.assertEqual(ditem.url, url)
  23. self.assertEqual(ditem.options, options)
  24. self.assertEqual(ditem.object_id, hash(url + unicode(options)))
  25. self.assertEqual(ditem.path, "")
  26. self.assertEqual(ditem.filenames, [])
  27. self.assertEqual(ditem.extensions, [])
  28. self.assertEqual(
  29. ditem.progress_stats,
  30. {"filename": url,
  31. "extension": "-",
  32. "filesize": "-",
  33. "percent": "0%",
  34. "speed": "-",
  35. "eta": "-",
  36. "status": "Queued"}
  37. )
  38. class TestGetFiles(unittest.TestCase):
  39. """Test case for DownloadItem get_files method."""
  40. def setUp(self):
  41. self.ditem = DownloadItem("url", ["-f", "flv"])
  42. def test_get_files(self):
  43. path = os.path.join("/home", "user", "downloads")
  44. self.ditem.path = path
  45. self.ditem.filenames = ["file1", "file2"]
  46. self.ditem.extensions = [".mp4", ".m4a"]
  47. self.assertEqual(self.ditem.get_files(), [os.path.join(path, "file1" + ".mp4"), os.path.join(path, "file2" + ".m4a")])
  48. def test_get_files_no_data(self):
  49. self.assertEqual(self.ditem.get_files(), [])
  50. class TestItemComparison(unittest.TestCase):
  51. """Test case for DownloadItem __eq__ method."""
  52. def test_equal_true(self):
  53. ditem1 = DownloadItem("url", ["-f", "flv"])
  54. ditem2 = DownloadItem("url", ["-f", "flv"])
  55. self.assertTrue(ditem1 == ditem2)
  56. def test_equal_false(self):
  57. ditem1 = DownloadItem("url", ["-f", "flv"])
  58. ditem2 = DownloadItem("url2", ["-f", "flv"])
  59. self.assertFalse(ditem1 == ditem2)
  60. ditem1 = DownloadItem("url", ["-f", "flv"])
  61. ditem2 = DownloadItem("url", ["-f", "mp4"])
  62. self.assertFalse(ditem1 == ditem2)
  63. class TestSetItemStage(unittest.TestCase):
  64. """Test case for DownloadItem stage setter."""
  65. def setUp(self):
  66. self.ditem = DownloadItem("url", ["-f", "flv"])
  67. def test_set_stage_valid(self):
  68. self.ditem.stage = "Queued"
  69. self.assertEqual(self.ditem.stage, "Queued")
  70. self.assertEqual(self.ditem.progress_stats["status"], "Queued")
  71. self.ditem.stage = "Active"
  72. self.assertEqual(self.ditem.stage, "Active")
  73. self.assertEqual(self.ditem.progress_stats["status"], "Pre Processing")
  74. self.ditem.stage = "Completed"
  75. self.assertEqual(self.ditem.stage, "Completed")
  76. self.assertEqual(self.ditem.progress_stats["status"], "Finished")
  77. self.ditem.stage = "Paused"
  78. self.assertEqual(self.ditem.stage, "Paused")
  79. self.assertEqual(self.ditem.progress_stats["status"], "Paused")
  80. def test_set_stage_invalid(self):
  81. raised = False
  82. try:
  83. self.ditem.stage = "some other status"
  84. except ValueError:
  85. raised = True
  86. self.assertTrue(raised)
  87. class TestUpdateStats(unittest.TestCase):
  88. """Test case for DownloadItem update_stats method."""
  89. def setUp(self):
  90. self.ditem = DownloadItem("url", ["-f", "flv"])
  91. def test_update_stats(self):
  92. path = os.path.join("/home", "user")
  93. self.ditem.update_stats({"filename": "somefilename",
  94. "extension": ".mp4",
  95. "filesize": "9.45MiB",
  96. "percent": "2.0%",
  97. "speed": "200.00KiB/s",
  98. "eta": "00:38",
  99. "status": "Downloading",
  100. "path": path})
  101. self.assertEqual(self.ditem.path, path)
  102. self.assertEqual(self.ditem.filenames, ["somefilename"])
  103. self.assertEqual(self.ditem.extensions, [".mp4"])
  104. self.assertEqual(
  105. self.ditem.progress_stats,
  106. {"filename": "somefilename",
  107. "extension": ".mp4",
  108. "filesize": "9.45MiB",
  109. "percent": "2.0%",
  110. "speed": "200.00KiB/s",
  111. "eta": "00:38",
  112. "status": "Downloading"}
  113. )
  114. self.ditem.update_stats({"filename": "someotherfilename", "extension": ".m4a"})
  115. self.assertEqual(self.ditem.filenames, ["somefilename", "someotherfilename"])
  116. self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"])
  117. self.assertEqual(
  118. self.ditem.progress_stats,
  119. {"filename": "someotherfilename",
  120. "extension": ".m4a",
  121. "filesize": "9.45MiB",
  122. "percent": "2.0%",
  123. "speed": "200.00KiB/s",
  124. "eta": "00:38",
  125. "status": "Downloading"}
  126. )
  127. def test_update_stats_invalid_input(self):
  128. self.assertRaises(AssertionError, self.ditem.update_stats, [])
  129. class TestDownloadItemPrivate(unittest.TestCase):
  130. """Test case for private method of the DownloadItem."""
  131. def test_set_stage(self):
  132. ditem = DownloadItem("url", ["-f", "flv"])
  133. active_status = ["Pre Processing", "Downloading", "Post Processing"]
  134. complete_status = ["Finished", "Error", "Warning", "Stopped", "Already Downloaded", "Filesize Abort"]
  135. for status in active_status:
  136. ditem._stage = "Queued"
  137. ditem._set_stage(status)
  138. self.assertEqual(ditem.stage, "Active")
  139. for status in complete_status:
  140. ditem._stage = "Active"
  141. ditem._set_stage(status)
  142. self.assertEqual(ditem.stage, "Completed")
  143. class TestReset(unittest.TestCase):
  144. """Test case for the DownloadItem reset method."""
  145. def setUp(self):
  146. self.ditem = DownloadItem("url", ["-f", "flv"])
  147. def test_reset_error_status(self):
  148. self.ditem._stage = "Completed"
  149. self.ditem.path = os.path.join("/home", "user")
  150. self.ditem.filenames = ["file1", "file2", "file"]
  151. self.ditem.extensions = [".mp4", ".m4a", ".mp4"]
  152. self.ditem.progress_stats = {
  153. "filename": "file",
  154. "extension": ".mp4",
  155. "filsize": "9.45MiB",
  156. "percent": "100%",
  157. "speed": "-",
  158. "eta": "00:00",
  159. "status": "Error"
  160. }
  161. self.ditem.reset()
  162. self.assertEqual(self.ditem._stage, "Queued")
  163. self.assertEqual(self.ditem.path, "")
  164. self.assertEqual(self.ditem.filenames, [])
  165. self.assertEqual(self.ditem.extensions, [])
  166. self.assertEqual(
  167. self.ditem.progress_stats,
  168. {"filename": "url",
  169. "extension": "-",
  170. "filesize": "-",
  171. "percent": "0%",
  172. "speed": "-",
  173. "eta": "-",
  174. "status": "Queued"}
  175. )
  176. def test_reset_paused_stage(self):
  177. self.ditem._stage = "Paused"
  178. # No need to change filanames, extension, etc
  179. # since everything in pause state has the default value
  180. self.ditem.reset()
  181. self.assertEqual(self.ditem._stage, "Queued")
  182. def test_reset_active_stage(self):
  183. self.ditem._stage = "Active"
  184. self.ditem.path = os.path.join("/home", "user")
  185. self.ditem.filenames = ["file1"]
  186. self.ditem.extensions = [".mp4"]
  187. self.ditem.progress_stats = {
  188. "filename": "file1",
  189. "extension": ".mp4",
  190. "filsize": "9.45MiB",
  191. "percent": "75.5%",
  192. "speed": "200.00KiB/s",
  193. "eta": "00:10",
  194. "status": "Downloading"
  195. }
  196. self.assertRaises(RuntimeError, self.ditem.reset)
  197. def main():
  198. unittest.main()
  199. if __name__ == '__main__':
  200. main()