Skip to content

Commit

Permalink
Fix test to no longer make web requests. (pytube#1048)
Browse files Browse the repository at this point in the history
* Update mock so that stream raises HTTPError, rather than head, and simplified some other mocks.

* pep8-naming updated, adding a new ignore
  • Loading branch information
tfdahlin authored Jul 7, 2021
1 parent cf5a7e7 commit 9a86686
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore = E231,E203,W503,Q000,WPS111,WPS305,WPS348,WPS602,D400,DAR201,S101,DAR101,C812,D104,I001,WPS306,WPS214,D401,WPS229,WPS420,WPS230,WPS414,WPS114,WPS226,WPS442,C819,WPS601,T001,RST304,WPS410,WPS428,A003,A002,I003,WPS221,WPS326,WPS201,S405,DAR301,WPS210,WPS202,WPS213,WPS301,P103,WPS407,WPS432,WPS211,S314,S310,S001,IF100,PT001,PT019,R504
ignore = E231,E203,W503,Q000,WPS111,WPS305,WPS348,WPS602,D400,DAR201,S101,DAR101,C812,D104,I001,WPS306,WPS214,D401,WPS229,WPS420,WPS230,WPS414,WPS114,WPS226,WPS442,C819,WPS601,T001,RST304,WPS410,WPS428,A003,A002,I003,WPS221,WPS326,WPS201,S405,DAR301,WPS210,WPS202,WPS213,WPS301,P103,WPS407,WPS432,WPS211,S314,S310,S001,IF100,PT001,PT019,R504,N818
max-line-length = 95

[isort]
18 changes: 9 additions & 9 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_views(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "6796391"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
def test_download(cipher_signature):
Expand All @@ -127,7 +127,7 @@ def test_download(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
@mock.patch("pytube.streams.target_directory", MagicMock(return_value="/target"))
Expand All @@ -145,7 +145,7 @@ def test_download_with_prefix(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
@mock.patch("pytube.streams.target_directory", MagicMock(return_value="/target"))
Expand All @@ -163,7 +163,7 @@ def test_download_with_filename(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
@mock.patch("pytube.streams.target_directory", MagicMock(return_value="/target"))
Expand All @@ -184,7 +184,7 @@ def test_download_with_existing(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
@mock.patch("pytube.streams.target_directory", MagicMock(return_value="/target"))
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_progressive_streams_return_includes_video_track(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
def test_on_progress_hook(cipher_signature):
Expand All @@ -236,7 +236,7 @@ def test_on_progress_hook(cipher_signature):
"pytube.streams.request.head", MagicMock(return_value={"content-length": "16384"})
)
@mock.patch(
"pytube.streams.request.stream",
"pytube.request.stream",
MagicMock(return_value=iter([str(random.getrandbits(8 * 1024))])),
)
def test_on_complete_hook(cipher_signature):
Expand Down Expand Up @@ -382,8 +382,8 @@ def test_segmented_stream_on_404(cipher_signature):

def test_segmented_only_catches_404(cipher_signature):
stream = cipher_signature.streams.filter(adaptive=True)[0]
with mock.patch('pytube.request.head') as mock_head:
mock_head.side_effect = HTTPError('', 403, 'Forbidden', '', '')
with mock.patch('pytube.request.stream') as mock_stream:
mock_stream.side_effect = HTTPError('', 403, 'Forbidden', '', '')
with mock.patch("pytube.streams.open", mock.mock_open(), create=True):
with pytest.raises(HTTPError):
stream.download()

0 comments on commit 9a86686

Please sign in to comment.