additional tests
Browse files- pytube/captions.py +2 -2
- tests/test_captions.py +19 -0
- tests/test_cli.py +9 -0
pytube/captions.py
CHANGED
|
@@ -114,13 +114,13 @@ class Caption:
|
|
| 114 |
else:
|
| 115 |
filename = title
|
| 116 |
|
| 117 |
-
filename = safe_filename(filename)
|
| 118 |
-
|
| 119 |
if filename_prefix:
|
| 120 |
filename = "{prefix}{filename}".format(
|
| 121 |
prefix=safe_filename(filename_prefix), filename=filename,
|
| 122 |
)
|
| 123 |
|
|
|
|
|
|
|
| 124 |
filename += " ({})".format(self.code)
|
| 125 |
|
| 126 |
if srt:
|
|
|
|
| 114 |
else:
|
| 115 |
filename = title
|
| 116 |
|
|
|
|
|
|
|
| 117 |
if filename_prefix:
|
| 118 |
filename = "{prefix}{filename}".format(
|
| 119 |
prefix=safe_filename(filename_prefix), filename=filename,
|
| 120 |
)
|
| 121 |
|
| 122 |
+
filename = safe_filename(filename)
|
| 123 |
+
|
| 124 |
filename += " ({})".format(self.code)
|
| 125 |
|
| 126 |
if srt:
|
tests/test_captions.py
CHANGED
|
@@ -56,6 +56,18 @@ def test_download(srt):
|
|
| 56 |
assert m.call_args_list[0][0][0].split("/")[-1] == "title (en).srt"
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
@mock.patch("pytube.captions.Caption.xml_captions")
|
| 60 |
def test_download_xml_and_trim_extension(xml):
|
| 61 |
m = mock_open()
|
|
@@ -66,3 +78,10 @@ def test_download_xml_and_trim_extension(xml):
|
|
| 66 |
)
|
| 67 |
caption.download("title.xml", srt=False)
|
| 68 |
assert m.call_args_list[0][0][0].split("/")[-1] == "title (en).xml"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
assert m.call_args_list[0][0][0].split("/")[-1] == "title (en).srt"
|
| 57 |
|
| 58 |
|
| 59 |
+
@mock.patch("pytube.captions.Caption.generate_srt_captions")
|
| 60 |
+
def test_download_with_prefix(srt):
|
| 61 |
+
m = mock_open()
|
| 62 |
+
with patch("builtins.open", m):
|
| 63 |
+
srt.return_value = ""
|
| 64 |
+
caption = Caption(
|
| 65 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
| 66 |
+
)
|
| 67 |
+
caption.download("title", filename_prefix="1 ")
|
| 68 |
+
assert m.call_args_list[0][0][0].split("/")[-1] == "1 title (en).srt"
|
| 69 |
+
|
| 70 |
+
|
| 71 |
@mock.patch("pytube.captions.Caption.xml_captions")
|
| 72 |
def test_download_xml_and_trim_extension(xml):
|
| 73 |
m = mock_open()
|
|
|
|
| 78 |
)
|
| 79 |
caption.download("title.xml", srt=False)
|
| 80 |
assert m.call_args_list[0][0][0].split("/")[-1] == "title (en).xml"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_repr():
|
| 84 |
+
caption = Caption(
|
| 85 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
| 86 |
+
)
|
| 87 |
+
assert str(caption) == '<Caption lang="name1" code="en">'
|
tests/test_cli.py
CHANGED
|
@@ -79,3 +79,12 @@ def test_download_caption_with_language_not_found(youtube):
|
|
| 79 |
) as wrapped_all:
|
| 80 |
cli.download_caption(youtube, "blah")
|
| 81 |
wrapped_all.assert_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
) as wrapped_all:
|
| 80 |
cli.download_caption(youtube, "blah")
|
| 81 |
wrapped_all.assert_called()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@mock.patch("pytube.Stream")
|
| 85 |
+
@mock.patch("io.BufferedWriter")
|
| 86 |
+
def test_on_progress(stream, writer):
|
| 87 |
+
stream.filesize = 10
|
| 88 |
+
cli.display_progress_bar = MagicMock()
|
| 89 |
+
cli.on_progress(stream, "", writer, 7)
|
| 90 |
+
cli.display_progress_bar.assert_called_once_with(3, 10)
|