| 1 |
"""Tests for discogs plugin.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
from typing import TYPE_CHECKING, Any |
| 7 |
from unittest.mock import MagicMock |
| 8 |
|
| 9 |
import pytest |
| 10 |
from discogs_client import Client, Release |
| 11 |
|
| 12 |
from beets import config |
| 13 |
from beets.library import Item |
| 14 |
from beets.metadata_plugins import SearchParams |
| 15 |
from beets.test.helper import TestHelper |
| 16 |
from beetsplug.discogs import ArtistState, DiscogsPlugin |
| 17 |
|
| 18 |
from .factories import discogs as factories |
| 19 |
|
| 20 |
if TYPE_CHECKING: |
| 21 |
from beetsplug.discogs import types |
| 22 |
|
| 23 |
|
| 24 |
_p = pytest.param |
| 25 |
|
| 26 |
|
| 27 |
def _artist(name: str, **kwargs): |
| 28 |
return { |
| 29 |
"id": 1, |
| 30 |
"name": name, |
| 31 |
"join": "", |
| 32 |
"role": "", |
| 33 |
"anv": "", |
| 34 |
"tracks": "", |
| 35 |
"resource_url": "", |
| 36 |
} | kwargs |
| 37 |
|
| 38 |
|
| 39 |
def audio_track( |
| 40 |
title: str, position: str = "", duration: str = "", **kwargs |
| 41 |
) -> types.AudioTrack: |
| 42 |
kwargs.update(title=title, position=position, duration=duration) |
| 43 |
return factories.AudioTrackFactory.build(**kwargs) |
| 44 |
|
| 45 |
|
| 46 |
def index_track( |
| 47 |
title: str, sub_tracks: list[types.AudioTrack], **kwargs |
| 48 |
) -> types.IndexTrack: |
| 49 |
kwargs.update(title=title, sub_tracks=sub_tracks) |
| 50 |
return factories.IndexTrackFactory.build(**kwargs) |
| 51 |
|
| 52 |
|
| 53 |
def heading_track(title: str, **kwargs) -> types.HeadingTrack: |
| 54 |
kwargs.update(title=title) |
| 55 |
return factories.HeadingTrackFactory.build(**kwargs) |
| 56 |
|
| 57 |
|
| 58 |
def get_release(data: dict[str, Any]) -> Release: |
| 59 |
return Release(Client("doesn't matter"), data) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture(autouse=True) |
| 63 |
def _patch_discogs_setup(monkeypatch): |
| 64 |
"""Autouse fixture to patch DiscogsPlugin.setup for each test.""" |
| 65 |
monkeypatch.setattr( |
| 66 |
"beetsplug.discogs.DiscogsPlugin.setup", lambda *_: None |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
class DiscogsTestMixin: |
| 71 |
@pytest.fixture |
| 72 |
def plugin_config(self): |
| 73 |
return {} |
| 74 |
|
| 75 |
@pytest.fixture |
| 76 |
def plugin(self, plugin_config): |
| 77 |
plugin = DiscogsPlugin() |
| 78 |
plugin.config.set(plugin_config) |
| 79 |
return plugin |
| 80 |
|
| 81 |
def _make_release(self, tracks=None): |
| 82 |
"""Return discogs_client.Release. |
| 83 |
|
| 84 |
The returned object is incomplete, including just the fields required |
| 85 |
for tests in this module. |
| 86 |
""" |
| 87 |
data = { |
| 88 |
"id": 11111111, |
| 89 |
"uri": "https://www.discogs.com/release/111111111", |
| 90 |
"title": "ALBUM TITLE", |
| 91 |
"year": 3001, |
| 92 |
"artists": [_artist("ARTIST NAME", join=",")], |
| 93 |
"formats": [ |
| 94 |
{ |
| 95 |
"descriptions": ["FORMAT DESC 1", "FORMAT DESC 2"], |
| 96 |
"name": "FORMAT", |
| 97 |
"qty": 1, |
| 98 |
} |
| 99 |
], |
| 100 |
# genres and styles are reversed in Discogs |
| 101 |
"genres": ["STYLE1", "STYLE2"], |
| 102 |
"styles": ["GENRE1", "GENRE2"], |
| 103 |
"labels": [{"name": "LABEL NAME", "catno": "CATALOG NUMBER"}], |
| 104 |
"tracklist": tracks or [], |
| 105 |
} |
| 106 |
|
| 107 |
return get_release(data) |
| 108 |
|
| 109 |
def _make_release_from_positions(self, positions): |
| 110 |
"""Return discogs_client.Release with tracks at the given positions.""" |
| 111 |
tracks = [ |
| 112 |
audio_track(f"TITLE{i}", position) |
| 113 |
for (i, position) in enumerate(positions, start=1) |
| 114 |
] |
| 115 |
return self._make_release(tracks) |
| 116 |
|
| 117 |
|
| 118 |
class TestDGAlbumInfo(DiscogsTestMixin, TestHelper): |
| 119 |
def test_parse_media_for_tracks(self): |
| 120 |
tracks = [ |
| 121 |
audio_track("TITLE ONE", "1", "01:01"), |
| 122 |
audio_track("TITLE TWO", "2", "02:02"), |
| 123 |
] |
| 124 |
release = self._make_release(tracks=tracks) |
| 125 |
|
| 126 |
d = DiscogsPlugin().get_album_info(release) |
| 127 |
t = d.tracks |
| 128 |
assert d.media == "FORMAT" |
| 129 |
assert t[0].media == d.media |
| 130 |
assert t[1].media == d.media |
| 131 |
|
| 132 |
def test_parse_medium_numbers_single_medium(self): |
| 133 |
release = self._make_release_from_positions(["1", "2"]) |
| 134 |
d = DiscogsPlugin().get_album_info(release) |
| 135 |
t = d.tracks |
| 136 |
|
| 137 |
assert d.mediums == 1 |
| 138 |
assert t[0].medium == 1 |
| 139 |
assert t[0].medium_total == 2 |
| 140 |
assert t[1].medium == 1 |
| 141 |
assert t[0].medium_total == 2 |
| 142 |
|
| 143 |
def test_parse_medium_numbers_two_mediums(self): |
| 144 |
release = self._make_release_from_positions(["1-1", "2-1"]) |
| 145 |
d = DiscogsPlugin().get_album_info(release) |
| 146 |
t = d.tracks |
| 147 |
|
| 148 |
assert d.mediums == 2 |
| 149 |
assert t[0].medium == 1 |
| 150 |
assert t[0].medium_total == 1 |
| 151 |
assert t[1].medium == 2 |
| 152 |
assert t[1].medium_total == 1 |
| 153 |
|
| 154 |
def test_parse_medium_numbers_two_mediums_two_sided(self): |
| 155 |
release = self._make_release_from_positions(["A1", "B1", "C1"]) |
| 156 |
d = DiscogsPlugin().get_album_info(release) |
| 157 |
t = d.tracks |
| 158 |
|
| 159 |
assert d.mediums == 2 |
| 160 |
assert t[0].medium == 1 |
| 161 |
assert t[0].medium_total == 2 |
| 162 |
assert t[0].medium_index == 1 |
| 163 |
assert t[1].medium == 1 |
| 164 |
assert t[1].medium_total == 2 |
| 165 |
assert t[1].medium_index == 2 |
| 166 |
assert t[2].medium == 2 |
| 167 |
assert t[2].medium_total == 1 |
| 168 |
assert t[2].medium_index == 1 |
| 169 |
|
| 170 |
def test_parse_track_indices(self): |
| 171 |
release = self._make_release_from_positions(["1", "2"]) |
| 172 |
d = DiscogsPlugin().get_album_info(release) |
| 173 |
t = d.tracks |
| 174 |
|
| 175 |
assert t[0].medium_index == 1 |
| 176 |
assert t[0].index == 1 |
| 177 |
assert t[0].medium_total == 2 |
| 178 |
assert t[1].medium_index == 2 |
| 179 |
assert t[1].index == 2 |
| 180 |
assert t[1].medium_total == 2 |
| 181 |
|
| 182 |
def test_parse_track_indices_several_media(self): |
| 183 |
release = self._make_release_from_positions( |
| 184 |
["1-1", "1-2", "2-1", "3-1"] |
| 185 |
) |
| 186 |
d = DiscogsPlugin().get_album_info(release) |
| 187 |
t = d.tracks |
| 188 |
|
| 189 |
assert d.mediums == 3 |
| 190 |
assert t[0].medium_index == 1 |
| 191 |
assert t[0].index == 1 |
| 192 |
assert t[0].medium_total == 2 |
| 193 |
assert t[1].medium_index == 2 |
| 194 |
assert t[1].index == 2 |
| 195 |
assert t[1].medium_total == 2 |
| 196 |
assert t[2].medium_index == 1 |
| 197 |
assert t[2].index == 3 |
| 198 |
assert t[2].medium_total == 1 |
| 199 |
assert t[3].medium_index == 1 |
| 200 |
assert t[3].index == 4 |
| 201 |
assert t[3].medium_total == 1 |
| 202 |
|
| 203 |
def test_parse_minimal_release(self): |
| 204 |
"""Test parsing of a release with the minimal amount of information.""" |
| 205 |
data = { |
| 206 |
"id": 123, |
| 207 |
"uri": "https://www.discogs.com/release/123456-something", |
| 208 |
"tracklist": [audio_track("A", "1", "01:01")], |
| 209 |
"artists": [_artist("ARTIST NAME", id=321)], |
| 210 |
"title": "TITLE", |
| 211 |
} |
| 212 |
release = get_release(data) |
| 213 |
d = DiscogsPlugin().get_album_info(release) |
| 214 |
assert d.artist == "ARTIST NAME" |
| 215 |
assert d.album == "TITLE" |
| 216 |
assert len(d.tracks) == 1 |
| 217 |
|
| 218 |
def test_default_genre_style_settings(self): |
| 219 |
"""Test genre default settings, genres to genre, styles to style""" |
| 220 |
release = self._make_release_from_positions(["1", "2"]) |
| 221 |
|
| 222 |
d = DiscogsPlugin().get_album_info(release) |
| 223 |
assert d.style == "STYLE1, STYLE2" |
| 224 |
assert d.genres == ["GENRE1", "GENRE2"] |
| 225 |
|
| 226 |
def test_append_style_to_genre(self): |
| 227 |
"""Test appending style to genre if config enabled""" |
| 228 |
config["discogs"]["append_style_genre"] = True |
| 229 |
release = self._make_release_from_positions(["1", "2"]) |
| 230 |
|
| 231 |
d = DiscogsPlugin().get_album_info(release) |
| 232 |
assert d.style == "STYLE1, STYLE2" |
| 233 |
assert d.genres == ["GENRE1", "GENRE2", "STYLE1", "STYLE2"] |
| 234 |
|
| 235 |
def test_append_style_to_genre_no_styles(self): |
| 236 |
"""Test nothing appended to genre if style is empty""" |
| 237 |
config["discogs"]["append_style_genre"] = True |
| 238 |
release = self._make_release_from_positions(["1", "2"]) |
| 239 |
release.data["genres"] = [] |
| 240 |
|
| 241 |
d = DiscogsPlugin().get_album_info(release) |
| 242 |
assert d.style is None |
| 243 |
assert d.genres == ["GENRE1", "GENRE2"] |
| 244 |
|
| 245 |
@pytest.mark.parametrize( |
| 246 |
"released, expected", |
| 247 |
[ |
| 248 |
_p("2000-08-13", (2000, 8, 13), id="full-date"), |
| 249 |
_p("2000-08", (2000, 8, None), id="year-and-month"), |
| 250 |
_p("2000", (2000, None, None), id="year-only"), |
| 251 |
_p("2000-00-00", (2000, None, None), id="zeroed-month-and-day"), |
| 252 |
_p("2000-08-00", (2000, 8, None), id="zeroed-day"), |
| 253 |
_p("2000-8-1", (2000, 8, 1), id="unpadded"), |
| 254 |
_p(" 2000-08-13 ", (2000, 8, 13), id="surrounding-whitespace"), |
| 255 |
# Fall back to the release's own `year` field. |
| 256 |
_p("", (3001, None, None), id="empty"), |
| 257 |
_p(None, (3001, None, None), id="missing"), |
| 258 |
_p("0000-00-00", (3001, None, None), id="zeroed-date"), |
| 259 |
_p("13 Aug 2000", (3001, None, None), id="unparseable"), |
| 260 |
], |
| 261 |
) |
| 262 |
def test_parse_release_date(self, released, expected): |
| 263 |
release = self._make_release_from_positions(["1"]) |
| 264 |
release.data["released"] = released |
| 265 |
|
| 266 |
d = DiscogsPlugin().get_album_info(release) |
| 267 |
|
| 268 |
assert (d.year, d.month, d.day) == expected |
| 269 |
|
| 270 |
def test_original_date_without_master(self): |
| 271 |
"""A release without a master release is its own original.""" |
| 272 |
release = self._make_release_from_positions(["1"]) |
| 273 |
release.data["released"] = "2000-08-13" |
| 274 |
|
| 275 |
d = DiscogsPlugin().get_album_info(release) |
| 276 |
|
| 277 |
assert (d.original_year, d.original_month, d.original_day) == ( |
| 278 |
2000, |
| 279 |
8, |
| 280 |
13, |
| 281 |
) |
| 282 |
|
| 283 |
def test_original_date_with_master(self, monkeypatch): |
| 284 |
"""Only the master release's year is known, so it alone is used.""" |
| 285 |
monkeypatch.setattr(DiscogsPlugin, "get_master_year", lambda *_: 1990) |
| 286 |
release = self._make_release_from_positions(["1"]) |
| 287 |
release.data["released"] = "2000-08-13" |
| 288 |
release.data["master_id"] = 22222222 |
| 289 |
|
| 290 |
d = DiscogsPlugin().get_album_info(release) |
| 291 |
|
| 292 |
assert (d.year, d.month, d.day) == (2000, 8, 13) |
| 293 |
assert (d.original_year, d.original_month, d.original_day) == ( |
| 294 |
1990, |
| 295 |
None, |
| 296 |
None, |
| 297 |
) |
| 298 |
|
| 299 |
|
| 300 |
class TestStripDisambiguation(DiscogsTestMixin): |
| 301 |
@pytest.fixture |
| 302 |
def album_info(self, plugin): |
| 303 |
data = { |
| 304 |
"id": 123, |
| 305 |
"uri": "https://www.discogs.com/release/123456-something", |
| 306 |
"tracklist": [ |
| 307 |
{ |
| 308 |
"title": "track", |
| 309 |
"position": "A", |
| 310 |
"type_": "track", |
| 311 |
"duration": "5:44", |
| 312 |
"artists": [_artist("TEST ARTIST (5)", id=11146)], |
| 313 |
} |
| 314 |
], |
| 315 |
"artists": [ |
| 316 |
_artist("ARTIST NAME (2)", id=321, join="&"), |
| 317 |
_artist("OTHER ARTIST (5)", id=322), |
| 318 |
], |
| 319 |
"title": "title", |
| 320 |
"labels": [{"name": "LABEL NAME (5)", "catno": "catalog number"}], |
| 321 |
} |
| 322 |
return plugin.get_album_info(get_release(data)) |
| 323 |
|
| 324 |
@pytest.mark.parametrize("plugin_config", [{"strip_disambiguation": True}]) |
| 325 |
def test_strip_disambiguation(self, album_info): |
| 326 |
"""Test removing disambiguation from all disambiguated fields.""" |
| 327 |
assert album_info.artist == "ARTIST NAME & OTHER ARTIST" |
| 328 |
assert album_info.artists == ["ARTIST NAME", "OTHER ARTIST"] |
| 329 |
assert album_info.artists_ids == ["321", "322"] |
| 330 |
assert album_info.tracks[0].artist == "TEST ARTIST" |
| 331 |
assert album_info.tracks[0].artists == ["TEST ARTIST"] |
| 332 |
assert album_info.tracks[0].artist_id == "11146" |
| 333 |
assert album_info.tracks[0].artists_ids == ["11146"] |
| 334 |
assert album_info.label == "LABEL NAME" |
| 335 |
|
| 336 |
@pytest.mark.parametrize("plugin_config", [{"strip_disambiguation": False}]) |
| 337 |
def test_dont_strip_disambiguation(self, album_info): |
| 338 |
"""Test disabling disambiguation removal from all disambiguated fields.""" |
| 339 |
assert album_info.artist == "ARTIST NAME (2) & OTHER ARTIST (5)" |
| 340 |
assert album_info.artists == ["ARTIST NAME (2)", "OTHER ARTIST (5)"] |
| 341 |
assert album_info.tracks[0].artist == "TEST ARTIST (5)" |
| 342 |
assert album_info.tracks[0].artists == ["TEST ARTIST (5)"] |
| 343 |
assert album_info.label == "LABEL NAME (5)" |
| 344 |
|
| 345 |
|
| 346 |
class TestTracklist(DiscogsTestMixin): |
| 347 |
@pytest.mark.parametrize( |
| 348 |
"positions,expected_mediums,expected_tracks", |
| 349 |
[ |
| 350 |
_p(["1", "2", "3"], 1, 3, id="without-sides"), |
| 351 |
_p(["A1", "A2", "B1", "B2"], 1, 4, id="with-sides"), |
| 352 |
_p(["A1", "A2", "B1", "C1"], 2, 4, id="multiple-lp"), |
| 353 |
_p(["1-1", "1-2", "2-1", "3-1"], 3, 4, id="multiple-cd"), |
| 354 |
_p(["I", "II", "III", "IV"], 1, 4, id="non-standard"), |
| 355 |
_p(["1", "2.1", "2.2", "3"], 1, 3, id="subtracks-dot"), |
| 356 |
_p(["A1", "A2.1", "A2.2", "A3"], 1, 3, id="subtracks-dot-with-sides"), |
| 357 |
_p(["A1", "A2a", "A2b", "A3"], 1, 3, id="subtracks-letter"), |
| 358 |
_p(["A1", "A2.a", "A2.b", "A3"], 1, 3, id="subtracks-letter-with-dot"), |
| 359 |
_p(["1.1", "1.2", "2.1", "2.2"], 1, 2, id="multiple-subtrack-groups"), |
| 360 |
_p(["1", "2", "Video 1"], 2, 3, id="subtracks-extra-material"), |
| 361 |
], |
| 362 |
) # fmt: skip |
| 363 |
def test_parse_tracklist_positions( |
| 364 |
self, plugin, positions, expected_mediums, expected_tracks |
| 365 |
): |
| 366 |
release = self._make_release_from_positions(positions) |
| 367 |
d = plugin.get_album_info(release) |
| 368 |
|
| 369 |
assert d.mediums == expected_mediums |
| 370 |
assert len(d.tracks) == expected_tracks |
| 371 |
|
| 372 |
@pytest.mark.parametrize( |
| 373 |
"plugin_config,tracks,expected_mediums,expected_tracks", |
| 374 |
[ |
| 375 |
_p( |
| 376 |
{"index_tracks": False}, |
| 377 |
[ |
| 378 |
heading_track("MEDIUM TITLE"), |
| 379 |
audio_track("TITLE ONE", "1.1"), |
| 380 |
audio_track("TITLE TWO", "1.2"), |
| 381 |
], |
| 382 |
1, |
| 383 |
[("TITLE ONE / TITLE TWO", "MEDIUM TITLE")], |
| 384 |
id="flat-logical-subtracks", |
| 385 |
), |
| 386 |
_p( |
| 387 |
{"index_tracks": False}, |
| 388 |
[ |
| 389 |
audio_track("TITLE ONE", "1"), |
| 390 |
index_track( |
| 391 |
"TRACK GROUP TITLE", |
| 392 |
[ |
| 393 |
audio_track("SUBTITLE ONE", "2.1", "01:01"), |
| 394 |
audio_track("SUBTITLE TWO", "2.2", "02:02"), |
| 395 |
], |
| 396 |
), |
| 397 |
audio_track("TITLE THREE", "3"), |
| 398 |
], |
| 399 |
1, |
| 400 |
[ |
| 401 |
("TITLE ONE", None), |
| 402 |
("TRACK GROUP TITLE", None), |
| 403 |
("TITLE THREE", None), |
| 404 |
], |
| 405 |
id="nested-logical-subtracks", |
| 406 |
), |
| 407 |
_p( |
| 408 |
{"index_tracks": False}, |
| 409 |
[ |
| 410 |
audio_track("TITLE ONE", "1"), |
| 411 |
index_track( |
| 412 |
"TRACK GROUP TITLE", |
| 413 |
[ |
| 414 |
audio_track("SUBTITLE ONE", "2", "01:01"), |
| 415 |
audio_track("SUBTITLE TWO", "3", "02:02"), |
| 416 |
], |
| 417 |
), |
| 418 |
audio_track("TITLE FOUR", "4"), |
| 419 |
], |
| 420 |
1, |
| 421 |
[ |
| 422 |
("TITLE ONE", None), |
| 423 |
("SUBTITLE ONE", None), |
| 424 |
("SUBTITLE TWO", None), |
| 425 |
("TITLE FOUR", None), |
| 426 |
], |
| 427 |
id="nested-physical-subtracks", |
| 428 |
), |
| 429 |
_p( |
| 430 |
{"index_tracks": True}, |
| 431 |
[ |
| 432 |
audio_track("TITLE ONE", "1"), |
| 433 |
index_track( |
| 434 |
"TRACK GROUP TITLE", |
| 435 |
[ |
| 436 |
audio_track("SUBTITLE ONE", "2", "01:01"), |
| 437 |
audio_track("SUBTITLE TWO", "3", "02:02"), |
| 438 |
], |
| 439 |
), |
| 440 |
audio_track("TITLE FOUR", "4"), |
| 441 |
], |
| 442 |
1, |
| 443 |
[ |
| 444 |
("TITLE ONE", None), |
| 445 |
("TRACK GROUP TITLE: SUBTITLE ONE", None), |
| 446 |
("TRACK GROUP TITLE: SUBTITLE TWO", None), |
| 447 |
("TITLE FOUR", None), |
| 448 |
], |
| 449 |
id="nested-physical-subtracks-with-index-tracks", |
| 450 |
), |
| 451 |
_p( |
| 452 |
{"index_tracks": False}, |
| 453 |
[ |
| 454 |
heading_track("MEDIUM TITLE CD1"), |
| 455 |
audio_track("TITLE ONE", "1-1"), |
| 456 |
audio_track("TITLE TWO", "1-2"), |
| 457 |
heading_track("MEDIUM TITLE CD2"), |
| 458 |
audio_track("TITLE THREE", "2-1"), |
| 459 |
], |
| 460 |
2, |
| 461 |
[ |
| 462 |
("TITLE ONE", "MEDIUM TITLE CD1"), |
| 463 |
("TITLE TWO", "MEDIUM TITLE CD1"), |
| 464 |
("TITLE THREE", "MEDIUM TITLE CD2"), |
| 465 |
], |
| 466 |
id="medium-titles", |
| 467 |
), |
| 468 |
], |
| 469 |
) # fmt: skip |
| 470 |
def test_parse_tracklist_index_tracks( |
| 471 |
self, plugin, tracks, expected_mediums, expected_tracks |
| 472 |
): |
| 473 |
release = self._make_release(tracks) |
| 474 |
album = plugin.get_album_info(release) |
| 475 |
|
| 476 |
assert album.mediums == expected_mediums |
| 477 |
assert [(t.title, t.disctitle) for t in album.tracks] == expected_tracks |
| 478 |
|
| 479 |
def test_parse_tracklist_inherited_artists(self, plugin): |
| 480 |
"""Verify grouped tracks combine explicit and inherited artist credits. |
| 481 |
|
| 482 |
This covers releases where a track group provides the default artist for |
| 483 |
sub-tracks that do not declare one themselves. |
| 484 |
|
| 485 |
Note: this is based on the following release: |
| 486 |
https://www.discogs.com/release/3647530 |
| 487 |
""" |
| 488 |
track_artist = "TRACK ARTIST" |
| 489 |
group_artist = "GROUP ARTIST" |
| 490 |
tracks = [ |
| 491 |
index_track( |
| 492 |
"TRACK GROUP TITLE", |
| 493 |
[ |
| 494 |
audio_track( |
| 495 |
"TRACK ONE", "2", artists=[_artist(track_artist)] |
| 496 |
), |
| 497 |
audio_track("SUBTITLE TWO", "3"), |
| 498 |
], |
| 499 |
artists=[_artist(group_artist)], |
| 500 |
) |
| 501 |
] |
| 502 |
release = self._make_release(tracks) |
| 503 |
album = plugin.get_album_info(release) |
| 504 |
|
| 505 |
assert album.mediums == 1 |
| 506 |
assert {t.artist for t in album.tracks} == {track_artist, group_artist} |
| 507 |
|
| 508 |
def test_coalesce_logical_index_as_audio_track(self, plugin): |
| 509 |
index = index_track( |
| 510 |
"TRACK GROUP TITLE", |
| 511 |
[ |
| 512 |
audio_track("SUBTITLE ONE", "2.1"), |
| 513 |
audio_track("SUBTITLE TWO", "2.2"), |
| 514 |
], |
| 515 |
duration="03:03", |
| 516 |
) |
| 517 |
|
| 518 |
assert plugin._coalesce_tracks([index]) == [ |
| 519 |
audio_track("TRACK GROUP TITLE", "2", "03:03") |
| 520 |
] |
| 521 |
|
| 522 |
|
| 523 |
class TestDGSearchQuery(TestHelper): |
| 524 |
def test_default_search_filters_without_extra_tags(self): |
| 525 |
"""Discogs search uses only the type filter when no extra_tags are set.""" |
| 526 |
plugin = DiscogsPlugin() |
| 527 |
items = [Item()] |
| 528 |
|
| 529 |
query, filters = plugin.get_search_query_with_filters( |
| 530 |
"album", items, "Artist", "Album", False |
| 531 |
) |
| 532 |
|
| 533 |
assert "Album" in query |
| 534 |
assert filters == {"type": "release"} |
| 535 |
|
| 536 |
def test_extra_tags_populate_discogs_filters(self): |
| 537 |
"""Configured extra_tags should populate Discogs search filters.""" |
| 538 |
plugin = DiscogsPlugin() |
| 539 |
plugin.config["extra_tags"] = ["label", "catalognum"] |
| 540 |
|
| 541 |
items = [ |
| 542 |
Item(catalognum="ABC 123", label="abc"), |
| 543 |
Item(catalognum="ABC 123", label="abc"), |
| 544 |
Item(catalognum="ABC 123", label="def"), |
| 545 |
] |
| 546 |
|
| 547 |
_query, filters = plugin.get_search_query_with_filters( |
| 548 |
"album", items, "Artist", "Album", False |
| 549 |
) |
| 550 |
|
| 551 |
assert filters["type"] == "release" |
| 552 |
assert filters["label"] == "abc" |
| 553 |
# Catalog number should have whitespace removed. |
| 554 |
assert filters["catno"] == "ABC123" |
| 555 |
config["discogs"]["extra_tags"] = [] |
| 556 |
|
| 557 |
|
| 558 |
class TestDGSearchResponse(DiscogsTestMixin): |
| 559 |
@staticmethod |
| 560 |
def _decode_error(): |
| 561 |
return json.JSONDecodeError("Expecting value", "", 0) |
| 562 |
|
| 563 |
@pytest.fixture |
| 564 |
def params(self): |
| 565 |
return SearchParams("album", "Album", {"type": "release"}, 5) |
| 566 |
|
| 567 |
@pytest.fixture |
| 568 |
def client(self, plugin): |
| 569 |
plugin.discogs_client = MagicMock() |
| 570 |
return plugin.discogs_client |
| 571 |
|
| 572 |
def test_retries_invalid_json_response(self, plugin, client, params): |
| 573 |
result = MagicMock(data={"id": 123}) |
| 574 |
results = client.search.return_value |
| 575 |
results.page.side_effect = [self._decode_error(), [result]] |
| 576 |
|
| 577 |
assert plugin.get_search_response(params) == [{"id": 123}] |
| 578 |
assert client.search.call_count == 2 |
| 579 |
assert results.page.call_count == 2 |
| 580 |
assert results.per_page == params.limit |
| 581 |
|
| 582 |
def test_raises_after_invalid_json_retry(self, plugin, client, params): |
| 583 |
results = client.search.return_value |
| 584 |
results.page.side_effect = [self._decode_error(), self._decode_error()] |
| 585 |
|
| 586 |
with pytest.raises(json.JSONDecodeError): |
| 587 |
plugin.get_search_response(params) |
| 588 |
|
| 589 |
assert client.search.call_count == 2 |
| 590 |
assert results.page.call_count == 2 |
| 591 |
|
| 592 |
|
| 593 |
class TestAnv: |
| 594 |
@pytest.fixture |
| 595 |
def album_info(self, monkeypatch, anv_config): |
| 596 |
monkeypatch.setattr( |
| 597 |
"beetsplug.discogs.DiscogsPlugin.setup", lambda _: None |
| 598 |
) |
| 599 |
data = { |
| 600 |
"id": 123, |
| 601 |
"uri": "https://www.discogs.com/release/123456-something", |
| 602 |
"tracklist": [ |
| 603 |
{ |
| 604 |
"title": "track", |
| 605 |
"position": "A", |
| 606 |
"type_": "track", |
| 607 |
"duration": "5:44", |
| 608 |
"artists": [ |
| 609 |
_artist("ARTIST", id=11146, anv="ART", join="Feat."), |
| 610 |
_artist("PERFORMER", id=787), |
| 611 |
], |
| 612 |
"extraartists": [ |
| 613 |
_artist("PERFORMER", id=787, role="Featuring") |
| 614 |
], |
| 615 |
} |
| 616 |
], |
| 617 |
"artists": [ |
| 618 |
_artist("DRUMMER", id=445, anv="DRUM", join=", "), |
| 619 |
_artist("ARTIST (4)", id=321, anv="ARTY", join="&"), |
| 620 |
_artist("SOLOIST", id=446, anv="SOLO"), |
| 621 |
], |
| 622 |
"title": "title", |
| 623 |
} |
| 624 |
release = get_release(data) |
| 625 |
plugin = DiscogsPlugin() |
| 626 |
plugin.config["anv"].set( |
| 627 |
{"artist": False, "album_artist": False, "artist_credit": False} |
| 628 |
| anv_config |
| 629 |
) |
| 630 |
return plugin.get_album_info(release) |
| 631 |
|
| 632 |
@staticmethod |
| 633 |
def _assert_fields(obj, expected): |
| 634 |
for field, value in expected.items(): |
| 635 |
assert getattr(obj, field) == value |
| 636 |
|
| 637 |
@pytest.mark.parametrize( |
| 638 |
"anv_config,expected_track_fields", |
| 639 |
[ |
| 640 |
( |
| 641 |
{"artist": False}, |
| 642 |
{ |
| 643 |
"artist": "ARTIST Feat. PERFORMER", |
| 644 |
"artists": ["ARTIST", "PERFORMER"], |
| 645 |
}, |
| 646 |
), |
| 647 |
( |
| 648 |
{"artist": True}, |
| 649 |
{ |
| 650 |
"artist": "ART Feat. PERFORMER", |
| 651 |
"artists": ["ART", "PERFORMER"], |
| 652 |
}, |
| 653 |
), |
| 654 |
], |
| 655 |
) |
| 656 |
def test_track_artist_fields(self, album_info, expected_track_fields): |
| 657 |
self._assert_fields(album_info.tracks[0], expected_track_fields) |
| 658 |
|
| 659 |
@pytest.mark.parametrize( |
| 660 |
"anv_config,expected_album_fields,expected_track_fields", |
| 661 |
[ |
| 662 |
( |
| 663 |
{"artist_credit": False}, |
| 664 |
{ |
| 665 |
"artist_credit": "DRUMMER, ARTIST & SOLOIST", |
| 666 |
"artists_credit": ["DRUMMER", "ARTIST", "SOLOIST"], |
| 667 |
}, |
| 668 |
{ |
| 669 |
"artist_credit": "ARTIST Feat. PERFORMER", |
| 670 |
"artists_credit": ["ARTIST", "PERFORMER"], |
| 671 |
}, |
| 672 |
), |
| 673 |
( |
| 674 |
{"artist_credit": True}, |
| 675 |
{ |
| 676 |
"artist_credit": "DRUM, ARTY & SOLO", |
| 677 |
"artists_credit": ["DRUM", "ARTY", "SOLO"], |
| 678 |
}, |
| 679 |
{ |
| 680 |
"artist_credit": "ART Feat. PERFORMER", |
| 681 |
"artists_credit": ["ART", "PERFORMER"], |
| 682 |
}, |
| 683 |
), |
| 684 |
], |
| 685 |
) |
| 686 |
def test_artist_credit_fields( |
| 687 |
self, album_info, expected_album_fields, expected_track_fields |
| 688 |
): |
| 689 |
self._assert_fields(album_info, expected_album_fields) |
| 690 |
self._assert_fields(album_info.tracks[0], expected_track_fields) |
| 691 |
|
| 692 |
@pytest.mark.parametrize( |
| 693 |
"anv_config,expected_album_fields", |
| 694 |
[ |
| 695 |
( |
| 696 |
{"album_artist": False}, |
| 697 |
{ |
| 698 |
"artist": "DRUMMER, ARTIST & SOLOIST", |
| 699 |
"artists": ["DRUMMER", "ARTIST", "SOLOIST"], |
| 700 |
}, |
| 701 |
), |
| 702 |
( |
| 703 |
{"album_artist": True}, |
| 704 |
{ |
| 705 |
"artist": "DRUM, ARTY & SOLO", |
| 706 |
"artists": ["DRUM", "ARTY", "SOLO"], |
| 707 |
}, |
| 708 |
), |
| 709 |
], |
| 710 |
) |
| 711 |
def test_album_artist_fields(self, album_info, expected_album_fields): |
| 712 |
self._assert_fields(album_info, expected_album_fields) |
| 713 |
|
| 714 |
|
| 715 |
def test_anv_album_artist(): |
| 716 |
"""Test using artist name variations when the album artist |
| 717 |
is the same as the track artist, but only the track artist |
| 718 |
should use the artist name variation.""" |
| 719 |
data = { |
| 720 |
"id": 123, |
| 721 |
"uri": "https://www.discogs.com/release/123456-something", |
| 722 |
"tracklist": [ |
| 723 |
{ |
| 724 |
"title": "track", |
| 725 |
"position": "A", |
| 726 |
"type_": "track", |
| 727 |
"duration": "5:44", |
| 728 |
} |
| 729 |
], |
| 730 |
"artists": [_artist("ARTIST (4)", id=321, anv="VARIATION")], |
| 731 |
"title": "title", |
| 732 |
} |
| 733 |
release = get_release(data) |
| 734 |
config["discogs"]["anv"]["album_artist"] = False |
| 735 |
config["discogs"]["anv"]["artist"] = True |
| 736 |
config["discogs"]["anv"]["artist_credit"] = False |
| 737 |
r = DiscogsPlugin().get_album_info(release) |
| 738 |
assert r.artist == "ARTIST" |
| 739 |
assert r.artists == ["ARTIST"] |
| 740 |
assert r.artist_credit == "ARTIST" |
| 741 |
assert r.artist_id == "321" |
| 742 |
assert r.artists_credit == ["ARTIST"] |
| 743 |
assert r.tracks[0].artist == "VARIATION" |
| 744 |
assert r.tracks[0].artists == ["VARIATION"] |
| 745 |
assert r.tracks[0].artist_credit == "ARTIST" |
| 746 |
assert r.tracks[0].artists_credit == ["ARTIST"] |
| 747 |
|
| 748 |
|
| 749 |
def test_parse_featured_artists(): |
| 750 |
"""Tests the plugins ability to parse a featured artist. |
| 751 |
Ignores artists that are not listed as featured.""" |
| 752 |
track = { |
| 753 |
"type_": "track", |
| 754 |
"title": "track", |
| 755 |
"position": "1", |
| 756 |
"duration": "5:00", |
| 757 |
"artists": [ |
| 758 |
_artist("NEW ARTIST", id=11146, join="&"), |
| 759 |
_artist("VOCALIST", id=344, join="feat."), |
| 760 |
], |
| 761 |
"extraartists": [ |
| 762 |
_artist("SOLOIST", id=3, role="Featuring"), |
| 763 |
_artist("PERFORMER (1)", id=5, role="Other Role, Featuring"), |
| 764 |
_artist("RANDOM", id=8, role="Written-By"), |
| 765 |
_artist("MUSICIAN", id=10, role="Featuring [Uncredited]"), |
| 766 |
], |
| 767 |
} |
| 768 |
|
| 769 |
plugin = DiscogsPlugin() |
| 770 |
artistinfo = ArtistState.from_config(plugin.config, [_artist("ARTIST")]) |
| 771 |
t, _, _ = plugin.get_track_info(track, 1, 1, artistinfo) |
| 772 |
assert ( |
| 773 |
t.artist == "NEW ARTIST & VOCALIST feat. SOLOIST, PERFORMER, MUSICIAN" |
| 774 |
) |
| 775 |
assert t.artists == [ |
| 776 |
"NEW ARTIST", |
| 777 |
"VOCALIST", |
| 778 |
"SOLOIST", |
| 779 |
"PERFORMER", |
| 780 |
"MUSICIAN", |
| 781 |
] |
| 782 |
assert t.artists_ids == ["11146", "344", "3", "5", "10"] |
| 783 |
assert t.composers == ["RANDOM"] |
| 784 |
|
| 785 |
|
| 786 |
def test_parse_extraartist_roles(): |
| 787 |
plugin = DiscogsPlugin() |
| 788 |
artistinfo = ArtistState.from_config(plugin.config, [_artist("ARTIST")]) |
| 789 |
track = { |
| 790 |
"type_": "track", |
| 791 |
"title": "track", |
| 792 |
"position": "1", |
| 793 |
"duration": "5:00", |
| 794 |
"artists": [_artist("TRACK ARTIST", id=11)], |
| 795 |
"extraartists": [ |
| 796 |
_artist("LYRICIST", id=2, role="Lyrics By"), |
| 797 |
_artist("ARRANGER", id=3, role="Arranged By"), |
| 798 |
_artist("REMIXER", id=5, role="Remixed By"), |
| 799 |
_artist("COMPOSER", id=6, role="Written-By"), |
| 800 |
], |
| 801 |
} |
| 802 |
|
| 803 |
t, _, _ = plugin.get_track_info(track, 1, 1, artistinfo) |
| 804 |
|
| 805 |
assert t.artist == "TRACK ARTIST" |
| 806 |
assert t.artists == ["TRACK ARTIST"] |
| 807 |
assert t.lyricists == ["LYRICIST"] |
| 808 |
assert t.arrangers == ["ARRANGER"] |
| 809 |
assert t.remixers == ["REMIXER"] |
| 810 |
assert t.composers == ["COMPOSER"] |
| 811 |
|
| 812 |
|
| 813 |
@pytest.mark.parametrize( |
| 814 |
"formats, expected_media, expected_albumtype", |
| 815 |
[ |
| 816 |
(None, None, None), |
| 817 |
( |
| 818 |
[ |
| 819 |
{ |
| 820 |
"descriptions": ['7"', "Single", "45 RPM"], |
| 821 |
"name": "Vinyl", |
| 822 |
"qty": 1, |
| 823 |
} |
| 824 |
], |
| 825 |
"Vinyl", |
| 826 |
'7", Single, 45 RPM', |
| 827 |
), |
| 828 |
], |
| 829 |
) |
| 830 |
def test_get_media_and_albumtype(formats, expected_media, expected_albumtype): |
| 831 |
result = DiscogsPlugin.get_media_and_albumtype(formats) |
| 832 |
|
| 833 |
assert result == (expected_media, expected_albumtype) |
| 834 |
|
| 835 |
|
| 836 |
def test_va_buildartistinfo(): |
| 837 |
config["va_name"] = "VARIOUS ARTISTS" |
| 838 |
expected_info = { |
| 839 |
"artist": "VARIOUS ARTISTS", |
| 840 |
"artist_id": "1", |
| 841 |
"artists": ["VARIOUS ARTISTS"], |
| 842 |
"artists_ids": ["1"], |
| 843 |
"artist_credit": "VARIOUS ARTISTS", |
| 844 |
"artists_credit": ["VARIOUS ARTISTS"], |
| 845 |
"arrangers": None, |
| 846 |
"composers": None, |
| 847 |
"remixers": None, |
| 848 |
"lyricists": None, |
| 849 |
} |
| 850 |
|
| 851 |
assert ( |
| 852 |
ArtistState.from_config( |
| 853 |
DiscogsPlugin().config, [_artist("Various")] |
| 854 |
).info |
| 855 |
== expected_info |
| 856 |
) |
| 857 |
|
| 858 |
|
| 859 |
@pytest.mark.parametrize( |
| 860 |
"position, medium, index, subindex", |
| 861 |
[ |
| 862 |
("1", None, "1", None), |
| 863 |
("A12", "A", "12", None), |
| 864 |
("12-34", "12-", "34", None), |
| 865 |
("CD1-1", "CD1-", "1", None), |
| 866 |
("1.12", None, "1", "12"), |
| 867 |
("12.a", None, "12", "A"), |
| 868 |
("12.34", None, "12", "34"), |
| 869 |
("1ab", None, "1", "AB"), |
| 870 |
# Non-standard |
| 871 |
("IV", "IV", None, None), |
| 872 |
], |
| 873 |
) |
| 874 |
def test_get_track_index(position, medium, index, subindex): |
| 875 |
assert DiscogsPlugin.get_track_index(position) == (medium, index, subindex) |