test_backend.py 2.6 KB
Newer Older
1
from __future__ import unicode_literals
Nicolas Delaby's avatar
Nicolas Delaby committed
2

Nicolas Delaby's avatar
Nicolas Delaby committed
3
import os.path
4
5

import mock
Nicolas Delaby's avatar
Nicolas Delaby committed
6

7
import pafy
Nicolas Delaby's avatar
Nicolas Delaby committed
8

9
import pytest
Nicolas Delaby's avatar
Nicolas Delaby committed
10

11
12
13
import vcr

from mopidy_youtube import backend
14
from mopidy_youtube.backend import YouTubeLibraryProvider
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


@pytest.yield_fixture
def pafy_mock():
    patcher = mock.patch.object(backend, 'pafy', spec=pafy)
    yield patcher.start()
    patcher.stop()


@pytest.fixture
def pafy_mock_with_video(pafy_mock):
    video_mock = pafy_mock.new.return_value
    video_mock.bigthumb = 'big thumb'
    video_mock.bigthumbhd = 'big thumb in hd'
    video_mock.getbestaudio.return_value.url = 'http://example.com/'
    video_mock.length = 2000
    video_mock.title = 'a title'
    video_mock.videoid = 'a video id'

    return pafy_mock

Nicolas Delaby's avatar
Nicolas Delaby committed
36

Nicolas Delaby's avatar
Nicolas Delaby committed
37
38
39
40
41
42
here = os.path.abspath(os.path.dirname(__file__))

my_vcr = vcr.VCR(
    serializer='yaml',
    cassette_library_dir=os.path.join(here, 'fixtures'),
    record_mode='once',
Nicolas Delaby's avatar
Nicolas Delaby committed
43
    match_on=['method', 'scheme', 'host', 'port', 'path', 'query'],
Nicolas Delaby's avatar
Nicolas Delaby committed
44
45
)

46

Nicolas Delaby's avatar
Nicolas Delaby committed
47
@my_vcr.use_cassette('youtube_playlist_resolve.yaml')
48
49
50
def test_playlist_resolver(pafy_mock_with_video):
    videos = backend.resolve_playlist('PLOxORm4jpOQfMU7bpfGCzDyLropIYEHuR')

Nicolas Delaby's avatar
Nicolas Delaby committed
51
    assert len(videos) == 108
52
53


Nicolas Delaby's avatar
Nicolas Delaby committed
54
@my_vcr.use_cassette('youtube_search.yaml')
55
56
57
58
59
60
def test_search_yt(pafy_mock_with_video):
    videos = backend.search_youtube('chvrches')

    assert len(videos) == 15


Nicolas Delaby's avatar
Nicolas Delaby committed
61
@my_vcr.use_cassette('resolve_track.yaml')
62
def test_resolve_track(pafy_mock_with_video):
63
    video = backend.resolve_track('C0DPdy98e4c')
64
65
66
67

    assert video


Nicolas Delaby's avatar
Nicolas Delaby committed
68
@my_vcr.use_cassette('resolve_track_failed.yaml')
69
70
71
72
73
74
75
76
def test_resolve_track_failed(pafy_mock):
    pafy_mock.new.side_effect = Exception('Removed')

    video = backend.resolve_track('unknown')

    assert not video


Nicolas Delaby's avatar
Nicolas Delaby committed
77
@my_vcr.use_cassette('resolve_track_stream.yaml')
78
def test_resolve_track_stream(pafy_mock_with_video):
79
    video = backend.resolve_track('C0DPdy98e4c', stream=True)
80
81

    assert video
82
83


Nicolas Delaby's avatar
Nicolas Delaby committed
84
@my_vcr.use_cassette('resolve_video_track_stream.yaml')
85
86
87
88
89
90
def test_resolve_video_track_stream(pafy_mock_with_video):
    video = backend.resolve_track('youtube:video/a title.a video id',
                                  stream=True)

    assert video
    assert video.uri == "http://example.com/"
91
92


Nicolas Delaby's avatar
Nicolas Delaby committed
93
@my_vcr.use_cassette('lookup_video_uri.yaml')
94
95
96
97
98
99
100
101
102
103
104
105
106
def test_lookup_video_uri(caplog):
    provider = YouTubeLibraryProvider(mock.PropertyMock())

    track = provider.lookup(backend.video_uri_prefix +
                            '/a title.C0DPdy98e4c')

    assert 'Need 11 character video id or the URL of the video.' \
           not in caplog.text()

    assert track

    assert track[0].uri == backend.video_uri_prefix + \
        '/TEST VIDEO.C0DPdy98e4c'