Skip to content

Commit

Permalink
Isolate parsing formats into a separate method and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Sep 19, 2024
1 parent e1a5088 commit 2f98344
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
21 changes: 21 additions & 0 deletions beetsplug/discogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
python3-discogs-client library.
"""

from __future__ import annotations

import http.client
import json
import os
Expand All @@ -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
Expand All @@ -52,6 +55,12 @@
)


class ReleaseFormat(TypedDict):
name: str
qty: int
descriptions: list[str] | None


class DiscogsPlugin(BeetsPlugin):
def __init__(self):
super().__init__()
Expand Down Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions test/plugins/test_discogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 2f98344

Please sign in to comment.