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.

550 lines
19 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(ditem.filesizes, [])
  29. self.assertEqual(
  30. ditem.progress_stats,
  31. {"filename": url,
  32. "extension": "-",
  33. "filesize": "-",
  34. "percent": "0%",
  35. "speed": "-",
  36. "eta": "-",
  37. "status": "Queued",
  38. "playlist_size": "",
  39. "playlist_index": ""}
  40. )
  41. class TestGetFiles(unittest.TestCase):
  42. """Test case for DownloadItem get_files method."""
  43. def setUp(self):
  44. self.ditem = DownloadItem("url", ["-f", "flv"])
  45. def test_get_files(self):
  46. path = os.path.join("/home", "user", "downloads")
  47. self.ditem.path = path
  48. self.ditem.filenames = ["file1", "file2"]
  49. self.ditem.extensions = [".mp4", ".m4a"]
  50. self.assertEqual(self.ditem.get_files(), [os.path.join(path, "file1" + ".mp4"), os.path.join(path, "file2" + ".m4a")])
  51. def test_get_files_no_data(self):
  52. self.assertEqual(self.ditem.get_files(), [])
  53. class TestItemComparison(unittest.TestCase):
  54. """Test case for DownloadItem __eq__ method."""
  55. def test_equal_true(self):
  56. ditem1 = DownloadItem("url", ["-f", "flv"])
  57. ditem2 = DownloadItem("url", ["-f", "flv"])
  58. self.assertTrue(ditem1 == ditem2)
  59. def test_equal_false(self):
  60. ditem1 = DownloadItem("url", ["-f", "flv"])
  61. ditem2 = DownloadItem("url2", ["-f", "flv"])
  62. self.assertFalse(ditem1 == ditem2)
  63. ditem1 = DownloadItem("url", ["-f", "flv"])
  64. ditem2 = DownloadItem("url", ["-f", "mp4"])
  65. self.assertFalse(ditem1 == ditem2)
  66. class TestSetItemStage(unittest.TestCase):
  67. """Test case for DownloadItem stage setter."""
  68. def setUp(self):
  69. self.ditem = DownloadItem("url", ["-f", "flv"])
  70. def test_set_stage_valid(self):
  71. self.ditem.stage = "Queued"
  72. self.assertEqual(self.ditem.stage, "Queued")
  73. self.assertEqual(self.ditem.progress_stats["status"], "Queued")
  74. self.ditem.stage = "Active"
  75. self.assertEqual(self.ditem.stage, "Active")
  76. self.assertEqual(self.ditem.progress_stats["status"], "Pre Processing")
  77. self.ditem.stage = "Completed"
  78. self.assertEqual(self.ditem.stage, "Completed")
  79. self.assertEqual(self.ditem.progress_stats["status"], "Finished")
  80. self.ditem.stage = "Paused"
  81. self.assertEqual(self.ditem.stage, "Paused")
  82. self.assertEqual(self.ditem.progress_stats["status"], "Paused")
  83. self.ditem.stage = "Error"
  84. self.assertEqual(self.ditem.stage, "Error")
  85. self.assertEqual(self.ditem.progress_stats["status"], "Error")
  86. def test_set_stage_invalid(self):
  87. raised = False
  88. try:
  89. self.ditem.stage = "some other status"
  90. except ValueError:
  91. raised = True
  92. self.assertTrue(raised)
  93. class TestUpdateStats(unittest.TestCase):
  94. """Test case for DownloadItem update_stats method."""
  95. def setUp(self):
  96. self.ditem = DownloadItem("url", ["-f", "flv"])
  97. def test_update_stats(self):
  98. path = os.path.join("/home", "user")
  99. # 1st playlist item
  100. self.ditem.update_stats({"filename": "somefilename.f1",
  101. "extension": ".mp4",
  102. "filesize": "9.45MiB",
  103. "percent": "2.0%",
  104. "speed": "200.00KiB/s",
  105. "eta": "00:38",
  106. "status": "Downloading",
  107. "path": path,
  108. "playlist_size": "10",
  109. "playlist_index": "1"})
  110. self.assertEqual(self.ditem.path, path)
  111. self.assertEqual(self.ditem.filenames, ["somefilename.f1"])
  112. self.assertEqual(self.ditem.extensions, [".mp4"])
  113. # Do not update filesizes unless percentage is 100%
  114. # See https://github.com/MrS0m30n3/youtube-dl-gui/issues/162
  115. self.assertEqual(self.ditem.filesizes, [])
  116. self.assertEqual(
  117. self.ditem.progress_stats,
  118. {"filename": "somefilename.f1",
  119. "extension": ".mp4",
  120. "filesize": "9.45MiB",
  121. "percent": "2.0%",
  122. "speed": "200.00KiB/s",
  123. "eta": "00:38",
  124. "status": "Downloading",
  125. "playlist_size": "10",
  126. "playlist_index": "1"}
  127. )
  128. # Since the percentage is 100% this should update the filesizes list
  129. self.ditem.update_stats({"filesize": "9.45MiB",
  130. "percent": "100%",
  131. "speed": "",
  132. "eta": "",
  133. "status": "Downloading"})
  134. self.assertEqual(self.ditem.filesizes, [9909043.20])
  135. self.ditem.update_stats({"filename": "somefilename.f2",
  136. "extension": ".m4a",
  137. "filesize": "2.22MiB",
  138. "percent": "33.0%",
  139. "speed": "200.00KiB/s",
  140. "eta": "00:20",
  141. "status": "Downloading",
  142. "path": path})
  143. self.assertEqual(self.ditem.path, path)
  144. self.assertEqual(self.ditem.filenames, ["somefilename.f1", "somefilename.f2"])
  145. self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"])
  146. self.assertEqual(self.ditem.filesizes, [9909043.20])
  147. self.assertEqual(
  148. self.ditem.progress_stats,
  149. {"filename": "somefilename.f2",
  150. "extension": ".m4a",
  151. "filesize": "2.22MiB",
  152. "percent": "33.0%",
  153. "speed": "200.00KiB/s",
  154. "eta": "00:20",
  155. "status": "Downloading",
  156. "playlist_size": "10",
  157. "playlist_index": "1"}
  158. )
  159. # Since the percentage is 100% this should update the filesizes list
  160. self.ditem.update_stats({"filesize": "2.22MiB",
  161. "percent": "100%",
  162. "speed": "",
  163. "eta": "",
  164. "status": "Downloading"})
  165. self.assertEqual(self.ditem.filesizes, [9909043.20, 2327838.72])
  166. # Moving to the 2nd playlist item
  167. self.ditem.update_stats({"filename": "someotherfilename.f1",
  168. "extension": ".mp4",
  169. "filesize": "10.25MiB",
  170. "percent": "50.0%",
  171. "speed": "200.00KiB/s",
  172. "eta": "00:38",
  173. "status": "Downloading",
  174. "path": path,
  175. "playlist_size": "10",
  176. "playlist_index": "2"})
  177. # We must reset filenames, extensions & filesizes lists when changing playlist index
  178. # else the filesizes for the post processed files will be wrong
  179. self.assertEqual(self.ditem.filenames, ["someotherfilename.f1"])
  180. self.assertEqual(self.ditem.extensions, [".mp4"])
  181. self.assertEqual(self.ditem.filesizes, [])
  182. self.assertEqual(
  183. self.ditem.progress_stats,
  184. {"filename": "someotherfilename.f1",
  185. "extension": ".mp4",
  186. "filesize": "10.25MiB",
  187. "percent": "50.0%",
  188. "speed": "200.00KiB/s",
  189. "eta": "00:38",
  190. "status": "Downloading",
  191. "playlist_size": "10",
  192. "playlist_index": "2"}
  193. )
  194. # Since the percentage is 100% this should update the filesizes list
  195. self.ditem.update_stats({"filesize": "10.25MiB",
  196. "percent": "100%",
  197. "speed": "",
  198. "eta": "",
  199. "status": "Downloading"})
  200. self.assertEqual(self.ditem.filesizes, [10747904.0])
  201. self.ditem.update_stats({"filename": "someotherfilename.f2",
  202. "extension": ".m4a",
  203. "filesize": "3.33MiB",
  204. "percent": "33.0%",
  205. "speed": "200.00KiB/s",
  206. "eta": "00:30",
  207. "status": "Downloading",
  208. "path": path})
  209. self.assertEqual(self.ditem.path, path)
  210. self.assertEqual(self.ditem.filenames, ["someotherfilename.f1", "someotherfilename.f2"])
  211. self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"])
  212. self.assertEqual(self.ditem.filesizes, [10747904.0])
  213. self.assertEqual(
  214. self.ditem.progress_stats,
  215. {"filename": "someotherfilename.f2",
  216. "extension": ".m4a",
  217. "filesize": "3.33MiB",
  218. "percent": "33.0%",
  219. "speed": "200.00KiB/s",
  220. "eta": "00:30",
  221. "status": "Downloading",
  222. "playlist_size": "10",
  223. "playlist_index": "2"}
  224. )
  225. # Since the percentage is 100% this should update the filesizes list
  226. self.ditem.update_stats({"filesize": "3.33MiB",
  227. "percent": "100%",
  228. "speed": "",
  229. "eta": "",
  230. "status": "Downloading"})
  231. self.assertEqual(self.ditem.filesizes, [10747904.0, 3491758.08])
  232. # Let's move to the 3rd playlist item
  233. # Here we mimic youtube-dl's "max downloads limit reached"
  234. # this line should not reset the filenames, extensions & filesizes lists
  235. # since we will lose the ability to play the last playlist item
  236. self.ditem.update_stats({"status": "Downloading",
  237. "playlist_size": "10",
  238. "playlist_index": "3"})
  239. self.assertEqual(self.ditem.filenames, ["someotherfilename.f1", "someotherfilename.f2"])
  240. self.assertEqual(self.ditem.extensions, [".mp4", ".m4a"])
  241. self.assertEqual(self.ditem.filesizes, [10747904.0, 3491758.08])
  242. self.assertEqual(
  243. self.ditem.progress_stats,
  244. {"filename": "someotherfilename.f2",
  245. "extension": ".m4a",
  246. "filesize": "3.33MiB",
  247. "percent": "100%",
  248. "speed": "-",
  249. "eta": "-",
  250. "status": "Downloading",
  251. "playlist_size": "10",
  252. "playlist_index": "3"}
  253. )
  254. def test_update_stats_invalid_input(self):
  255. self.assertRaises(AssertionError, self.ditem.update_stats, [])
  256. def test_update_stats_empty_strings(self):
  257. self.ditem.update_stats({"filename": "",
  258. "extension": "",
  259. "filesize": "",
  260. "percent": "",
  261. "speed": "",
  262. "eta": "",
  263. "status": "",
  264. "playlist_size": "",
  265. "playlist_index": ""})
  266. self.assertEqual(
  267. self.ditem.progress_stats,
  268. {"filename": "url",
  269. "extension": "-",
  270. "filesize": "-",
  271. "percent": "0%",
  272. "speed": "-",
  273. "eta": "-",
  274. "status": "Queued",
  275. "playlist_size": "",
  276. "playlist_index": ""}
  277. )
  278. def test_update_stats_not_string(self):
  279. self.ditem.update_stats({"filename": None, "status": 1234, "eta": False})
  280. self.assertEqual(self.ditem.progress_stats["filename"], "url")
  281. self.assertEqual(self.ditem.progress_stats["status"], "Queued")
  282. self.assertEqual(self.ditem.progress_stats["eta"], "-")
  283. class TestDownloadItemPrivate(unittest.TestCase):
  284. """Test case for private method of the DownloadItem."""
  285. def test_set_stage(self):
  286. ditem = DownloadItem("url", ["-f", "flv"])
  287. active_status = ["Pre Processing", "Downloading", "Post Processing"]
  288. complete_status = ["Finished", "Warning", "Already Downloaded"]
  289. error_status = ["Error", "Stopped", "Filesize Abort"]
  290. for status in active_status:
  291. ditem._set_stage(status)
  292. self.assertEqual(ditem.stage, "Active")
  293. for status in complete_status:
  294. ditem._set_stage(status)
  295. self.assertEqual(ditem.stage, "Completed")
  296. for status in error_status:
  297. ditem._set_stage(status)
  298. self.assertEqual(ditem.stage, "Error")
  299. def test_calc_post_proc_size(self):
  300. # REFACTOR Not an actual method
  301. # should transfer to TestUpdateStats
  302. ditem = DownloadItem("url", ["-f", "flv"])
  303. ditem.update_stats({"filename": "file.f123",
  304. "extension": ".webm",
  305. "filesize": "10.00MiB",
  306. "percent": "100%",
  307. "speed": "",
  308. "eta": "",
  309. "status": "Downloading",
  310. "path": "/home/user"})
  311. ditem.update_stats({"filename": "file.f456",
  312. "extension": ".m4a",
  313. "filesize": "3.45MiB",
  314. "percent": "100%",
  315. "speed": "",
  316. "eta": "",
  317. "status": "Downloading",
  318. "path": "/home/user"})
  319. # Mimic youtube-dl post process behaviour
  320. ditem.update_stats({"filename": "file",
  321. "extension": ".webm",
  322. "percent": "100%",
  323. "speed": "",
  324. "eta": "",
  325. "status": "Post Processing"})
  326. self.assertEqual(ditem.filesizes, [10485760.00, 3617587.20, 14103347.20])
  327. self.assertEqual(
  328. ditem.progress_stats,
  329. {"filename": "file",
  330. "extension": ".webm",
  331. "filesize": "13.45MiB",
  332. "percent": "100%",
  333. "speed": "-",
  334. "eta": "-",
  335. "status": "Post Processing",
  336. "playlist_size": "",
  337. "playlist_index": ""}
  338. )
  339. class TestReset(unittest.TestCase):
  340. """Test case for the DownloadItem reset method."""
  341. def setUp(self):
  342. self.ditem = DownloadItem("url", ["-f", "flv"])
  343. def test_reset_completed_stage(self):
  344. self.ditem._stage = "Completed"
  345. self.ditem.path = os.path.join("/home", "user")
  346. self.ditem.filenames = ["file"]
  347. self.ditem.extensions = [".mp4"]
  348. self.ditem.filesizes = [123456.00]
  349. self.ditem.progress_stats = {
  350. "filename": "file",
  351. "extension": ".mp4",
  352. "filsize": "6.66MiB",
  353. "percent": "100%",
  354. "speed": "-",
  355. "eta": "-",
  356. "status": "Finished",
  357. "playlist_size": "",
  358. "playlist_index": ""
  359. }
  360. self.ditem.reset()
  361. self.assertEqual(self.ditem._stage, "Queued")
  362. self.assertEqual(self.ditem.path, "")
  363. self.assertEqual(self.ditem.filenames, [])
  364. self.assertEqual(self.ditem.extensions, [])
  365. self.assertEqual(self.ditem.filesizes, [])
  366. self.assertEqual(
  367. self.ditem.progress_stats,
  368. {"filename": "url",
  369. "extension": "-",
  370. "filesize": "-",
  371. "percent": "0%",
  372. "speed": "-",
  373. "eta": "-",
  374. "status": "Queued",
  375. "playlist_size": "",
  376. "playlist_index": ""}
  377. )
  378. def test_reset_error_stage(self):
  379. self.ditem._stage = "Error"
  380. self.ditem.path = os.path.join("/home", "user")
  381. self.ditem.filenames = ["file1", "file2", "file"]
  382. self.ditem.extensions = [".mp4", ".m4a", ".mp4"]
  383. self.ditem.filesizes = [1234.00, 3421.00, 4655.00]
  384. self.ditem.progress_stats = {
  385. "filename": "file",
  386. "extension": ".mp4",
  387. "filsize": "9.45MiB",
  388. "percent": "100%",
  389. "speed": "-",
  390. "eta": "-",
  391. "status": "Error",
  392. "playlist_size": "10",
  393. "playlist_index": "8"
  394. }
  395. self.ditem.reset()
  396. self.assertEqual(self.ditem._stage, "Queued")
  397. self.assertEqual(self.ditem.path, "")
  398. self.assertEqual(self.ditem.filenames, [])
  399. self.assertEqual(self.ditem.extensions, [])
  400. self.assertEqual(self.ditem.filesizes, [])
  401. self.assertEqual(
  402. self.ditem.progress_stats,
  403. {"filename": "url",
  404. "extension": "-",
  405. "filesize": "-",
  406. "percent": "0%",
  407. "speed": "-",
  408. "eta": "-",
  409. "status": "Queued",
  410. "playlist_size": "",
  411. "playlist_index": ""}
  412. )
  413. def test_reset_paused_stage(self):
  414. self.ditem._stage = "Paused"
  415. # No need to change filanames, extension, etc
  416. # since everything in pause state has the default value
  417. self.ditem.reset()
  418. self.assertEqual(self.ditem._stage, "Queued")
  419. def test_reset_active_stage(self):
  420. self.ditem._stage = "Active"
  421. self.ditem.path = os.path.join("/home", "user")
  422. self.ditem.filenames = ["file1"]
  423. self.ditem.extensions = [".mp4"]
  424. self.ditem.filesizes = []
  425. self.ditem.progress_stats = {
  426. "filename": "file1",
  427. "extension": ".mp4",
  428. "filsize": "9.45MiB",
  429. "percent": "75.5%",
  430. "speed": "200.00KiB/s",
  431. "eta": "00:10",
  432. "status": "Downloading"
  433. }
  434. self.assertRaises(RuntimeError, self.ditem.reset)
  435. def main():
  436. unittest.main()
  437. if __name__ == '__main__':
  438. main()