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.

213 lines
6.5 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. import mock
  12. from youtube_dl_gui import utils
  13. except ImportError as error:
  14. print error
  15. sys.exit(1)
  16. class TestToBytes(unittest.TestCase):
  17. """Test case for the to_bytes method."""
  18. def test_to_bytes_bytes(self):
  19. self.assertEqual(utils.to_bytes("596.00B"), 596.00)
  20. self.assertEqual(utils.to_bytes("133.55B"), 133.55)
  21. def test_to_bytes_kilobytes(self):
  22. self.assertEqual(utils.to_bytes("1.00KiB"), 1024.00)
  23. self.assertEqual(utils.to_bytes("5.55KiB"), 5683.20)
  24. def test_to_bytes_megabytes(self):
  25. self.assertEqual(utils.to_bytes("13.64MiB"), 14302576.64)
  26. self.assertEqual(utils.to_bytes("1.00MiB"), 1048576.00)
  27. def test_to_bytes_gigabytes(self):
  28. self.assertEqual(utils.to_bytes("1.00GiB"), 1073741824.00)
  29. self.assertEqual(utils.to_bytes("1.55GiB"), 1664299827.20)
  30. def test_to_bytes_terabytes(self):
  31. self.assertEqual(utils.to_bytes("1.00TiB"), 1099511627776.00)
  32. class TestFormatBytes(unittest.TestCase):
  33. """Test case for the format_bytes method."""
  34. def test_format_bytes_bytes(self):
  35. self.assertEqual(utils.format_bytes(518.00), "518.00B")
  36. def test_format_bytes_kilobytes(self):
  37. self.assertEqual(utils.format_bytes(1024.00), "1.00KiB")
  38. def test_format_bytes_megabytes(self):
  39. self.assertEqual(utils.format_bytes(1048576.00), "1.00MiB")
  40. def test_format_bytes_gigabytes(self):
  41. self.assertEqual(utils.format_bytes(1073741824.00), "1.00GiB")
  42. def test_format_bytes_terabytes(self):
  43. self.assertEqual(utils.format_bytes(1099511627776.00), "1.00TiB")
  44. class TestBuildCommand(unittest.TestCase):
  45. """Test case for the build_command method."""
  46. def setUp(self):
  47. self.url = "https://www.youtube.com/watch?v=aaaaaaaaaaa&list=AAAAAAAAAAA"
  48. self.options = ["-o", None, "-f", "mp4", "--ignore-config"]
  49. self.result = "{{ydl_bin}} -o \"{{tmpl}}\" -f mp4 --ignore-config \"{url}\"".format(url=self.url)
  50. def run_tests(self, ydl_bin, tmpl):
  51. """Run the main test.
  52. Args:
  53. ydl_bin (str): Name of the youtube-dl binary
  54. tmpl (str): Youtube-dl output template
  55. """
  56. utils.YOUTUBEDL_BIN = ydl_bin
  57. self.options[1] = tmpl # Plug the template in our options
  58. result = self.result.format(ydl_bin=ydl_bin, tmpl=tmpl)
  59. self.assertEqual(utils.build_command(self.options, self.url), result)
  60. def test_build_command_with_spaces_linux(self):
  61. tmpl = "/home/user/downloads/%(upload_date)s/%(id)s_%(playlist_id)s - %(format)s.%(ext)s"
  62. self.run_tests("youtube-dl", tmpl)
  63. def test_build_command_without_spaces_linux(self):
  64. tmpl = "/home/user/downloads/%(id)s.%(ext)s"
  65. self.run_tests("youtube-dl", tmpl)
  66. def test_build_command_with_spaces_windows(self):
  67. tmpl = "C:\\downloads\\%(upload_date)s\\%(id)s_%(playlist_id)s - %(format)s.%(ext)s"
  68. self.run_tests("youtube-dl.exe", tmpl)
  69. def test_build_command_without_spaces_windows(self):
  70. tmpl = "C:\\downloads\\%(id)s.%(ext)s"
  71. self.run_tests("youtube-dl.exe", tmpl)
  72. class TestConvertItem(unittest.TestCase):
  73. """Test case for the convert_item function."""
  74. def setUp(self):
  75. self.input_list_u = ["v1", "v2", "v3"]
  76. self.input_list_s = [str("v1"), str("v2"), str("v3")]
  77. self.input_tuple_u = ("v1", "v2", "v3")
  78. self.input_tuple_s = (str("v1"), str("v2"), str("v3"))
  79. self.input_dict_u = {"k1": "v1", "k2": "v2"}
  80. self.input_dict_s = {str("k1"): str("v1"), str("k2"): str("v2")}
  81. def check_iter(self, iterable, iter_type, is_unicode):
  82. check_type = unicode if is_unicode else str
  83. iterable = utils.convert_item(iterable, is_unicode)
  84. self.assertIsInstance(iterable, iter_type)
  85. for item in iterable:
  86. if iter_type == dict:
  87. self.assertIsInstance(iterable[item], check_type)
  88. self.assertIsInstance(item, check_type)
  89. def test_convert_item_unicode_str(self):
  90. self.assertIsInstance(utils.convert_item("test"), str)
  91. def test_convert_item_unicode_unicode(self):
  92. self.assertIsInstance(utils.convert_item("test", True), unicode)
  93. def test_convert_item_str_unicode(self):
  94. self.assertIsInstance(utils.convert_item(str("test"), True), unicode)
  95. def test_convert_item_str_str(self):
  96. self.assertIsInstance(utils.convert_item(str("test")), str)
  97. def test_convert_item_list_empty(self):
  98. self.assertEqual(len(utils.convert_item([])), 0)
  99. def test_convert_item_dict_empty(self):
  100. self.assertEqual(len(utils.convert_item({})), 0)
  101. def test_convert_item_list_unicode_str(self):
  102. self.check_iter(self.input_list_u, list, False)
  103. def test_convert_item_list_str_unicode(self):
  104. self.check_iter(self.input_list_s, list, True)
  105. def test_convert_item_tuple_unicode_str(self):
  106. self.check_iter(self.input_tuple_u, tuple, False)
  107. def test_convert_item_tuple_str_unicode(self):
  108. self.check_iter(self.input_tuple_s, tuple, True)
  109. def test_convert_item_dict_unicode_str(self):
  110. self.check_iter(self.input_dict_u, dict, False)
  111. def test_convert_item_dict_str_unicode(self):
  112. self.check_iter(self.input_dict_s, dict, True)
  113. class TestGetDefaultLang(unittest.TestCase):
  114. """Test case for the get_default_lang function."""
  115. @mock.patch("youtube_dl_gui.utils.locale_getdefaultlocale")
  116. def run_tests(self, ret_value, result, mock_getdefaultlocale):
  117. """Run the main test.
  118. Args:
  119. ret_value (tuple): Return tuple of the locale.getdefaultlocale module
  120. result (unicode): Result we want to see
  121. mock_getdefaultlocale (MagicMock): Mock object
  122. """
  123. mock_getdefaultlocale.return_value = ret_value
  124. lang = utils.get_default_lang()
  125. mock_getdefaultlocale.assert_called_once()
  126. self.assertEqual(lang, result)
  127. def test_get_default_lang(self):
  128. self.run_tests(("it_IT", "UTF-8"), "it_IT")
  129. def test_get_default_lang_none(self):
  130. self.run_tests((None, None), "en_US")
  131. def test_get_default_lang_empty(self):
  132. self.run_tests(("", ""), "en_US")
  133. def main():
  134. unittest.main()
  135. if __name__ == "__main__":
  136. main()