Daniel Crane
[Bug Fix] Change from hardcoded FPS to dynamically extract from stream data (#920)
bdaa6bc
unverified
# -*- coding: utf-8 -*- | |
"""Unit tests for the :class:`StreamQuery <StreamQuery>` class.""" | |
import pytest | |
def test_filters(test_input, expected, cipher_signature): | |
"""Ensure filters produce the expected results.""" | |
result = [s.itag for s in cipher_signature.streams.filter(**test_input)] | |
assert result == expected | |
def test_empty(test_input, cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.last` and | |
:meth:`~pytube.StreamQuery.first` return None if the resultset is | |
empty. | |
""" | |
query = cipher_signature.streams.filter(video_codec="vp20") | |
fn = getattr(query, test_input) | |
assert fn() is None | |
def test_get_last(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.last` returns the expected | |
:class:`Stream <Stream>`. | |
""" | |
assert cipher_signature.streams[-1].itag == 251 | |
def test_get_first(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.first` returns the expected | |
:class:`Stream <Stream>`. | |
""" | |
assert cipher_signature.streams[0].itag == 18 | |
def test_order_by(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.order_by` sorts the list of | |
:class:`Stream <Stream>` instances in the expected order. | |
""" | |
itags = [ | |
s.itag | |
for s in cipher_signature.streams.filter(type="audio").order_by("itag") | |
] | |
assert itags == [140, 249, 250, 251] | |
def test_order_by_descending(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of | |
:class:`Stream <Stream>` instances in the reverse order. | |
""" | |
# numerical values | |
itags = [ | |
s.itag | |
for s in cipher_signature.streams.filter(type="audio") | |
.order_by("itag") | |
.desc() | |
] | |
assert itags == [251, 250, 249, 140] | |
def test_order_by_non_numerical(cipher_signature): | |
mime_types = [ | |
s.mime_type | |
for s in cipher_signature.streams.filter(res="360p") | |
.order_by("mime_type") | |
.desc() | |
] | |
assert mime_types == ["video/webm", "video/mp4", "video/mp4", "video/mp4"] | |
def test_order_by_ascending(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of | |
:class:`Stream <Stream>` instances in ascending order. | |
""" | |
# numerical values | |
itags = [ | |
s.itag | |
for s in cipher_signature.streams.filter(type="audio") | |
.order_by("itag") | |
.asc() | |
] | |
assert itags == [140, 249, 250, 251] | |
def test_order_by_non_numerical_ascending(cipher_signature): | |
mime_types = [ | |
s.mime_type | |
for s in cipher_signature.streams.filter(res="360p") | |
.order_by("mime_type") | |
.asc() | |
] | |
assert mime_types == ["video/mp4", "video/mp4", "video/mp4", "video/webm"] | |
def test_order_by_with_none_values(cipher_signature): | |
abrs = [s.abr for s in cipher_signature.streams.order_by("abr").asc()] | |
assert abrs == ["50kbps", "70kbps", "96kbps", "128kbps", "160kbps", "192kbps"] | |
def test_get_by_itag(cipher_signature): | |
"""Ensure :meth:`~pytube.StreamQuery.get_by_itag` returns the expected | |
:class:`Stream <Stream>`. | |
""" | |
assert cipher_signature.streams.get_by_itag(18).itag == 18 | |
def test_get_by_non_existent_itag(cipher_signature): | |
assert not cipher_signature.streams.get_by_itag(22983) | |
def test_get_by_resolution(cipher_signature): | |
assert cipher_signature.streams.get_by_resolution("360p").itag == 18 | |
def test_get_lowest_resolution(cipher_signature): | |
assert cipher_signature.streams.get_lowest_resolution().itag == 18 | |
def test_get_highest_resolution(cipher_signature): | |
assert cipher_signature.streams.get_highest_resolution().itag == 22 | |
def test_filter_is_dash(cipher_signature): | |
streams = cipher_signature.streams.filter(is_dash=False) | |
itags = [s.itag for s in streams] | |
assert itags == [18, 22] | |
def test_get_audio_only(cipher_signature): | |
assert cipher_signature.streams.get_audio_only().itag == 140 | |
def test_get_audio_only_with_subtype(cipher_signature): | |
assert cipher_signature.streams.get_audio_only(subtype="webm").itag == 251 | |
def test_sequence(cipher_signature): | |
assert len(cipher_signature.streams) == 24 | |
assert cipher_signature.streams[0] is not None | |
def test_otf(cipher_signature): | |
non_otf = cipher_signature.streams.otf() | |
assert len(non_otf) == 24 | |
otf = cipher_signature.streams.otf(True) | |
assert len(otf) == 0 | |
def test_repr(cipher_signature): | |
assert repr( | |
cipher_signature.streams.filter( | |
progressive=True, subtype="mp4", resolution="360p" | |
) | |
) == ( | |
'[<Stream: itag="18" mime_type="video/mp4" ' | |
'res="360p" fps="24fps" vcodec="avc1.42001E" ' | |
'acodec="mp4a.40.2" progressive="True" type="video">]' | |
) | |