hbmartin commited on
Commit
b2b9291
·
1 Parent(s): 1e829ab

added tests

Browse files
pytube/helpers.py CHANGED
@@ -138,7 +138,7 @@ def deprecated(reason: str) -> Callable:
138
  return decorator
139
 
140
 
141
- def target_directory(output_path: Optional[str]) -> str:
142
  """
143
  Function for determining target directory of a download.
144
  Returns an absolute path (if relative one given) or the current
 
138
  return decorator
139
 
140
 
141
+ def target_directory(output_path: Optional[str] = None) -> str:
142
  """
143
  Function for determining target directory of a download.
144
  Returns an absolute path (if relative one given) or the current
pytube/query.py CHANGED
@@ -284,7 +284,7 @@ class StreamQuery:
284
  """
285
  return self.filter(progressive=True).order_by("resolution").asc().last()
286
 
287
- def get_audio_only(self, codec: str = "mp4") -> Optional[Stream]:
288
  """Get highest bitrate audio stream for given codec (defaults to mp4)
289
 
290
  :param str codec:
@@ -296,8 +296,8 @@ class StreamQuery:
296
 
297
  """
298
  return (
299
- self.filter(only_audio=True, audio_codec=codec)
300
- .order_by("resolution")
301
  .asc()
302
  .last()
303
  )
 
284
  """
285
  return self.filter(progressive=True).order_by("resolution").asc().last()
286
 
287
+ def get_audio_only(self, subtype: str = "mp4") -> Optional[Stream]:
288
  """Get highest bitrate audio stream for given codec (defaults to mp4)
289
 
290
  :param str codec:
 
296
 
297
  """
298
  return (
299
+ self.filter(only_audio=True, subtype=subtype)
300
+ .order_by("abr")
301
  .asc()
302
  .last()
303
  )
tests/contrib/test_playlist.py CHANGED
@@ -95,3 +95,12 @@ def test_videos(youtube, request_get, playlist_html):
95
  playlist._find_load_more_url = MagicMock(return_value=None)
96
  request_get.assert_called()
97
  assert len(list(playlist.videos)) == 12
 
 
 
 
 
 
 
 
 
 
95
  playlist._find_load_more_url = MagicMock(return_value=None)
96
  request_get.assert_called()
97
  assert len(list(playlist.videos)) == 12
98
+
99
+
100
+ @mock.patch("pytube.contrib.playlist.request.get")
101
+ @mock.patch("pytube.contrib.playlist.install_proxy", return_value=None)
102
+ def test_proxy(install_proxy, request_get):
103
+ url = "https://www.fakeurl.com/playlist?list=whatever"
104
+ request_get.return_value = ""
105
+ Playlist(url, proxies={"http": "things"})
106
+ install_proxy.assert_called_with({"http": "things"})
tests/test_helpers.py CHANGED
@@ -1,11 +1,12 @@
1
  # -*- coding: utf-8 -*-
2
  from unittest import mock
 
3
 
4
  import pytest
5
 
6
  from pytube import helpers
7
  from pytube.exceptions import RegexMatchError
8
- from pytube.helpers import deprecated, cache
9
 
10
 
11
  def test_regex_search_no_match():
@@ -52,3 +53,25 @@ def test_cache():
52
  cached_func("bye")
53
 
54
  assert call_count == 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
  from unittest import mock
3
+ from unittest.mock import MagicMock
4
 
5
  import pytest
6
 
7
  from pytube import helpers
8
  from pytube.exceptions import RegexMatchError
9
+ from pytube.helpers import deprecated, cache, target_directory
10
 
11
 
12
  def test_regex_search_no_match():
 
53
  cached_func("bye")
54
 
55
  assert call_count == 2
56
+
57
+
58
+ @mock.patch("os.path.isabs", return_value=False)
59
+ @mock.patch("os.getcwd", return_value="/cwd")
60
+ @mock.patch("os.makedirs")
61
+ def test_target_directory_with_relative_path(_, __, makedirs):
62
+ assert target_directory("test") == "/cwd/test"
63
+ makedirs.assert_called()
64
+
65
+
66
+ @mock.patch("os.path.isabs", return_value=True)
67
+ @mock.patch("os.makedirs")
68
+ def test_target_directory_with_absolute_path(_, makedirs):
69
+ assert target_directory("/test") == "/test"
70
+ makedirs.assert_called()
71
+
72
+
73
+ @mock.patch("os.getcwd", return_value="/cwd")
74
+ @mock.patch("os.makedirs")
75
+ def test_target_directory_with_no_path(_, makedirs):
76
+ assert target_directory() == "/cwd"
77
+ makedirs.assert_called()
tests/test_query.py CHANGED
@@ -154,3 +154,11 @@ def test_filter_is_dash(cipher_signature):
154
  streams = cipher_signature.streams.filter(is_dash=False).all()
155
  itags = [s.itag for s in streams]
156
  assert itags == [18, 398, 397, 396, 395, 394]
 
 
 
 
 
 
 
 
 
154
  streams = cipher_signature.streams.filter(is_dash=False).all()
155
  itags = [s.itag for s in streams]
156
  assert itags == [18, 398, 397, 396, 395, 394]
157
+
158
+
159
+ def test_get_audio_only(cipher_signature):
160
+ assert cipher_signature.streams.get_audio_only().itag == 140
161
+
162
+
163
+ def test_get_audio_only_with_subtype(cipher_signature):
164
+ assert cipher_signature.streams.get_audio_only(subtype="webm").itag == 251