|
| 1 | +"""Tests for the YouTube client.""" |
| 2 | +from datetime import datetime, timezone |
| 3 | + |
| 4 | +import aiohttp |
| 5 | +import pytest |
| 6 | +from aresponses import ResponsesMockServer |
| 7 | + |
| 8 | +from youtubeaio.youtube import YouTube |
| 9 | + |
| 10 | +from . import load_fixture |
| 11 | +from .const import YOUTUBE_URL |
| 12 | + |
| 13 | + |
| 14 | +async def test_fetch_channel( |
| 15 | + aresponses: ResponsesMockServer, |
| 16 | +) -> None: |
| 17 | + """Test retrieving a channel.""" |
| 18 | + aresponses.add( |
| 19 | + YOUTUBE_URL, |
| 20 | + "/youtube/v3/channels", |
| 21 | + "GET", |
| 22 | + aresponses.Response( |
| 23 | + status=200, |
| 24 | + headers={"Content-Type": "application/json"}, |
| 25 | + text=load_fixture("channel_response_snippet.json"), |
| 26 | + ), |
| 27 | + ) |
| 28 | + async with aiohttp.ClientSession() as session: |
| 29 | + youtube = YouTube(session=session) |
| 30 | + channel_generator = youtube.get_channels( |
| 31 | + channel_ids=["UC_x5XG1OV2P6uZZ5FSM9Ttw"], |
| 32 | + ) |
| 33 | + channel = await channel_generator.__anext__() |
| 34 | + assert channel |
| 35 | + assert channel.snippet |
| 36 | + assert channel.snippet.published_at == datetime( |
| 37 | + 2007, |
| 38 | + 8, |
| 39 | + 23, |
| 40 | + 0, |
| 41 | + 34, |
| 42 | + 43, |
| 43 | + tzinfo=timezone.utc, |
| 44 | + ) |
| 45 | + with pytest.raises(StopAsyncIteration): |
| 46 | + await channel_generator.__anext__() |
| 47 | + await youtube.close() |
| 48 | + |
| 49 | + |
| 50 | +async def test_fetch_own_channel( |
| 51 | + aresponses: ResponsesMockServer, |
| 52 | +) -> None: |
| 53 | + """Test retrieving own channel.""" |
| 54 | + aresponses.add( |
| 55 | + YOUTUBE_URL, |
| 56 | + "/youtube/v3/channels?part=snippet&mine=true", |
| 57 | + "GET", |
| 58 | + aresponses.Response( |
| 59 | + status=200, |
| 60 | + headers={"Content-Type": "application/json"}, |
| 61 | + text=load_fixture("channel_response_snippet.json"), |
| 62 | + ), |
| 63 | + match_querystring=True, |
| 64 | + ) |
| 65 | + async with aiohttp.ClientSession() as session: |
| 66 | + youtube = YouTube(session=session) |
| 67 | + channel_generator = youtube.get_user_channels() |
| 68 | + channel = await channel_generator.__anext__() |
| 69 | + assert channel |
| 70 | + assert channel.snippet |
| 71 | + assert channel.snippet.published_at == datetime( |
| 72 | + 2007, |
| 73 | + 8, |
| 74 | + 23, |
| 75 | + 0, |
| 76 | + 34, |
| 77 | + 43, |
| 78 | + tzinfo=timezone.utc, |
| 79 | + ) |
| 80 | + with pytest.raises(StopAsyncIteration): |
| 81 | + await channel_generator.__anext__() |
| 82 | + await youtube.close() |
0 commit comments