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.

114 lines
3.3 KiB

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 test_build_command_linux(self):
  46. result = "youtube-dl -o \"/home/user/downloads/%(upload_date)s/%(id)" \
  47. "s_%(playlist_id)s - %(format)s.%(ext)s\" -f mp4 --ignore-config " \
  48. "\"https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA\""
  49. options = [
  50. "-o",
  51. "/home/user/downloads/%(upload_date)s/%(id)s_%(playlist_id)s - %(format)s.%(ext)s",
  52. "-f",
  53. "mp4",
  54. "--ignore-config"
  55. ]
  56. url = "https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA"
  57. utils.YOUTUBEDL_BIN = "youtube-dl"
  58. self.assertEqual(utils.build_command(options, url), result)
  59. def test_build_command_windows(self):
  60. result = "youtube-dl.exe -o \"C:\\downloads\\%(upload_date)s\\%(id)" \
  61. "s_%(playlist_id)s - %(format)s.%(ext)s\" -f mp4 --ignore-config " \
  62. "\"https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA\""
  63. options = [
  64. "-o",
  65. "C:\\downloads\\%(upload_date)s\\%(id)s_%(playlist_id)s - %(format)s.%(ext)s",
  66. "-f",
  67. "mp4",
  68. "--ignore-config"
  69. ]
  70. url = "https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA"
  71. utils.YOUTUBEDL_BIN = "youtube-dl.exe"
  72. self.assertEqual(utils.build_command(options, url), result)
  73. def main():
  74. unittest.main()
  75. if __name__ == "__main__":
  76. main()