nficano commited on
Commit
8b33d56
·
1 Parent(s): adfdf3f

made all tests deterministic.

Browse files
Files changed (1) hide show
  1. tests/test_pytube.py +56 -7
tests/test_pytube.py CHANGED
@@ -2,21 +2,70 @@
2
  # -*- coding: utf-8 -*-
3
 
4
  import mock
5
- from nose.tools import eq_
6
  from pytube import api
 
7
 
8
 
9
  class TestPytube(object):
10
  def setUp(self):
11
- url = 'http://www.youtube.com/watch?v=Ik-RsDGPI5Y'
12
- mock_js = open('tests/mock_video.js')
13
- mock_html = open('tests/mock_video.html')
14
  with mock.patch('pytube.api.urlopen') as urlopen:
15
- urlopen.return_value.read.return_value = mock_html.read()
16
  self.yt = api.YouTube()
17
- self.yt._js_code = mock_js.read()
18
  self.yt.from_url(url)
19
 
20
  def test_get_video_id(self):
21
  """Resolve the video id from url"""
22
- eq_(self.yt.video_id, 'Ik-RsDGPI5Y')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  # -*- coding: utf-8 -*-
3
 
4
  import mock
5
+ from nose.tools import eq_, raises
6
  from pytube import api
7
+ from pytube.exceptions import MultipleObjectsReturned
8
 
9
 
10
  class TestPytube(object):
11
  def setUp(self):
12
+ url = 'http://www.youtube.com/watch?v=9bZkp7q19f0'
13
+ self.mock_js = open('tests/mock_video.js').read()
14
+ self.mock_html = open('tests/mock_video.html').read()
15
  with mock.patch('pytube.api.urlopen') as urlopen:
16
+ urlopen.return_value.read.return_value = self.mock_html
17
  self.yt = api.YouTube()
18
+ self.yt._js_code = self.mock_js
19
  self.yt.from_url(url)
20
 
21
  def test_get_video_id(self):
22
  """Resolve the video id from url"""
23
+ eq_(self.yt.video_id, '9bZkp7q19f0')
24
+
25
+ def test_auto_filename(self):
26
+ """Generate safe filename based on video title"""
27
+ expected = u'PSY - GANGNAM STYLE(\uac15\ub0a8\uc2a4\ud0c0\uc77c) MV'
28
+
29
+ eq_(self.yt.filename, expected)
30
+
31
+ def test_manual_filename(self):
32
+ """Manually set a filename"""
33
+ expected = u'PSY - Gangnam Style'
34
+
35
+ self.yt.set_filename(expected)
36
+ eq_(self.yt.filename, expected)
37
+
38
+ def test_get_all_videos(self):
39
+ """Get all videos"""
40
+ eq_(len(self.yt.get_videos()), 6)
41
+
42
+ def test_filter_video_by_extension(self):
43
+ """Filter videos by filetype"""
44
+ eq_(len(self.yt.filter('mp4')), 2)
45
+ eq_(len(self.yt.filter('3gp')), 2)
46
+ eq_(len(self.yt.filter('webm')), 1)
47
+ eq_(len(self.yt.filter('flv')), 1)
48
+
49
+ def test_filter_video_by_extension_and_resolution(self):
50
+ """Filter videos by file extension and resolution"""
51
+ eq_(len(self.yt.filter('mp4', '720p')), 1)
52
+ eq_(len(self.yt.filter('mp4', '1080p')), 0)
53
+
54
+ def test_filter_video_by_extension_resolution_profile(self):
55
+ """Filter videos by file extension, resolution, and profile"""
56
+ eq_(len(self.yt.filter('mp4', '360p', 'Baseline')), 1)
57
+
58
+ def test_filter_video_by_profile(self):
59
+ """Filter videos by file profile"""
60
+ eq_(len(self.yt.filter(profile='Simple')), 2)
61
+
62
+ def test_filter_video_by_resolution(self):
63
+ """Filter videos by resolution"""
64
+ eq_(len(self.yt.filter(resolution='360p')), 2)
65
+
66
+ @raises(MultipleObjectsReturned)
67
+ def test_get_multiple_itmes(self):
68
+ """.get(...) cannot return more than one video"""
69
+ self.yt.get(profile='Simple')
70
+ self.yt.get('mp4')
71
+ self.yt.get(resolution='240p')