File: test_playlist_item.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 (93 lines) | stat: -rw-r--r-- 2,866 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
"""Tests for the YouTube client."""

import json

import aiohttp
import pytest
from aresponses import ResponsesMockServer

from youtubeaio.types import PartMissingError
from youtubeaio.youtube import YouTube

from . import construct_fixture, load_fixture
from .const import YOUTUBE_URL


async def test_fetch_playlist_items(
    aresponses: ResponsesMockServer,
) -> None:
    """Test retrieving playlist items."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/playlistItems",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=load_fixture("playlist_item_response_snippet_content_details.json"),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        count = 0
        async for playlist_item in youtube.get_playlist_items(
            "UU_x5XG1OV2P6uZZ5FSM9Ttw",
        ):
            count += 1
            assert playlist_item
            assert playlist_item.snippet
            assert playlist_item.content_details
        assert count == 5
        await youtube.close()


async def test_nullable_fields(
    aresponses: ResponsesMockServer,
) -> None:
    """Check if the fields exist when they are filled."""
    aresponses.add(
        YOUTUBE_URL,
        "/youtube/v3/playlistItems",
        "GET",
        aresponses.Response(
            status=200,
            headers={"Content-Type": "application/json"},
            text=json.dumps(
                construct_fixture("playlist_item", ["snippet", "contentDetails"], 1),
            ),
        ),
    )
    async with aiohttp.ClientSession() as session:
        youtube = YouTube(session=session)
        async for playlist_item in youtube.get_playlist_items(
            "UU_x5XG1OV2P6uZZ5FSM9Ttw",
        ):
            assert playlist_item
            assert playlist_item.snippet
            assert playlist_item.content_details


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