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.

118 lines
3.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """Contains test cases for the utils.py module."""
  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 import utils
  12. except ImportError as error:
  13. print error
  14. sys.exit(1)
  15. class TestToBytes(unittest.TestCase):
  16. """Test case for the to_bytes method."""
  17. def test_to_bytes_bytes(self):
  18. self.assertEqual(utils.to_bytes("596.00B"), 596.00)
  19. self.assertEqual(utils.to_bytes("133.55B"), 133.55)
  20. def test_to_bytes_kilobytes(self):
  21. self.assertEqual(utils.to_bytes("1.00KiB"), 1024.00)
  22. self.assertEqual(utils.to_bytes("5.55KiB"), 5683.20)
  23. def test_to_bytes_megabytes(self):
  24. self.assertEqual(utils.to_bytes("13.64MiB"), 14302576.64)
  25. self.assertEqual(utils.to_bytes("1.00MiB"), 1048576.00)
  26. def test_to_bytes_gigabytes(self):
  27. self.assertEqual(utils.to_bytes("1.00GiB"), 1073741824.00)
  28. self.assertEqual(utils.to_bytes("1.55GiB"), 1664299827.20)
  29. def test_to_bytes_terabytes(self):
  30. self.assertEqual(utils.to_bytes("1.00TiB"), 1099511627776.00)
  31. class TestFormatBytes(unittest.TestCase):
  32. """Test case for the format_bytes method."""
  33. def test_format_bytes_bytes(self):
  34. self.assertEqual(utils.format_bytes(518.00), "518.00B")
  35. def test_format_bytes_kilobytes(self):
  36. self.assertEqual(utils.format_bytes(1024.00), "1.00KiB")
  37. def test_format_bytes_megabytes(self):
  38. self.assertEqual(utils.format_bytes(1048576.00), "1.00MiB")
  39. def test_format_bytes_gigabytes(self):
  40. self.assertEqual(utils.format_bytes(1073741824.00), "1.00GiB")
  41. def test_format_bytes_terabytes(self):
  42. self.assertEqual(utils.format_bytes(1099511627776.00), "1.00TiB")
  43. class TestBuildCommand(unittest.TestCase):
  44. """Test case for the build_command method."""
  45. def setUp(self):
  46. self.url = "https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA"
  47. self.options = ["-o", None, "-f", "mp4", "--ignore-config"]
  48. self.result = "{{ydl_bin}} -o \"{{tmpl}}\" -f mp4 --ignore-config \"{url}\"".format(url=self.url)
  49. def run_tests(self, ydl_bin, tmpl):
  50. """Run the main test.
  51. Args:
  52. ydl_bin (str): Name of the youtube-dl binary
  53. tmpl (str): Youtube-dl output template
  54. """
  55. utils.YOUTUBEDL_BIN = ydl_bin
  56. self.options[1] = tmpl # Plug the template in our options
  57. result = self.result.format(ydl_bin=ydl_bin, tmpl=tmpl)
  58. self.assertEqual(utils.build_command(self.options, self.url), result)
  59. def test_build_command_with_spaces_linux(self):
  60. tmpl = "/home/user/downloads/%(upload_date)s/%(id)s_%(playlist_id)s - %(format)s.%(ext)s"
  61. self.run_tests("youtube-dl", tmpl)
  62. def test_build_command_without_spaces_linux(self):
  63. tmpl = "/home/user/downloads/%(id)s.%(ext)s"
  64. self.run_tests("youtube-dl", tmpl)
  65. def test_build_command_with_spaces_windows(self):
  66. tmpl = "C:\\downloads\\%(upload_date)s\\%(id)s_%(playlist_id)s - %(format)s.%(ext)s"
  67. self.run_tests("youtube-dl.exe", tmpl)
  68. def test_build_command_without_spaces_windows(self):
  69. tmpl = "C:\\downloads\\%(id)s.%(ext)s"
  70. self.run_tests("youtube-dl.exe", tmpl)
  71. def main():
  72. unittest.main()
  73. if __name__ == "__main__":
  74. main()