hbmartin commited on
Commit
2f4cd16
·
1 Parent(s): d2eed62

added playlist tests

Browse files
tests/conftest.py CHANGED
@@ -50,3 +50,15 @@ def age_restricted():
50
  """Youtube instance initialized with video id zRbsm3e2ltw."""
51
  filename = "yt-video-zRbsm3e2ltw-1507777044.json.gz"
52
  return load_playback_file(filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  """Youtube instance initialized with video id zRbsm3e2ltw."""
51
  filename = "yt-video-zRbsm3e2ltw-1507777044.json.gz"
52
  return load_playback_file(filename)
53
+
54
+
55
+ @pytest.fixture
56
+ def playlist_html():
57
+ """Youtube playlist HTML loaded on 2020-01-25 from
58
+ https://www.youtube.com/playlist?list=PLzMcBGfZo4-mP7qA9cagf68V06sko5otr"""
59
+ file_path = os.path.join(
60
+ os.path.dirname(os.path.realpath(__file__)), "mocks", "playlist.html"
61
+ )
62
+ with open(file_path, encoding="utf-8") as f:
63
+ read_data = f.read()
64
+ return read_data
tests/contrib/test_playlist.py CHANGED
@@ -1,5 +1,6 @@
1
  # -*- coding: utf-8 -*-
2
  from unittest import mock
 
3
 
4
  from pytube import Playlist
5
 
@@ -32,3 +33,51 @@ def test_init_with_watch_url():
32
  playlist.playlist_url
33
  == "https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X"
34
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # -*- coding: utf-8 -*-
2
  from unittest import mock
3
+ from unittest.mock import MagicMock
4
 
5
  from pytube import Playlist
6
 
 
33
  playlist.playlist_url
34
  == "https://www.youtube.com/playlist?list=PLynhp4cZEpTbRs_PYISQ8v_uwO0_mDg_X"
35
  )
36
+
37
+
38
+ @mock.patch("pytube.contrib.playlist.request.get")
39
+ def test_parse_links(request_get, playlist_html):
40
+ url = "https://www.fakeurl.com/playlist?list=whatever"
41
+ request_get.return_value = playlist_html
42
+ playlist = Playlist(url)
43
+ playlist._find_load_more_url = MagicMock(return_value=None)
44
+ links = playlist.parse_links()
45
+ request_get.assert_called()
46
+ assert links == [
47
+ "/watch?v=ujTCoH21GlA",
48
+ "/watch?v=45ryDIPHdGg",
49
+ "/watch?v=1BYu65vLKdA",
50
+ "/watch?v=3AQ_74xrch8",
51
+ "/watch?v=ddqQUz9mZaM",
52
+ "/watch?v=vwLT6bZrHEE",
53
+ "/watch?v=TQKI0KE-JYY",
54
+ "/watch?v=dNBvQ38MlT8",
55
+ "/watch?v=JHxyrMgOUWI",
56
+ "/watch?v=l2I8NycJMCY",
57
+ "/watch?v=g1Zbuk1gAfk",
58
+ "/watch?v=zixd-si9Q-o",
59
+ ]
60
+
61
+
62
+ @mock.patch("pytube.contrib.playlist.request.get")
63
+ def test_populate_video_urls(request_get, playlist_html):
64
+ url = "https://www.fakeurl.com/playlist?list=whatever"
65
+ request_get.return_value = playlist_html
66
+ playlist = Playlist(url)
67
+ playlist._find_load_more_url = MagicMock(return_value=None)
68
+ playlist.populate_video_urls()
69
+ request_get.assert_called()
70
+ assert playlist.video_urls == [
71
+ "https://www.youtube.com/watch?v=ujTCoH21GlA",
72
+ "https://www.youtube.com/watch?v=45ryDIPHdGg",
73
+ "https://www.youtube.com/watch?v=1BYu65vLKdA",
74
+ "https://www.youtube.com/watch?v=3AQ_74xrch8",
75
+ "https://www.youtube.com/watch?v=ddqQUz9mZaM",
76
+ "https://www.youtube.com/watch?v=vwLT6bZrHEE",
77
+ "https://www.youtube.com/watch?v=TQKI0KE-JYY",
78
+ "https://www.youtube.com/watch?v=dNBvQ38MlT8",
79
+ "https://www.youtube.com/watch?v=JHxyrMgOUWI",
80
+ "https://www.youtube.com/watch?v=l2I8NycJMCY",
81
+ "https://www.youtube.com/watch?v=g1Zbuk1gAfk",
82
+ "https://www.youtube.com/watch?v=zixd-si9Q-o",
83
+ ]
tests/mocks/playlist.html ADDED
The diff for this file is too large to render. See raw diff
 
tests/test_cli.py CHANGED
@@ -161,7 +161,7 @@ def test_main_download_caption(youtube):
161
  @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
162
  def test_download_by_resolution(youtube):
163
  parser = argparse.ArgumentParser()
164
- args = parse_args(parser, ["urlhere", "-r", "720p"])
165
  cli._parse_args = MagicMock(return_value=args)
166
  cli.download_by_resolution = MagicMock()
167
  cli.main()
 
161
  @mock.patch("pytube.cli.YouTube.__init__", return_value=None)
162
  def test_download_by_resolution(youtube):
163
  parser = argparse.ArgumentParser()
164
+ args = parse_args(parser, ["urlhere", "-r", "320p"])
165
  cli._parse_args = MagicMock(return_value=args)
166
  cli.download_by_resolution = MagicMock()
167
  cli.main()