File: test_video.py

package info (click to toggle)
python-youtubeaio 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: python: 1,549; makefile: 3
file content (226 lines) | stat: -rw-r--r-- 6,851 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""Tests for the YouTube client."""

import json
from datetime import timedelta

import aiohttp
import pytest
from aiohttp.web_request import BaseRequest
from aresponses import Response, ResponsesMockServer
from syrupy import SnapshotAssertion

from youtubeaio.helper import first
from youtubeaio.models import YouTubeVideoThumbnails
from youtubeaio.types import PartMissingError
from youtubeaio.youtube import YouTube

from . import construct_fixture, load_fixture
from .const import YOUTUBE_URL
from .helper import get_thumbnail


async def test_fetch_video(
    aresponses: ResponsesMockServer,
    snapshot: SnapshotAssertion,
) -> None:
    """Test retrieving a video."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/videos",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(
                construct_fixture("video", ["snippet", "contentDetails"], 1),
            ),
        ),
    )
    async with aiohttp.ClientSession() as session, YouTube(session=session) as youtube:
        video = await youtube.get_video(video_id="Ks-_Mh1QhMc")
        assert video == snapshot


async def test_fetch_videos(
    aresponses: ResponsesMockServer,
) -> None:
    """Test retrieving a list of videos."""

    async def response_handler(req: BaseRequest) -> Response:
        """Response handler for this test."""
        if "pageToken" in req.query:
            fixture = "video_response_2.json"
        else:
            fixture = "video_response_1.json"
        return aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture(fixture),
        )

    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/videos",
        "GET",
        response_handler,
        repeat=2,
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        videos = youtube.get_videos(
            video_ids=["Ks-_Mh1QhMc", "GvgqDSnpRQM", "V4DDt30Aat4"],
        )
        video1 = await videos.__anext__()
        assert video1
        assert video1.video_id == "Ks-_Mh1QhMc"
        video2 = await videos.__anext__()
        assert video2
        assert video2.video_id == "GvgqDSnpRQM"
        video3 = await videos.__anext__()
        assert video3
        assert video3.video_id == "V4DDt30Aat4"


async def test_fetch_single_page_video(
    aresponses: ResponsesMockServer,
) -> None:
    """Test retrieving a page of videos."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/videos",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("video_response_2.json"),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        videos = youtube.get_videos(
            video_ids=["V4DDt30Aat4"],
        )
        video3 = await videos.__anext__()
        assert video3
        assert video3.video_id == "V4DDt30Aat4"
        with pytest.raises(StopAsyncIteration):
            await videos.__anext__()


async def test_fetch_no_videos() -> None:
    """Test retrieving no videos."""
    youtube = YouTube()
    with pytest.raises(ValueError):
        await first(youtube.get_videos(video_ids=[]))


@pytest.mark.parametrize(
    ("thumbnails", "result_url"),
    [
        (
            YouTubeVideoThumbnails(
                maxres=get_thumbnail("maxres"),
                standard=get_thumbnail("standard"),
                high=get_thumbnail("high"),
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "maxres",
        ),
        (
            YouTubeVideoThumbnails(
                maxres=None,
                standard=get_thumbnail("standard"),
                high=get_thumbnail("high"),
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "standard",
        ),
        (
            YouTubeVideoThumbnails(
                maxres=None,
                standard=None,
                high=get_thumbnail("high"),
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "high",
        ),
        (
            YouTubeVideoThumbnails(
                maxres=None,
                standard=None,
                high=None,
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "medium",
        ),
        (
            YouTubeVideoThumbnails(
                maxres=None,
                standard=None,
                high=None,
                medium=None,
                default=get_thumbnail("default"),
            ),
            "default",
        ),
    ],
)
async def test_get_hq_thumbnail(
    thumbnails: YouTubeVideoThumbnails,
    result_url: str,
) -> None:
    """Check if the highest quality thumbnail is returned."""
    assert thumbnails.get_highest_quality().url == result_url


async def test_nullable_fields(
    aresponses: ResponsesMockServer,
) -> None:
    """Check if the fields exist when they are filled."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/videos",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(
                construct_fixture("video", ["snippet", "contentDetails"], 1),
            ),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        video = await youtube.get_video(video_id="V4DDt30Aat4")
        assert video
        assert video.snippet.channel_id == "UCAuUUnT6oDeKwE6v1NGQxug"
        assert video.content_details.duration == timedelta(minutes=21, seconds=3)
        assert video.content_details.caption is True


async def test_nullable_fields_null(
    aresponses: ResponsesMockServer,
) -> None:
    """Check if an error is thrown if a non-requested part is accessed."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/videos",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(construct_fixture("video", [], 1)),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        video = await youtube.get_video(video_id="V4DDt30Aat4")
        assert video
        with pytest.raises(PartMissingError):
            assert video.snippet.thumbnails
        with pytest.raises(PartMissingError):
            assert video.content_details