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.

182 lines
5.6 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. class TestConvertItem(unittest.TestCase):
  72. """Test case for the convert_item function."""
  73. def setUp(self):
  74. self.input_list_u = ["v1", "v2", "v3"]
  75. self.input_list_s = [str("v1"), str("v2"), str("v3")]
  76. self.input_tuple_u = ("v1", "v2", "v3")
  77. self.input_tuple_s = (str("v1"), str("v2"), str("v3"))
  78. self.input_dict_u = {"k1": "v1", "k2": "v2"}
  79. self.input_dict_s = {str("k1"): str("v1"), str("k2"): str("v2")}
  80. def check_iter(self, iterable, iter_type, is_unicode):
  81. check_type = unicode if is_unicode else str
  82. iterable = utils.convert_item(iterable, is_unicode)
  83. self.assertIsInstance(iterable, iter_type)
  84. for item in iterable:
  85. if iter_type == dict:
  86. self.assertIsInstance(iterable[item], check_type)
  87. self.assertIsInstance(item, check_type)
  88. def test_convert_item_unicode_str(self):
  89. self.assertIsInstance(utils.convert_item("test"), str)
  90. def test_convert_item_unicode_unicode(self):
  91. self.assertIsInstance(utils.convert_item("test", True), unicode)
  92. def test_convert_item_str_unicode(self):
  93. self.assertIsInstance(utils.convert_item(str("test"), True), unicode)
  94. def test_convert_item_str_str(self):
  95. self.assertIsInstance(utils.convert_item(str("test")), str)
  96. def test_convert_item_list_empty(self):
  97. self.assertEqual(len(utils.convert_item([])), 0)
  98. def test_convert_item_dict_empty(self):
  99. self.assertEqual(len(utils.convert_item({})), 0)
  100. def test_convert_item_list_unicode_str(self):
  101. self.check_iter(self.input_list_u, list, False)
  102. def test_convert_item_list_str_unicode(self):
  103. self.check_iter(self.input_list_s, list, True)
  104. def test_convert_item_tuple_unicode_str(self):
  105. self.check_iter(self.input_tuple_u, tuple, False)
  106. def test_convert_item_tuple_str_unicode(self):
  107. self.check_iter(self.input_tuple_s, tuple, True)
  108. def test_convert_item_dict_unicode_str(self):
  109. self.check_iter(self.input_dict_u, dict, False)
  110. def test_convert_item_dict_str_unicode(self):
  111. self.check_iter(self.input_dict_s, dict, True)
  112. def main():
  113. unittest.main()
  114. if __name__ == "__main__":
  115. main()