add tests for cli
Browse files- pytube/cli.py +4 -3
- tests/test_cli.py +75 -6
pytube/cli.py
CHANGED
@@ -159,7 +159,7 @@ def on_progress(
|
|
159 |
display_progress_bar(bytes_received, filesize)
|
160 |
|
161 |
|
162 |
-
def download(youtube: YouTube, itag:
|
163 |
"""Start downloading a YouTube video.
|
164 |
|
165 |
:param YouTube youtube:
|
@@ -170,13 +170,14 @@ def download(youtube: YouTube, itag: str) -> None:
|
|
170 |
"""
|
171 |
# TODO(nficano): allow download target to be specified
|
172 |
# TODO(nficano): allow dash itags to be selected
|
173 |
-
youtube.
|
174 |
-
stream = youtube.streams.get_by_itag(int(itag))
|
175 |
if stream is None:
|
176 |
print("Could not find a stream with itag: {itag}".format(itag=itag))
|
177 |
print("Try one of these:")
|
178 |
display_streams(youtube)
|
179 |
sys.exit()
|
|
|
|
|
180 |
print("\n{fn} | {fs} bytes".format(fn=stream.default_filename, fs=stream.filesize,))
|
181 |
try:
|
182 |
stream.download()
|
|
|
159 |
display_progress_bar(bytes_received, filesize)
|
160 |
|
161 |
|
162 |
+
def download(youtube: YouTube, itag: int) -> None:
|
163 |
"""Start downloading a YouTube video.
|
164 |
|
165 |
:param YouTube youtube:
|
|
|
170 |
"""
|
171 |
# TODO(nficano): allow download target to be specified
|
172 |
# TODO(nficano): allow dash itags to be selected
|
173 |
+
stream = youtube.streams.get_by_itag(itag)
|
|
|
174 |
if stream is None:
|
175 |
print("Could not find a stream with itag: {itag}".format(itag=itag))
|
176 |
print("Try one of these:")
|
177 |
display_streams(youtube)
|
178 |
sys.exit()
|
179 |
+
|
180 |
+
youtube.register_on_progress_callback(on_progress)
|
181 |
print("\n{fn} | {fs} bytes".format(fn=stream.default_filename, fs=stream.filesize,))
|
182 |
try:
|
183 |
stream.download()
|
tests/test_cli.py
CHANGED
@@ -1,12 +1,81 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
from unittest import mock
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
@mock.patch("pytube.cli.YouTube")
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
from unittest import mock
|
3 |
+
from unittest.mock import MagicMock, patch
|
4 |
|
5 |
+
import pytest
|
6 |
+
|
7 |
+
from pytube import cli, StreamQuery, Caption, CaptionQuery
|
8 |
+
|
9 |
+
|
10 |
+
@mock.patch("pytube.cli.YouTube")
|
11 |
+
def test_download_when_itag_not_found(youtube):
|
12 |
+
youtube.streams = mock.Mock()
|
13 |
+
youtube.streams.all.return_value = []
|
14 |
+
youtube.streams.get_by_itag.return_value = None
|
15 |
+
with pytest.raises(SystemExit):
|
16 |
+
cli.download(youtube, 123)
|
17 |
+
youtube.streams.get_by_itag.assert_called_with(123)
|
18 |
+
|
19 |
+
|
20 |
+
@mock.patch("pytube.cli.YouTube")
|
21 |
+
@mock.patch("pytube.Stream")
|
22 |
+
def test_download_when_itag_is_found(youtube, stream):
|
23 |
+
stream.itag = 123
|
24 |
+
youtube.streams = StreamQuery([stream])
|
25 |
+
with patch.object(
|
26 |
+
youtube.streams, "get_by_itag", wraps=youtube.streams.get_by_itag
|
27 |
+
) as wrapped_itag:
|
28 |
+
cli.download(youtube, 123)
|
29 |
+
wrapped_itag.assert_called_with(123)
|
30 |
+
youtube.register_on_progress_callback.assert_called_with(cli.on_progress)
|
31 |
+
stream.download.assert_called()
|
32 |
+
|
33 |
+
|
34 |
+
@mock.patch("pytube.cli.YouTube")
|
35 |
+
@mock.patch("pytube.Stream")
|
36 |
+
def test_display_stream(youtube, stream):
|
37 |
+
stream.itag = 123
|
38 |
+
stream.__repr__ = MagicMock(return_value="")
|
39 |
+
youtube.streams = StreamQuery([stream])
|
40 |
+
with patch.object(youtube.streams, "all", wraps=youtube.streams.all) as wrapped_all:
|
41 |
+
cli.display_streams(youtube)
|
42 |
+
wrapped_all.assert_called()
|
43 |
+
stream.__repr__.assert_called()
|
44 |
+
|
45 |
+
|
46 |
+
@mock.patch("pytube.cli.YouTube")
|
47 |
+
def test_download_caption_with_none(youtube):
|
48 |
+
caption = Caption(
|
49 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
50 |
+
)
|
51 |
+
youtube.captions = CaptionQuery([caption])
|
52 |
+
with patch.object(
|
53 |
+
youtube.captions, "all", wraps=youtube.captions.all
|
54 |
+
) as wrapped_all:
|
55 |
+
cli.download_caption(youtube, None)
|
56 |
+
wrapped_all.assert_called()
|
57 |
+
|
58 |
+
|
59 |
+
@mock.patch("pytube.cli.YouTube")
|
60 |
+
def test_download_caption_with_language_found(youtube):
|
61 |
+
youtube.title = "video title"
|
62 |
+
caption = Caption(
|
63 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
64 |
+
)
|
65 |
+
caption.download = MagicMock(return_value="file_path")
|
66 |
+
youtube.captions = CaptionQuery([caption])
|
67 |
+
cli.download_caption(youtube, "en")
|
68 |
+
caption.download.assert_called_with(title="video title")
|
69 |
|
70 |
|
71 |
@mock.patch("pytube.cli.YouTube")
|
72 |
+
def test_download_caption_with_language_not_found(youtube):
|
73 |
+
caption = Caption(
|
74 |
+
{"url": "url1", "name": {"simpleText": "name1"}, "languageCode": "en"}
|
75 |
+
)
|
76 |
+
youtube.captions = CaptionQuery([caption])
|
77 |
+
with patch.object(
|
78 |
+
youtube.captions, "all", wraps=youtube.captions.all
|
79 |
+
) as wrapped_all:
|
80 |
+
cli.download_caption(youtube, "blah")
|
81 |
+
wrapped_all.assert_called()
|