From 2f983447e6197c9fe56ddc38fab641cfc8846638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Thu, 19 Sep 2024 13:32:18 +0100 Subject: [PATCH] Isolate parsing formats into a separate method and test it --- beetsplug/discogs.py | 21 +++++++++++++++++++++ test/plugins/test_discogs.py | 21 +++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/beetsplug/discogs.py b/beetsplug/discogs.py index c896febf90..92c7f387e5 100644 --- a/beetsplug/discogs.py +++ b/beetsplug/discogs.py @@ -16,6 +16,8 @@ python3-discogs-client library. """ +from __future__ import annotations + import http.client import json import os @@ -30,6 +32,7 @@ from discogs_client import __version__ as dc_string from discogs_client.exceptions import DiscogsAPIError from requests.exceptions import ConnectionError +from typing_extensions import TypedDict import beets import beets.ui @@ -52,6 +55,12 @@ ) +class ReleaseFormat(TypedDict): + name: str + qty: int + descriptions: list[str] | None + + class DiscogsPlugin(BeetsPlugin): def __init__(self): super().__init__() @@ -363,6 +372,18 @@ def get_master_year(self, master_id): ) return None + @staticmethod + def get_media_and_albumtype( + formats: list[ReleaseFormat] | None, + ) -> tuple[str | None, str | None]: + media = albumtype = None + if formats and (first_format := formats[0]): + if descriptions := first_format["descriptions"]: + albumtype = ", ".join(descriptions) + media = first_format["name"] + + return media, albumtype + def get_album_info(self, result): """Returns an AlbumInfo object for a discogs Release object.""" # Explicitly reload the `Release` fields, as they might not be yet diff --git a/test/plugins/test_discogs.py b/test/plugins/test_discogs.py index 634b3cdb96..a9359e406a 100644 --- a/test/plugins/test_discogs.py +++ b/test/plugins/test_discogs.py @@ -12,9 +12,9 @@ # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -"""Tests for discogs plugin. -""" +"""Tests for discogs plugin.""" +import pytest from beets import config from beets.test._common import Bag @@ -423,3 +423,20 @@ def test_append_style_to_genre_no_style(self): d = DiscogsPlugin().get_album_info(release) assert d.genre == "GENRE1, GENRE2" assert d.style is None + + +@pytest.mark.parametrize( + "formats, expected_media, expected_albumtype", + [ + (None, None, None), + ( + [{"descriptions": ['7"', "Single", "45 RPM"], "name": "Vinyl", "qty": 1}], + "Vinyl", + '7", Single, 45 RPM', + ), + ], +) +def test_get_media_and_albumtype(formats, expected_media, expected_albumtype): + result = DiscogsPlugin.get_media_and_albumtype(formats) + + assert result == (expected_media, expected_albumtype)