From f98274f0ecd20583e8a0eaf13b9bf145f9f9927e Mon Sep 17 00:00:00 2001 From: sigma67 Date: Sun, 4 Feb 2024 16:59:24 +0100 Subject: [PATCH] improve coverage --- tests/mixins/test_browsing.py | 4 ++++ tests/mixins/test_watch.py | 7 +++++++ ytmusicapi/parsers/_utils.py | 11 ++--------- ytmusicapi/parsers/explore.py | 16 ---------------- ytmusicapi/parsers/songs.py | 3 ++- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/tests/mixins/test_browsing.py b/tests/mixins/test_browsing.py index cdd98b8..8dea6d7 100644 --- a/tests/mixins/test_browsing.py +++ b/tests/mixins/test_browsing.py @@ -86,6 +86,10 @@ def test_get_album(self, yt, yt_auth, sample_album): album = yt.get_album("MPREb_YuigcYm2erf") # album with track (#8) disabled/greyed out assert album["tracks"][7]["trackNumber"] is None + def test_get_album_errors(self, yt): + with pytest.raises(Exception, match="Invalid album browseId"): + yt.get_album("asdf") + def test_get_album_other_versions(self, yt): # Eminem - Curtain Call: The Hits (Explicit Variant) album = yt.get_album("MPREb_LQCAymzbaKJ") diff --git a/tests/mixins/test_watch.py b/tests/mixins/test_watch.py index 0262274..ff77dc7 100644 --- a/tests/mixins/test_watch.py +++ b/tests/mixins/test_watch.py @@ -1,3 +1,6 @@ +import pytest + + class TestWatch: def test_get_watch_playlist(self, config, yt, yt_brand, yt_oauth): playlist = yt_oauth.get_watch_playlist( @@ -14,3 +17,7 @@ def test_get_watch_playlist(self, config, yt, yt_brand, yt_oauth): assert len(playlist["tracks"]) == config.getint("albums", "album_track_length") playlist = yt_brand.get_watch_playlist(playlistId=config["playlists"]["own"], shuffle=True) assert len(playlist["tracks"]) == config.getint("playlists", "own_length") + + def test_get_watch_playlist_errors(self, config, yt): + with pytest.raises(Exception, match="No content returned by the server"): + yt.get_watch_playlist(playlistId="PL_NOT_EXIST") diff --git a/ytmusicapi/parsers/_utils.py b/ytmusicapi/parsers/_utils.py index 48e8ccb..de6a90d 100644 --- a/ytmusicapi/parsers/_utils.py +++ b/ytmusicapi/parsers/_utils.py @@ -51,19 +51,12 @@ def get_fixed_column_item(item, index): return item["fixedColumns"][index]["musicResponsiveListItemFixedColumnRenderer"] -def get_browse_id(item, index): - if "navigationEndpoint" not in item["text"]["runs"][index]: - return None - else: - return nav(item["text"]["runs"][index], NAVIGATION_BROWSE_ID) - - def get_dot_separator_index(runs): - index = len(runs) try: index = runs.index({"text": " • "}) except ValueError: - len(runs) + index = len(runs) + return index diff --git a/ytmusicapi/parsers/explore.py b/ytmusicapi/parsers/explore.py index 4b366a3..7284bec 100644 --- a/ytmusicapi/parsers/explore.py +++ b/ytmusicapi/parsers/explore.py @@ -24,22 +24,6 @@ def parse_chart_artist(data): return parsed -def parse_chart_trending(data): - flex_0 = get_flex_column_item(data, 0) - artists = parse_song_artists(data, 1) - index = get_dot_separator_index(artists) - # last item is views for some reason - views = None if index == len(artists) else artists.pop()["name"].split(" ")[0] - return { - "title": nav(flex_0, TEXT_RUN_TEXT), - "videoId": nav(flex_0, TEXT_RUN + NAVIGATION_VIDEO_ID, True), - "playlistId": nav(flex_0, TEXT_RUN + NAVIGATION_PLAYLIST_ID, True), - "artists": artists, - "thumbnails": nav(data, THUMBNAILS), - "views": views, - } - - def parse_ranking(data): return { "rank": nav(data, ["customIndexColumn", "musicCustomIndexColumnRenderer", *TEXT_RUN_TEXT]), diff --git a/ytmusicapi/parsers/songs.py b/ytmusicapi/parsers/songs.py index 8f93ada..1347ee4 100644 --- a/ytmusicapi/parsers/songs.py +++ b/ytmusicapi/parsers/songs.py @@ -53,7 +53,8 @@ def parse_song_runs(runs): def parse_song_album(data, index): flex_item = get_flex_column_item(data, index) - return None if not flex_item else {"name": get_item_text(data, index), "id": get_browse_id(flex_item, 0)} + browse_id = nav(flex_item, TEXT_RUN + NAVIGATION_BROWSE_ID, True) + return None if not flex_item else {"name": get_item_text(data, index), "id": browse_id} def parse_song_library_status(item) -> bool: