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.

361 lines
11 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. "playlist_size": "",
  38. "playlist_index": ""}
  39. )
  40. class TestGetFiles(unittest.TestCase):
  41. """Test case for DownloadItem get_files method."""
  42. def setUp(self):
  43. self.ditem = DownloadItem("url", ["-f", "flv"])
  44. def test_get_files(self):
  45. path = os.path.join("/home", "user", "downloads")
  46. self.ditem.path = path
  47. self.ditem.filenames = ["file1", "file2"]
  48. self.ditem.extensions = [".mp4", ".m4a"]
  49. self.assertEqual(self.ditem.get_files(), [os.path.join(path, "file1" + ".mp4"), os.path.join(path, "file2" + ".m4a")])
  50. def test_get_files_no_data(self):
  51. self.assertEqual(self.ditem.get_files(), [])
  52. class TestItemComparison(unittest.TestCase):
  53. """Test case for DownloadItem __eq__ method."""
  54. def test_equal_true(self):
  55. ditem1 = DownloadItem("url", ["-f", "flv"])
  56. ditem2 = DownloadItem("url", ["-f", "flv"])
  57. self.assertTrue(ditem1 == ditem2)
  58. def test_equal_false(self):
  59. ditem1 = DownloadItem("url", ["-f", "flv"])
  60. ditem2 = DownloadItem("url2", ["-f", "flv"])
  61. self.assertFalse(ditem1 == ditem2)
  62. ditem1 = DownloadItem("url", ["-f", "flv"])
  63. ditem2 = DownloadItem("url", ["-f", "mp4"])
  64. self.assertFalse(ditem1 == ditem2)
  65. class TestSetItemStage(unittest.TestCase):
  66. """Test case for DownloadItem stage setter."""
  67. def setUp(self):
  68. self.ditem = DownloadItem("url", ["-f", "flv"])
  69. def test_set_stage_valid(self):
  70. self.ditem.stage = "Queued"
  71. self.assertEqual(self.ditem.stage, "Queued")
  72. self.assertEqual(self.ditem.progress_stats["status"], "Queued")
  73. self.ditem.stage = "Active"
  74. self.assertEqual(self.ditem.stage, "Active")
  75. self.assertEqual(self.ditem.progress_stats["status"], "Pre Processing")
  76. self.ditem.stage = "Completed"
  77. self.assertEqual(self.ditem.stage, "Completed")
  78. self.assertEqual(self.ditem.progress_stats["status"], "Finished")
  79. self.ditem.stage = "Paused"
  80. self.assertEqual(self.ditem.stage, "Paused")
  81. self.assertEqual(self.ditem.progress_stats["status"], "Paused")
  82. self.ditem.stage = "Error"
  83. self.assertEqual(self.ditem.stage, "Error")
  84. self.assertEqual(self.ditem.progress_stats["status"], "Error")
  85. def test_set_stage_invalid(self):
  86. raised = False
  87. try:
  88. self.ditem.stage = "some other status"
  89. except ValueError:
  90. raised = True
  91. self.assertTrue(raised)
  92. class TestUpdateStats(unittest.TestCase):
  93. """Test case for DownloadItem update_stats method."""
  94. def setUp(self):
  95. self.ditem = DownloadItem("url", ["-f", "flv"])
  96. def test_update_stats(self):
  97. path = os.path.join("/home", "user")
  98. self.ditem.update_stats({"filename": "somefilename",
  99. "extension": ".mp4",
  100. "filesize": "9.45MiB",
  101. "percent": "2.0%",
  102. "speed": "200.00KiB/s",
  103. "eta": "00:38",
  104. "status": "Downloading",
  105. "path": path,
  106. "playlist_size": "10",
  107. "playlist_index": "1"})
  108. self.assertEqual(self.ditem.path, path)
  109. self.assertEqual(self.ditem.filenames, ["somefilename"])
  110. self.assertEqual(self.ditem.extensions, [".mp4"])
  111. self.assertEqual(
  112. self.ditem.progress_stats,
  113. {"filename": "somefilename",
  114. "extension": ".mp4",
  115. "filesize": "9.45MiB",
  116. "percent": "2.0%",
  117. "speed": "200.00KiB/s",
  118. "eta": "00:38",
  119. "status": "Downloading",
  120. "playlist_size": "10",
  121. "playlist_index": "1"}
  122. )
  123. self.ditem.update_stats({"filename": "someotherfilename",
  124. "extension": ".m4a",
  125. "playlist_index": "2"})
  126. self.assertEqual(self.ditem.filenames, ["somefilename", "someotherfilename"])
  127. self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"])
  128. self.assertEqual(
  129. self.ditem.progress_stats,
  130. {"filename": "someotherfilename",
  131. "extension": ".m4a",
  132. "filesize": "9.45MiB",
  133. "percent": "2.0%",
  134. "speed": "200.00KiB/s",
  135. "eta": "00:38",
  136. "status": "Downloading",
  137. "playlist_size": "10",
  138. "playlist_index": "2"}
  139. )
  140. def test_update_stats_invalid_input(self):
  141. self.assertRaises(AssertionError, self.ditem.update_stats, [])
  142. def test_update_stats_empty_strings(self):
  143. self.ditem.update_stats({"filename": "",
  144. "extension": "",
  145. "filesize": "",
  146. "percent": "",
  147. "speed": "",
  148. "eta": "",
  149. "status": "",
  150. "playlist_size": "",
  151. "playlist_index": ""})
  152. self.assertEqual(
  153. self.ditem.progress_stats,
  154. {"filename": "url",
  155. "extension": "-",
  156. "filesize": "-",
  157. "percent": "0%",
  158. "speed": "-",
  159. "eta": "-",
  160. "status": "Queued",
  161. "playlist_size": "",
  162. "playlist_index": ""}
  163. )
  164. def test_update_stats_not_string(self):
  165. self.ditem.update_stats({"filename": None, "status": 1234, "eta": False})
  166. self.assertEqual(self.ditem.progress_stats["filename"], "url")
  167. self.assertEqual(self.ditem.progress_stats["status"], "Queued")
  168. self.assertEqual(self.ditem.progress_stats["eta"], "-")
  169. class TestDownloadItemPrivate(unittest.TestCase):
  170. """Test case for private method of the DownloadItem."""
  171. def test_set_stage(self):
  172. ditem = DownloadItem("url", ["-f", "flv"])
  173. active_status = ["Pre Processing", "Downloading", "Post Processing"]
  174. complete_status = ["Finished", "Warning", "Already Downloaded"]
  175. error_status = ["Error", "Stopped", "Filesize Abort"]
  176. for status in active_status:
  177. ditem._set_stage(status)
  178. self.assertEqual(ditem.stage, "Active")
  179. for status in complete_status:
  180. ditem._set_stage(status)
  181. self.assertEqual(ditem.stage, "Completed")
  182. for status in error_status:
  183. ditem._set_stage(status)
  184. self.assertEqual(ditem.stage, "Error")
  185. class TestReset(unittest.TestCase):
  186. """Test case for the DownloadItem reset method."""
  187. def setUp(self):
  188. self.ditem = DownloadItem("url", ["-f", "flv"])
  189. def test_reset_completed_stage(self):
  190. self.ditem._stage = "Completed"
  191. self.ditem.path = os.path.join("/home", "user")
  192. self.ditem.filenames = ["file"]
  193. self.ditem.extensions = [".mp4"]
  194. self.ditem.progress_stats = {
  195. "filename": "file",
  196. "extension": ".mp4",
  197. "filsize": "6.66MiB",
  198. "percent": "100%",
  199. "speed": "-",
  200. "eta": "00:00",
  201. "status": "Finished",
  202. "playlist_size": "",
  203. "playlist_index": ""
  204. }
  205. self.ditem.reset()
  206. self.assertEqual(self.ditem._stage, "Queued")
  207. self.assertEqual(self.ditem.path, "")
  208. self.assertEqual(self.ditem.filenames, [])
  209. self.assertEqual(self.ditem.extensions, [])
  210. self.assertEqual(
  211. self.ditem.progress_stats,
  212. {"filename": "url",
  213. "extension": "-",
  214. "filesize": "-",
  215. "percent": "0%",
  216. "speed": "-",
  217. "eta": "-",
  218. "status": "Queued",
  219. "playlist_size": "",
  220. "playlist_index": ""}
  221. )
  222. def test_reset_error_stage(self):
  223. self.ditem._stage = "Error"
  224. self.ditem.path = os.path.join("/home", "user")
  225. self.ditem.filenames = ["file1", "file2", "file"]
  226. self.ditem.extensions = [".mp4", ".m4a", ".mp4"]
  227. self.ditem.progress_stats = {
  228. "filename": "file",
  229. "extension": ".mp4",
  230. "filsize": "9.45MiB",
  231. "percent": "100%",
  232. "speed": "-",
  233. "eta": "00:00",
  234. "status": "Error",
  235. "playlist_size": "10",
  236. "playlist_index": "8"
  237. }
  238. self.ditem.reset()
  239. self.assertEqual(self.ditem._stage, "Queued")
  240. self.assertEqual(self.ditem.path, "")
  241. self.assertEqual(self.ditem.filenames, [])
  242. self.assertEqual(self.ditem.extensions, [])
  243. self.assertEqual(
  244. self.ditem.progress_stats,
  245. {"filename": "url",
  246. "extension": "-",
  247. "filesize": "-",
  248. "percent": "0%",
  249. "speed": "-",
  250. "eta": "-",
  251. "status": "Queued",
  252. "playlist_size": "",
  253. "playlist_index": ""}
  254. )
  255. def test_reset_paused_stage(self):
  256. self.ditem._stage = "Paused"
  257. # No need to change filanames, extension, etc
  258. # since everything in pause state has the default value
  259. self.ditem.reset()
  260. self.assertEqual(self.ditem._stage, "Queued")
  261. def test_reset_active_stage(self):
  262. self.ditem._stage = "Active"
  263. self.ditem.path = os.path.join("/home", "user")
  264. self.ditem.filenames = ["file1"]
  265. self.ditem.extensions = [".mp4"]
  266. self.ditem.progress_stats = {
  267. "filename": "file1",
  268. "extension": ".mp4",
  269. "filsize": "9.45MiB",
  270. "percent": "75.5%",
  271. "speed": "200.00KiB/s",
  272. "eta": "00:10",
  273. "status": "Downloading"
  274. }
  275. self.assertRaises(RuntimeError, self.ditem.reset)
  276. def main():
  277. unittest.main()
  278. if __name__ == '__main__':
  279. main()