File size: 3,832 Bytes
ccd4117
 
 
 
 
328e22b
d2c119a
328e22b
ccd4117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328e22b
d2c119a
 
 
 
 
ccd4117
 
 
8b82476
328e22b
d2c119a
da19fc7
d2c119a
 
328e22b
8b82476
 
ccd4117
 
328e22b
d2c119a
 
 
328e22b
ccd4117
 
328e22b
d2c119a
 
 
328e22b
ccd4117
 
328e22b
d2c119a
 
 
ccd4117
328e22b
ccd4117
 
 
 
 
 
 
 
328e22b
d2c119a
 
 
ccd4117
328e22b
ccd4117
 
 
 
 
 
 
 
 
328e22b
d2c119a
 
 
ccd4117
328e22b
ccd4117
 
 
 
 
 
 
 
 
328e22b
d2c119a
 
 
328e22b
ccd4117
 
328e22b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
"""Unit tests for the :class:`StreamQuery <StreamQuery>` class."""
import pytest


def test_count(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.count` returns an accurate amount."""
    assert cipher_signature.streams.count() == 22


@pytest.mark.parametrize(
    'test_input,expected', [
        ({'progressive': True}, ['22', '43', '18', '36', '17']),
        ({'resolution': '720p'}, ['22', '136', '247']),
        ({'res': '720p'}, ['22', '136', '247']),
        ({'fps': 30, 'resolution': '480p'}, ['135', '244']),
        ({'mime_type': 'audio/mp4'}, ['140']),
        ({'type': 'audio'}, ['140', '171', '249', '250', '251']),
        ({'subtype': '3gpp'}, ['36', '17']),
        ({'abr': '128kbps'}, ['43', '140', '171']),
        ({'bitrate': '128kbps'}, ['43', '140', '171']),
        ({'audio_codec': 'vorbis'}, ['43', '171']),
        ({'video_codec': 'vp9'}, ['248', '247', '244', '243', '242', '278']),
        ({'only_audio': True}, ['140', '171', '249', '250', '251']),
        ({'only_video': True, 'video_codec': 'avc1.4d4015'}, ['133']),
        ({'progressive': True}, ['22', '43', '18', '36', '17']),
        ({'adaptive': True, 'resolution': '1080p'}, ['137', '248']),
        ({'custom_filter_functions': [lambda s: s.itag == '22']}, ['22']),
    ],
)
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).all()
    ]
    assert result == expected


@pytest.mark.parametrize('test_input', ['first', 'last'])
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.last().itag == '251'


def test_get_first(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.first` returns the expected
    :class:`Stream <Stream>`.
    """
    assert cipher_signature.streams.first().itag == '22'


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(progressive=True)
        .order_by('itag')
        .all()
    ]

    assert itags == ['17', '18', '22', '36', '43']


def test_order_by_descending(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
    :class:`Stream <Stream>` instances in the reverse order.
    """
    itags = [
        s.itag for s in cipher_signature.streams
        .filter(progressive=True)
        .order_by('itag')
        .desc()
        .all()
    ]

    assert itags == ['43', '36', '22', '18', '17']


def test_order_by_ascending(cipher_signature):
    """Ensure :meth:`~pytube.StreamQuery.desc` sorts the list of
    :class:`Stream <Stream>` instances in ascending order.
    """
    itags = [
        s.itag for s in cipher_signature.streams
        .filter(progressive=True)
        .order_by('itag')
        .asc()
        .all()
    ]

    assert itags == ['17', '18', '22', '36', '43']


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(22).itag == '22'


def test_get_by_non_existent_itag(cipher_signature):
    assert not cipher_signature.streams.get_by_itag(22983)