File: test_channel.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 (181 lines) | stat: -rw-r--r-- 5,411 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
"""Tests for the YouTube client."""

import json
from datetime import datetime, timezone

import aiohttp
import pytest
from aresponses import ResponsesMockServer

from youtubeaio.models import YouTubeChannelThumbnails
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_channel(
    aresponses: ResponsesMockServer,
) -> None:
    """Test retrieving a channel."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/channels",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("channel_response_snippet.json"),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        channel_generator = youtube.get_channels(
            channel_ids=["UC_x5XG1OV2P6uZZ5FSM9Ttw"],
        )
        channel = await channel_generator.__anext__()
        assert channel
        assert channel.channel_id == "UC_x5XG1OV2P6uZZ5FSM9Ttw"
        assert channel.upload_playlist_id == "UU_x5XG1OV2P6uZZ5FSM9Ttw"
        assert channel.snippet
        assert channel.snippet.published_at == datetime(
            2007,
            8,
            23,
            0,
            34,
            43,
            tzinfo=timezone.utc,
        )
        with pytest.raises(StopAsyncIteration):
            await channel_generator.__anext__()
        await youtube.close()


async def test_fetch_own_channel(
    aresponses: ResponsesMockServer,
) -> None:
    """Test retrieving own channel."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/channels?part=snippet&mine=true",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("channel_response_snippet.json"),
        ),
        match_querystring=True,
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        channel_generator = youtube.get_user_channels()
        channel = await channel_generator.__anext__()
        assert channel
        assert channel.snippet
        assert channel.snippet.published_at == datetime(
            2007,
            8,
            23,
            0,
            34,
            43,
            tzinfo=timezone.utc,
        )
        with pytest.raises(StopAsyncIteration):
            await channel_generator.__anext__()
        await youtube.close()


@pytest.mark.parametrize(
    ("thumbnails", "result_url"),
    [
        (
            YouTubeChannelThumbnails(
                high=get_thumbnail("high"),
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "high",
        ),
        (
            YouTubeChannelThumbnails(
                high=None,
                medium=get_thumbnail("medium"),
                default=get_thumbnail("default"),
            ),
            "medium",
        ),
        (
            YouTubeChannelThumbnails(
                high=None,
                medium=None,
                default=get_thumbnail("default"),
            ),
            "default",
        ),
    ],
)
async def test_get_hq_thumbnail(
    thumbnails: YouTubeChannelThumbnails,
    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/channels",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(
                construct_fixture(
                    "channel",
                    ["snippet", "contentDetails", "statistics"],
                    1,
                ),
            ),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        async for subscription in youtube.get_user_channels():
            assert subscription
            assert subscription.snippet
            assert subscription.content_details
            assert subscription.statistics


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/channels",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(construct_fixture("channel", [], 1)),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        async for subscription in youtube.get_user_channels():
            assert subscription
            with pytest.raises(PartMissingError):
                assert subscription.snippet
            with pytest.raises(PartMissingError):
                assert subscription.content_details
            with pytest.raises(PartMissingError):
                assert subscription.statistics