nficano commited on
Commit
471986f
·
2 Parent(s): 416263e 8b33d56

Merge branch 'nosetests'

Browse files

* nosetests:
made all tests deterministic.
mocked all the html and js with local fixtures
added mock to test requirements
updated description/how files are read
changes for nose test, added mock data

setup.py CHANGED
@@ -1,19 +1,17 @@
1
  #!/usr/bin/env python
2
  # -*- coding: utf-8 -*-
3
-
4
  from pytube import __version__
5
- import os
6
-
7
 
8
  try:
9
  from setuptools import setup
10
  except ImportError:
11
  from distutils.core import setup
12
 
 
 
13
 
14
- def open_file(fname):
15
- return open(os.path.join(os.path.dirname(__file__), fname))
16
-
17
 
18
  setup(
19
  name="pytube",
@@ -21,8 +19,7 @@ setup(
21
  author="Nick Ficano",
22
  author_email="nficano@gmail.com",
23
  packages=['pytube'],
24
- url="http://pytube.nickficano.com",
25
- license=open_file('LICENSE.txt').read(),
26
  scripts=['scripts/pytube'],
27
  classifiers=[
28
  "Development Status :: 4 - Beta",
@@ -40,8 +37,8 @@ setup(
40
  "Topic :: Internet",
41
  "Topic :: Multimedia :: Video"
42
  ],
43
- description="A simple, yet versatile package for downloading "
44
- "YouTube videos.",
45
- long_description=open_file('README.rst').read(),
46
  zip_safe=True,
47
  )
 
1
  #!/usr/bin/env python
2
  # -*- coding: utf-8 -*-
 
3
  from pytube import __version__
 
 
4
 
5
  try:
6
  from setuptools import setup
7
  except ImportError:
8
  from distutils.core import setup
9
 
10
+ with open('README.rst') as readme_file:
11
+ readme = readme_file.read()
12
 
13
+ with open('LICENSE.txt') as readme_file:
14
+ license = readme_file.read()
 
15
 
16
  setup(
17
  name="pytube",
 
19
  author="Nick Ficano",
20
  author_email="nficano@gmail.com",
21
  packages=['pytube'],
22
+ license=license,
 
23
  scripts=['scripts/pytube'],
24
  classifiers=[
25
  "Development Status :: 4 - Beta",
 
37
  "Topic :: Internet",
38
  "Topic :: Multimedia :: Video"
39
  ],
40
+ description=("A simple, yet versatile Python library (and command-line) "
41
+ "for downloading YouTube videos."),
42
+ long_description=readme,
43
  zip_safe=True,
44
  )
tests/mock_video.html ADDED
The diff for this file is too large to render. See raw diff
 
tests/mock_video.js ADDED
The diff for this file is too large to render. See raw diff
 
tests/test_pytube.py CHANGED
@@ -1,60 +1,71 @@
1
  #!/usr/bin/env/python
2
  # -*- coding: utf-8 -*-
3
 
4
- import unittest
5
- from pytube import YouTube
 
6
  from pytube.exceptions import MultipleObjectsReturned
7
 
8
- SHOW_ERROR_MESSAGES = True
9
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- class TestYouTube(unittest.TestCase):
12
- """Test all methods of Youtube class"""
 
13
 
14
- def setUp(self):
15
- """Set up the all attributes required for a particular video."""
16
-
17
- self.url = "https://www.youtube.com/watch?v=Ik-RsDGPI5Y"
18
- self.video_id = 'Ik-RsDGPI5Y'
19
- self.filename = 'Pulp Fiction - Dancing Scene'
20
- self.yt = YouTube(self.url)
21
- #: don't hard code, make is universal
22
- self.videos = [
23
- '<Video: MPEG-4 Visual (.3gp) - 144p - Simple>',
24
- '<Video: MPEG-4 Visual (.3gp) - 240p - Simple>',
25
- '<Video: Sorenson H.263 (.flv) - 240p - N/A>',
26
- '<Video: H.264 (.mp4) - 360p - Baseline>',
27
- '<Video: H.264 (.mp4) - 720p - High>',
28
- '<Video: VP8 (.webm) - 360p - N/A>'
29
- ]
30
- # using flv since it has only once video
31
- self.flv = '<Video: Sorenson H.263 (.flv) - 240p - N/A>'
32
-
33
- def test_url(self):
34
- self.assertEqual(self.yt.url, self.url)
35
-
36
- def test_video_id(self):
37
- self.assertEqual(self.yt.video_id, self.video_id)
38
-
39
- def test_filename(self):
40
- self.assertEqual(self.yt.filename, self.filename)
41
-
42
- def test_get_videos(self):
43
- self.assertEqual(map(str, self.yt.get_videos()), self.videos)
44
-
45
- def test_get_video_data(self):
46
- self.assertEqual((self.yt.get_video_data()['args']['loaderUrl']),
47
- self.url)
48
-
49
- def test_get_false(self):
50
- with self.assertRaises(MultipleObjectsReturned):
51
- self.yt.get()
52
-
53
- def test_get_true(self):
54
- self.assertEqual(str(self.yt.get('flv')), self.flv)
55
-
56
- def test_filter(self):
57
- self.assertEqual(str(self.yt.filter('flv')[0]), self.flv)
58
-
59
- if __name__ == '__main__':
60
- unittest.main()
 
1
  #!/usr/bin/env/python
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')
tests/test_requirements.txt CHANGED
@@ -1 +1,2 @@
 
1
  nose==1.3.7
 
1
+ mock==1.3.0
2
  nose==1.3.7