| 1 |
"""Tests for MusicBrainz API wrapper.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import uuid |
| 6 |
from typing import TYPE_CHECKING, ClassVar |
| 7 |
|
| 8 |
import pytest |
| 9 |
import requests |
| 10 |
|
| 11 |
from beets.library import Item |
| 12 |
from beets.test.helper import PluginMixin |
| 13 |
from beetsplug import musicbrainz |
| 14 |
from beetsplug.musicbrainz import MusicBrainzPlugin |
| 15 |
|
| 16 |
from .factories import musicbrainz as factories |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from beetsplug._utils import musicbrainz as mb |
| 20 |
|
| 21 |
_p = pytest.param |
| 22 |
|
| 23 |
|
| 24 |
def alias_factory(**kwargs) -> mb.Alias: |
| 25 |
return factories.AliasFactory.build(**kwargs) |
| 26 |
|
| 27 |
|
| 28 |
def artist_credit_factory(**kwargs) -> mb.ArtistCredit: |
| 29 |
return factories.ArtistCreditFactory.build(**kwargs) |
| 30 |
|
| 31 |
|
| 32 |
def artist_relation_factory(**kwargs) -> mb.ArtistRelation: |
| 33 |
return factories.ArtistRelationFactory.build(**kwargs) |
| 34 |
|
| 35 |
|
| 36 |
def label_info_factory(**kwargs) -> mb.LabelInfo: |
| 37 |
return factories.LabelInfoFactory.build(**kwargs) |
| 38 |
|
| 39 |
|
| 40 |
def recording_factory(**kwargs) -> mb.Recording: |
| 41 |
return factories.RecordingFactory.build(**kwargs) |
| 42 |
|
| 43 |
|
| 44 |
def track_factory(**kwargs) -> mb.Track: |
| 45 |
return factories.TrackFactory.build(**kwargs) |
| 46 |
|
| 47 |
|
| 48 |
def url_relation_factory(**kwargs) -> mb.UrlRelation: |
| 49 |
return factories.UrlRelationFactory.build(**kwargs) |
| 50 |
|
| 51 |
|
| 52 |
def medium_factory(**kwargs) -> mb.Medium: |
| 53 |
return factories.MediumFactory.build(**kwargs) |
| 54 |
|
| 55 |
|
| 56 |
def release_factory(**kwargs) -> mb.Release: |
| 57 |
return factories.ReleaseFactory.build(**kwargs) |
| 58 |
|
| 59 |
|
| 60 |
class TestUtils: |
| 61 |
@pytest.mark.parametrize( |
| 62 |
"date, expected_parts", |
| 63 |
[ |
| 64 |
("1987-03-01", (1987, 3, 1)), |
| 65 |
("1987-03", (1987, 3, None)), |
| 66 |
("1987", (1987, None, None)), |
| 67 |
], |
| 68 |
) |
| 69 |
def test_get_date(self, date, expected_parts): |
| 70 |
assert musicbrainz._get_date(date) == expected_parts |
| 71 |
|
| 72 |
def test_single_artist(self): |
| 73 |
credit = [artist_credit_factory(artist__name="Artist")] |
| 74 |
|
| 75 |
assert MusicBrainzPlugin()._parse_artist_credits(credit) == { |
| 76 |
"artist": "Artist", |
| 77 |
"artist_id": "00000000-0000-0000-0000-000000000001", |
| 78 |
"artist_sort": "Artist, The", |
| 79 |
"artist_credit": "Artist Credit", |
| 80 |
"artists": ["Artist"], |
| 81 |
"artists_ids": ["00000000-0000-0000-0000-000000000001"], |
| 82 |
"artists_sort": ["Artist, The"], |
| 83 |
"artists_credit": ["Artist Credit"], |
| 84 |
} |
| 85 |
|
| 86 |
def test_two_artists(self): |
| 87 |
credit = [ |
| 88 |
artist_credit_factory(artist__name="Artist", joinphrase=" AND "), |
| 89 |
artist_credit_factory(artist__name="Other Artist", artist__index=2), |
| 90 |
] |
| 91 |
|
| 92 |
assert MusicBrainzPlugin()._parse_artist_credits(credit) == { |
| 93 |
"artist": "Artist AND Other Artist", |
| 94 |
"artist_id": "00000000-0000-0000-0000-000000000001", |
| 95 |
"artist_sort": "Artist, The AND Other Artist, The", |
| 96 |
"artist_credit": "Artist Credit AND Other Artist Credit", |
| 97 |
"artists": ["Artist", "Other Artist"], |
| 98 |
"artists_ids": [ |
| 99 |
"00000000-0000-0000-0000-000000000001", |
| 100 |
"00000000-0000-0000-0000-000000000002", |
| 101 |
], |
| 102 |
"artists_sort": ["Artist, The", "Other Artist, The"], |
| 103 |
"artists_credit": ["Artist Credit", "Other Artist Credit"], |
| 104 |
} |
| 105 |
|
| 106 |
@pytest.mark.parametrize( |
| 107 |
"languages_config, expected_alias_name", |
| 108 |
[ |
| 109 |
_p([], None, id="no alias without languages"), |
| 110 |
_p(["en"], "Alias en", id="en primary"), |
| 111 |
_p(["en_GB", "en"], "Alias en_GB", id="en_GB primary"), |
| 112 |
_p(["en", "en_GB"], "Alias en", id="en primary over en_GB"), |
| 113 |
_p(["fr"], "Alias fr_P", id="fr primary"), |
| 114 |
_p(["pt_BR", "fr"], "Alias fr_P", id="non-primary ignored"), |
| 115 |
], |
| 116 |
) |
| 117 |
def test_preferred_alias( |
| 118 |
self, config, languages_config, expected_alias_name |
| 119 |
): |
| 120 |
aliases = [ |
| 121 |
alias_factory(locale="en"), |
| 122 |
alias_factory(locale="en_GB"), |
| 123 |
alias_factory(locale="fr", primary=False), |
| 124 |
alias_factory(suffix="fr_P", locale="fr"), |
| 125 |
alias_factory(locale="pt_BR", primary=False), |
| 126 |
] |
| 127 |
|
| 128 |
config["import"]["languages"] = languages_config |
| 129 |
|
| 130 |
alias = musicbrainz._preferred_alias(aliases) |
| 131 |
|
| 132 |
if expected_alias_name is None: |
| 133 |
assert not alias |
| 134 |
else: |
| 135 |
assert alias["name"] == expected_alias_name |
| 136 |
|
| 137 |
@pytest.mark.parametrize( |
| 138 |
"label_infos, expected", |
| 139 |
[ |
| 140 |
_p([], {"catalognum": None, "label": None}, id="no label"), |
| 141 |
_p( |
| 142 |
[label_info_factory(label=None)], |
| 143 |
{"catalognum": "LAB123", "label": None}, |
| 144 |
id="no label", |
| 145 |
), |
| 146 |
_p( |
| 147 |
[label_info_factory(label__name="[no label]")], |
| 148 |
{"catalognum": "LAB123", "label": None}, |
| 149 |
id="label with ignored [no label] name", |
| 150 |
), |
| 151 |
_p( |
| 152 |
[label_info_factory()], |
| 153 |
{"catalognum": "LAB123", "label": "Label"}, |
| 154 |
id="normal case", |
| 155 |
), |
| 156 |
], |
| 157 |
) |
| 158 |
def test_parse_label_info(self, label_infos, expected): |
| 159 |
assert MusicBrainzPlugin._parse_label_infos(label_infos) == expected |
| 160 |
|
| 161 |
|
| 162 |
class MusicBrainzPluginTestMixin(PluginMixin): |
| 163 |
plugin = "musicbrainz" |
| 164 |
|
| 165 |
@pytest.fixture |
| 166 |
def plugin_config(self): |
| 167 |
return {} |
| 168 |
|
| 169 |
@pytest.fixture |
| 170 |
def mb(self, plugin_config): |
| 171 |
self.config[self.plugin].set(plugin_config) |
| 172 |
|
| 173 |
return musicbrainz.MusicBrainzPlugin() |
| 174 |
|
| 175 |
|
| 176 |
class TestParseRecording(MusicBrainzPluginTestMixin): |
| 177 |
def test_parse_recording(self, mb): |
| 178 |
recording = recording_factory( |
| 179 |
length=None, |
| 180 |
disambiguation="Recording Disambiguation", |
| 181 |
artist_relations=[ |
| 182 |
artist_relation_factory( |
| 183 |
type="remixer", |
| 184 |
artist__index=1, |
| 185 |
artist__name="Recording Remixer", |
| 186 |
), |
| 187 |
artist_relation_factory( |
| 188 |
type="arranger", |
| 189 |
artist__index=2, |
| 190 |
artist__name="Recording Arranger", |
| 191 |
), |
| 192 |
artist_relation_factory( |
| 193 |
type="arranger", |
| 194 |
artist__index=3, |
| 195 |
artist__name="Another Recording Arranger", |
| 196 |
), |
| 197 |
artist_relation_factory(type="engineer"), |
| 198 |
], |
| 199 |
work_relations=[ |
| 200 |
{ |
| 201 |
"type": "performance", |
| 202 |
"work": { |
| 203 |
"id": "WORK ID", |
| 204 |
"title": "WORK TITLE", |
| 205 |
"artist_relations": [ |
| 206 |
artist_relation_factory( |
| 207 |
type="lyricist", |
| 208 |
artist__index=4, |
| 209 |
artist__name="Recording Lyricist", |
| 210 |
), |
| 211 |
artist_relation_factory( |
| 212 |
type="lyricist", |
| 213 |
artist__index=5, |
| 214 |
artist__name="Another Recording Lyricist", |
| 215 |
), |
| 216 |
artist_relation_factory( |
| 217 |
type="composer", |
| 218 |
artist__index=6, |
| 219 |
artist__name="Recording Composer", |
| 220 |
), |
| 221 |
artist_relation_factory( |
| 222 |
type="composer", |
| 223 |
artist__index=7, |
| 224 |
artist__name="Another Recording Composer", |
| 225 |
), |
| 226 |
artist_relation_factory(type="mastering"), |
| 227 |
], |
| 228 |
}, |
| 229 |
} |
| 230 |
], |
| 231 |
) |
| 232 |
|
| 233 |
assert mb.track_info(recording) == { |
| 234 |
"album": None, |
| 235 |
"arrangers": ["Recording Arranger", "Another Recording Arranger"], |
| 236 |
"arrangers_ids": [ |
| 237 |
"00000000-0000-0000-0000-000000000002", |
| 238 |
"00000000-0000-0000-0000-000000000003", |
| 239 |
], |
| 240 |
"artist": "Recording Artist", |
| 241 |
"artist_credit": "Recording Artist Credit", |
| 242 |
"artist_id": "00000000-0000-0000-0000-000000000001", |
| 243 |
"artist_sort": "Recording Artist, The", |
| 244 |
"artists": ["Recording Artist"], |
| 245 |
"artists_credit": ["Recording Artist Credit"], |
| 246 |
"artists_ids": ["00000000-0000-0000-0000-000000000001"], |
| 247 |
"artists_sort": ["Recording Artist, The"], |
| 248 |
"bpm": None, |
| 249 |
"composer_sort": "Recording Composer, The, Another Recording Composer, The", |
| 250 |
"composers": ["Recording Composer", "Another Recording Composer"], |
| 251 |
"composers_ids": [ |
| 252 |
"00000000-0000-0000-0000-000000000006", |
| 253 |
"00000000-0000-0000-0000-000000000007", |
| 254 |
], |
| 255 |
"data_source": "MusicBrainz", |
| 256 |
"data_url": "https://musicbrainz.org/recording/00000000-0000-0000-0000-000000001001", |
| 257 |
"disctitle": None, |
| 258 |
"genres": None, |
| 259 |
"index": None, |
| 260 |
"initial_key": None, |
| 261 |
"isrc": None, |
| 262 |
"length": None, |
| 263 |
"lyricists": ["Recording Lyricist", "Another Recording Lyricist"], |
| 264 |
"lyricists_ids": [ |
| 265 |
"00000000-0000-0000-0000-000000000004", |
| 266 |
"00000000-0000-0000-0000-000000000005", |
| 267 |
], |
| 268 |
"mb_workid": "WORK ID", |
| 269 |
"media": None, |
| 270 |
"medium": None, |
| 271 |
"medium_index": None, |
| 272 |
"medium_total": None, |
| 273 |
"release_track_id": None, |
| 274 |
"remixers": ["Recording Remixer"], |
| 275 |
"remixers_ids": ["00000000-0000-0000-0000-000000000001"], |
| 276 |
"title": "Recording", |
| 277 |
"track_alt": None, |
| 278 |
"track_id": "00000000-0000-0000-0000-000000001001", |
| 279 |
"trackdisambig": "Recording Disambiguation", |
| 280 |
"work": "WORK TITLE", |
| 281 |
"work_disambig": None, |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
class TestParseMedia(MusicBrainzPluginTestMixin): |
| 286 |
def test_multiple_mediums(self, mb): |
| 287 |
first_medium = medium_factory( |
| 288 |
title="First Medium", |
| 289 |
position=1, |
| 290 |
tracks=[ |
| 291 |
track_factory(recording__length=100000), |
| 292 |
track_factory( |
| 293 |
position=2, |
| 294 |
recording__index=2, |
| 295 |
recording__length=200000, |
| 296 |
recording__title="Other Recording", |
| 297 |
), |
| 298 |
], |
| 299 |
) |
| 300 |
second_medium = medium_factory( |
| 301 |
title="Second Medium", |
| 302 |
position=2, |
| 303 |
pregap=track_factory(recording__title="Pregap", position=0), |
| 304 |
tracks=[track_factory()], |
| 305 |
) |
| 306 |
release = release_factory(media=[first_medium, second_medium]) |
| 307 |
|
| 308 |
d = mb.album_info(release) |
| 309 |
|
| 310 |
assert d.mediums == 2 |
| 311 |
t = d.tracks |
| 312 |
assert len(t) == 4 |
| 313 |
|
| 314 |
assert t[0].title == "Recording" |
| 315 |
assert t[0].track_id == "00000000-0000-0000-0000-000000001001" |
| 316 |
assert t[0].length == 100.0 |
| 317 |
assert t[0].medium == 1 |
| 318 |
assert t[0].medium_index == 1 |
| 319 |
assert t[0].index == 1 |
| 320 |
assert t[0].disctitle == "First Medium" |
| 321 |
|
| 322 |
assert t[1].title == "Other Recording" |
| 323 |
assert t[1].track_id == "00000000-0000-0000-0000-000000001002" |
| 324 |
assert t[1].length == 200.0 |
| 325 |
assert t[1].medium == 1 |
| 326 |
assert t[1].medium_index == 2 |
| 327 |
assert t[1].index == 2 |
| 328 |
assert t[1].disctitle == "First Medium" |
| 329 |
|
| 330 |
assert t[2].title == "Pregap" |
| 331 |
assert t[2].medium == 2 |
| 332 |
assert t[2].medium_index == 0 |
| 333 |
assert t[2].index == 3 |
| 334 |
assert t[2].disctitle == "Second Medium" |
| 335 |
|
| 336 |
assert t[3].medium == 2 |
| 337 |
assert t[3].medium_index == 1 |
| 338 |
assert t[3].index == 4 |
| 339 |
assert t[3].disctitle == "Second Medium" |
| 340 |
|
| 341 |
def test_track_overrides_recording(self, mb): |
| 342 |
release = release_factory( |
| 343 |
media__0__tracks=[ |
| 344 |
track_factory( |
| 345 |
length=1000.0, |
| 346 |
artist_credit=[ |
| 347 |
artist_credit_factory( |
| 348 |
artist__name="Track Artist", joinphrase=" & " |
| 349 |
), |
| 350 |
artist_credit_factory( |
| 351 |
artist__name="Other Track Artist", artist__index=2 |
| 352 |
), |
| 353 |
], |
| 354 |
recording__length=2000.0, |
| 355 |
recording__artist_credit=[ |
| 356 |
artist_credit_factory( |
| 357 |
artist__name="Recording Artist", joinphrase=" & " |
| 358 |
), |
| 359 |
artist_credit_factory( |
| 360 |
artist__name="Other Recording Artist", |
| 361 |
artist__index=2, |
| 362 |
), |
| 363 |
], |
| 364 |
) |
| 365 |
] |
| 366 |
) |
| 367 |
|
| 368 |
track = mb.album_info(release).tracks[0] |
| 369 |
|
| 370 |
assert track.length == 1.0 |
| 371 |
assert track.artist == "Track Artist & Other Track Artist" |
| 372 |
assert track.artist_id == "00000000-0000-0000-0000-000000000001" |
| 373 |
assert ( |
| 374 |
track.artist_sort == "Track Artist, The & Other Track Artist, The" |
| 375 |
) |
| 376 |
assert ( |
| 377 |
track.artist_credit |
| 378 |
== "Track Artist Credit & Other Track Artist Credit" |
| 379 |
) |
| 380 |
|
| 381 |
assert track.artists == ["Track Artist", "Other Track Artist"] |
| 382 |
assert track.artists_ids == [ |
| 383 |
"00000000-0000-0000-0000-000000000001", |
| 384 |
"00000000-0000-0000-0000-000000000002", |
| 385 |
] |
| 386 |
assert track.artists_sort == [ |
| 387 |
"Track Artist, The", |
| 388 |
"Other Track Artist, The", |
| 389 |
] |
| 390 |
assert track.artists_credit == [ |
| 391 |
"Track Artist Credit", |
| 392 |
"Other Track Artist Credit", |
| 393 |
] |
| 394 |
|
| 395 |
def test_missing_tracks(self, mb): |
| 396 |
release = release_factory( |
| 397 |
media=[ |
| 398 |
medium_factory(), |
| 399 |
medium_factory( |
| 400 |
tracks=[ |
| 401 |
track_factory(), |
| 402 |
track_factory( |
| 403 |
recording__title="Other Recording", |
| 404 |
recording__disambiguation="SECOND TRACK", |
| 405 |
), |
| 406 |
] |
| 407 |
), |
| 408 |
] |
| 409 |
) |
| 410 |
|
| 411 |
assert mb.album_info(release).mediums == 2 |
| 412 |
|
| 413 |
@pytest.mark.parametrize( |
| 414 |
"beets_match_config, expected_titles", |
| 415 |
[ |
| 416 |
_p({}, ("Audio",), id="only audio tracks by default"), |
| 417 |
_p( |
| 418 |
{"ignore_data_tracks": False}, |
| 419 |
("Audio", "Data"), |
| 420 |
id="include data tracks", |
| 421 |
), |
| 422 |
_p( |
| 423 |
{"ignore_data_tracks": False, "ignore_video_tracks": False}, |
| 424 |
("Audio", "Video: Video", "Data"), |
| 425 |
id="include data and video tracks", |
| 426 |
), |
| 427 |
_p({"ignored_media": "Vinyl"}, (), id="ignore all tracks"), |
| 428 |
], |
| 429 |
) |
| 430 |
def test_data_tracks(self, config, beets_match_config, mb, expected_titles): |
| 431 |
medium = medium_factory( |
| 432 |
format="Vinyl", |
| 433 |
tracks=[ |
| 434 |
track_factory(recording__title="Audio"), |
| 435 |
track_factory(recording__title="[data track]"), |
| 436 |
track_factory(recording__title="Video", recording__video=True), |
| 437 |
], |
| 438 |
data_tracks=[track_factory(recording__title="Data")], |
| 439 |
) |
| 440 |
release = release_factory(media=[medium]) |
| 441 |
|
| 442 |
config.set({"match": beets_match_config}) |
| 443 |
|
| 444 |
actual_titles = tuple(t.title for t in mb.album_info(release).tracks) |
| 445 |
|
| 446 |
assert actual_titles == expected_titles |
| 447 |
|
| 448 |
|
| 449 |
class TestParseRelease(MusicBrainzPluginTestMixin): |
| 450 |
def test_parse_release(self, config, mb): |
| 451 |
config["match"]["preferred"]["countries"] = ["US"] |
| 452 |
mb.config.set( |
| 453 |
{ |
| 454 |
"external_ids": { |
| 455 |
"discogs": True, |
| 456 |
"bandcamp": True, |
| 457 |
"spotify": False, |
| 458 |
} |
| 459 |
} |
| 460 |
) |
| 461 |
|
| 462 |
release = release_factory( |
| 463 |
url_relations=[ |
| 464 |
url_relation_factory( |
| 465 |
url__resource="https://discogs.com/release/123456" |
| 466 |
), |
| 467 |
url_relation_factory( |
| 468 |
url__resource="https://open.spotify.com/album/ABCDab2ImQyHZ9sXCXFyZ8" |
| 469 |
), |
| 470 |
url_relation_factory( |
| 471 |
url__resource="https://somemusic.bandcamp.com/album/somealbum" |
| 472 |
), |
| 473 |
] |
| 474 |
) |
| 475 |
d = mb.album_info(release) |
| 476 |
|
| 477 |
assert d == { |
| 478 |
"album": "Album", |
| 479 |
"album_id": "00000000-0000-0000-0000-000001000001", |
| 480 |
"albumdisambig": "Album Disambiguation", |
| 481 |
"albumstatus": "Official", |
| 482 |
"albumtype": "album", |
| 483 |
"albumtypes": ["album"], |
| 484 |
"artist": "Artist", |
| 485 |
"artist_credit": "Artist Credit", |
| 486 |
"artist_id": "00000000-0000-0000-0000-000000000011", |
| 487 |
"artist_sort": "Artist, The", |
| 488 |
"artists": ["Artist"], |
| 489 |
"artists_credit": ["Artist Credit"], |
| 490 |
"artists_ids": ["00000000-0000-0000-0000-000000000011"], |
| 491 |
"artists_sort": ["Artist, The"], |
| 492 |
"asin": "Album Asin", |
| 493 |
"bandcamp_album_id": "https://somemusic.bandcamp.com/album/somealbum", |
| 494 |
"barcode": "0000000000000", |
| 495 |
"catalognum": "LAB123", |
| 496 |
"country": "US", |
| 497 |
"data_source": "MusicBrainz", |
| 498 |
"data_url": "https://musicbrainz.org/release/00000000-0000-0000-0000-000001000001", |
| 499 |
"day": 1, |
| 500 |
"discogs_album_id": "123456", |
| 501 |
"discogs_albumid": None, |
| 502 |
"discogs_artistid": None, |
| 503 |
"discogs_labelid": None, |
| 504 |
"genres": None, |
| 505 |
"label": "Label", |
| 506 |
"language": "eng", |
| 507 |
"media": "Digital Media", |
| 508 |
"mediums": 1, |
| 509 |
"month": 1, |
| 510 |
"original_day": 3, |
| 511 |
"original_month": 2, |
| 512 |
"original_year": 2001, |
| 513 |
"release_group_title": "Release Group", |
| 514 |
"releasegroup_id": "00000000-0000-0000-0000-000000000101", |
| 515 |
"releasegroupdisambig": "Release Group Disambiguation", |
| 516 |
"script": "Latn", |
| 517 |
"style": None, |
| 518 |
"tracks": [ |
| 519 |
{ |
| 520 |
"album": None, |
| 521 |
"arrangers": None, |
| 522 |
"arrangers_ids": [], |
| 523 |
"artist": "Recording Artist", |
| 524 |
"artist_credit": "Recording Artist Credit", |
| 525 |
"artist_id": "00000000-0000-0000-0000-000000000001", |
| 526 |
"artist_sort": "Recording Artist, The", |
| 527 |
"artists": ["Recording Artist"], |
| 528 |
"artists_credit": ["Recording Artist Credit"], |
| 529 |
"artists_ids": ["00000000-0000-0000-0000-000000000001"], |
| 530 |
"artists_sort": ["Recording Artist, The"], |
| 531 |
"bpm": None, |
| 532 |
"composer_sort": None, |
| 533 |
"composers": None, |
| 534 |
"composers_ids": [], |
| 535 |
"data_source": "MusicBrainz", |
| 536 |
"data_url": "https://musicbrainz.org/recording/00000000-0000-0000-0000-000000001001", |
| 537 |
"disctitle": "Medium", |
| 538 |
"genres": None, |
| 539 |
"index": 1, |
| 540 |
"initial_key": None, |
| 541 |
"isrc": None, |
| 542 |
"length": 0.36, |
| 543 |
"lyricists": None, |
| 544 |
"lyricists_ids": [], |
| 545 |
"mb_workid": None, |
| 546 |
"media": "Digital Media", |
| 547 |
"medium": 1, |
| 548 |
"medium_index": 1, |
| 549 |
"medium_total": 1, |
| 550 |
"release_track_id": "00000000-0000-0000-0000-000000010001", |
| 551 |
"remixers": None, |
| 552 |
"remixers_ids": [], |
| 553 |
"title": "Recording", |
| 554 |
"track_alt": "A1", |
| 555 |
"track_id": "00000000-0000-0000-0000-000000001001", |
| 556 |
"trackdisambig": None, |
| 557 |
"work": None, |
| 558 |
"work_disambig": None, |
| 559 |
} |
| 560 |
], |
| 561 |
"va": False, |
| 562 |
"year": 2020, |
| 563 |
} |
| 564 |
|
| 565 |
def test_detect_various_artists(self, mb): |
| 566 |
release = release_factory( |
| 567 |
artist_credit=[ |
| 568 |
artist_credit_factory(artist__id=musicbrainz.VARIOUS_ARTISTS_ID) |
| 569 |
] |
| 570 |
) |
| 571 |
|
| 572 |
assert mb.album_info(release).va |
| 573 |
|
| 574 |
def test_missing_language(self, mb): |
| 575 |
release = release_factory(text_representation__language=None) |
| 576 |
|
| 577 |
assert mb.album_info(release).language is None |
| 578 |
|
| 579 |
@pytest.mark.parametrize( |
| 580 |
"plugin_config, expected_genres", |
| 581 |
[ |
| 582 |
_p({"genres": False}, None, id="genres disabled"), |
| 583 |
_p({"genres": True, "genres_tag": "genre"}, ["Genre"], id="use genres"), |
| 584 |
_p({"genres": True, "genres_tag": "tag"}, ["Tag"], id="use tags"), |
| 585 |
], |
| 586 |
) # fmt: skip |
| 587 |
def test_genres(self, mb, expected_genres): |
| 588 |
assert mb.album_info(release_factory()).genres == expected_genres |
| 589 |
|
| 590 |
@pytest.mark.parametrize( |
| 591 |
"plugin_config", [_p({"aliases_as_credits": True})] |
| 592 |
) |
| 593 |
def test_parse_aliased_titles(self, config, mb: MusicBrainzPlugin): |
| 594 |
release = release_factory() |
| 595 |
|
| 596 |
config["import"]["languages"] = ["en"] |
| 597 |
|
| 598 |
d = mb.album_info(release) |
| 599 |
|
| 600 |
assert d.album == "Album Alias en" |
| 601 |
assert d.release_group_title == "Release Group Alias en" |
| 602 |
assert d.tracks[0].title == "Recording Alias en" |
| 603 |
|
| 604 |
album_artist = "Artist Alias en" |
| 605 |
assert d.artist == album_artist |
| 606 |
assert d.artists == [album_artist] |
| 607 |
# There is no artist credit specific alias |
| 608 |
assert d.artist_credit == album_artist |
| 609 |
assert d.artists_credit == [album_artist] |
| 610 |
|
| 611 |
album_artist_sort = "Artist Alias en, The" |
| 612 |
assert d.artist_sort == album_artist_sort |
| 613 |
assert d.artists_sort == [album_artist_sort] |
| 614 |
|
| 615 |
first_track = d.tracks[0] |
| 616 |
|
| 617 |
track_artist = "Recording Artist Alias en" |
| 618 |
assert first_track.artist == track_artist |
| 619 |
assert first_track.artists == [track_artist] |
| 620 |
# There is no artist credit specific alias |
| 621 |
assert first_track.artist_credit == track_artist |
| 622 |
assert first_track.artists_credit == [track_artist] |
| 623 |
|
| 624 |
track_artist_sort = "Recording Artist Alias en, The" |
| 625 |
assert first_track.artist_sort == track_artist_sort |
| 626 |
assert first_track.artists_sort == [track_artist_sort] |
| 627 |
|
| 628 |
@pytest.mark.parametrize( |
| 629 |
"plugin_config, expected_artist_credit", |
| 630 |
[ |
| 631 |
_p( |
| 632 |
{"aliases_as_credits": False}, |
| 633 |
{ |
| 634 |
"track_artist_credit": "Recording Artist Credit", |
| 635 |
"track_artists_credit": ["Recording Artist Credit"], |
| 636 |
"artist_credit": "Artist Credit", |
| 637 |
"artists_credit": ["Artist Credit"], |
| 638 |
}, |
| 639 |
id="no aliases", |
| 640 |
), |
| 641 |
_p( |
| 642 |
{"aliases_as_credits": True}, |
| 643 |
{ |
| 644 |
"artist_credit": "Artist Alias en", |
| 645 |
"artists_credit": ["Artist Alias en"], |
| 646 |
"track_artist_credit": "Recording Artist Alias en", |
| 647 |
"track_artists_credit": ["Recording Artist Alias en"], |
| 648 |
}, |
| 649 |
id="aliases", |
| 650 |
), |
| 651 |
], |
| 652 |
) |
| 653 |
def test_aliases_as_credits( |
| 654 |
self, config, mb: MusicBrainzPlugin, expected_artist_credit |
| 655 |
): |
| 656 |
config["import"]["languages"] = ["en"] |
| 657 |
release = release_factory() |
| 658 |
|
| 659 |
d = mb.album_info(release) |
| 660 |
|
| 661 |
assert d.artist_credit == expected_artist_credit["artist_credit"] |
| 662 |
assert d.artists_credit == expected_artist_credit["artists_credit"] |
| 663 |
|
| 664 |
first_track = d.tracks[0] |
| 665 |
|
| 666 |
assert ( |
| 667 |
first_track.artist_credit |
| 668 |
== expected_artist_credit["track_artist_credit"] |
| 669 |
) |
| 670 |
assert ( |
| 671 |
first_track.artists_credit |
| 672 |
== expected_artist_credit["track_artists_credit"] |
| 673 |
) |
| 674 |
|
| 675 |
def test_ensure_complete_recordings(self, monkeypatch, mb): |
| 676 |
titles = ["Recording", "Other Recording"] |
| 677 |
initial_recordings = [ |
| 678 |
recording_factory(index=idx, title=t) |
| 679 |
for idx, t in enumerate(titles) |
| 680 |
] |
| 681 |
complete_recordings = [ |
| 682 |
{**r, "url_relations": [url_relation_factory()]} |
| 683 |
for r in initial_recordings |
| 684 |
] |
| 685 |
|
| 686 |
monkeypatch.setattr("beetsplug.musicbrainz.BROWSE_CHUNKSIZE", 1) |
| 687 |
monkeypatch.setattr("beetsplug.musicbrainz.BROWSE_MAXTRACKS", 1) |
| 688 |
monkeypatch.setattr( |
| 689 |
mb.mb_api, |
| 690 |
"browse_recordings", |
| 691 |
lambda offset=0, **__: [complete_recordings[offset]], |
| 692 |
) |
| 693 |
|
| 694 |
release = release_factory( |
| 695 |
media__0__tracks=[ |
| 696 |
track_factory(recording=r) for r in initial_recordings |
| 697 |
] |
| 698 |
) |
| 699 |
|
| 700 |
mb._ensure_complete_recordings(release) |
| 701 |
|
| 702 |
new_recordings = [ |
| 703 |
t["recording"] for m in release["media"] for t in m["tracks"] |
| 704 |
] |
| 705 |
assert new_recordings == complete_recordings |
| 706 |
|
| 707 |
def test_album_info_browse_recordings_prefers_alias_over_track_title( |
| 708 |
self, config, monkeypatch, mb |
| 709 |
): |
| 710 |
config["import"]["languages"] = ["en"] |
| 711 |
|
| 712 |
recording = recording_factory( |
| 713 |
title="Recording Title", |
| 714 |
aliases=[alias_factory(type="Recording name", locale="en")], |
| 715 |
) |
| 716 |
browsed_recording = { |
| 717 |
**recording, |
| 718 |
"url_relations": [url_relation_factory()], |
| 719 |
} |
| 720 |
|
| 721 |
monkeypatch.setattr("beetsplug.musicbrainz.BROWSE_CHUNKSIZE", 1) |
| 722 |
monkeypatch.setattr("beetsplug.musicbrainz.BROWSE_MAXTRACKS", 0) |
| 723 |
monkeypatch.setattr( |
| 724 |
mb.mb_api, |
| 725 |
"browse_recordings", |
| 726 |
lambda offset=0, **__: [browsed_recording] if offset == 0 else [], |
| 727 |
) |
| 728 |
|
| 729 |
release = release_factory( |
| 730 |
media__0__tracks=[ |
| 731 |
track_factory(recording=recording, title="Track Title") |
| 732 |
] |
| 733 |
) |
| 734 |
|
| 735 |
album = mb.album_info(release) |
| 736 |
|
| 737 |
assert album.tracks[0].title == "Alias en" |
| 738 |
|
| 739 |
|
| 740 |
class TestPseudoRelease(MusicBrainzPluginTestMixin): |
| 741 |
ACTUAL_RELEASE = release_factory(index=1, country="US") |
| 742 |
PSEUDO_WITHOUT_LINKS = release_factory( |
| 743 |
index=2, status="Pseudo-Release", country=None |
| 744 |
) |
| 745 |
PSEUDO_INVALID_LINK = release_factory( |
| 746 |
index=3, |
| 747 |
status="Pseudo-Release", |
| 748 |
country=None, |
| 749 |
release_relations=[ |
| 750 |
{ |
| 751 |
"type": "remaster", |
| 752 |
"direction": "backward", |
| 753 |
"release": {"id": "d2a6f856-b553-40a0-ac54-a321e8e2da01"}, |
| 754 |
} |
| 755 |
], |
| 756 |
) |
| 757 |
PSEUDO_VALID_LINK = release_factory( |
| 758 |
index=4, |
| 759 |
status="Pseudo-Release", |
| 760 |
country=None, |
| 761 |
release_relations=[ |
| 762 |
{ |
| 763 |
"type": "transl-tracklisting", |
| 764 |
"direction": "backward", |
| 765 |
"release": {"id": ACTUAL_RELEASE["id"]}, |
| 766 |
} |
| 767 |
], |
| 768 |
) |
| 769 |
|
| 770 |
@pytest.fixture(autouse=True) |
| 771 |
def setup_album_lookup(self, monkeypatch, mb): |
| 772 |
releases = [ |
| 773 |
self.ACTUAL_RELEASE, |
| 774 |
self.PSEUDO_WITHOUT_LINKS, |
| 775 |
self.PSEUDO_INVALID_LINK, |
| 776 |
self.PSEUDO_VALID_LINK, |
| 777 |
] |
| 778 |
release_by_id = {r["id"]: r for r in releases} |
| 779 |
|
| 780 |
monkeypatch.setattr( |
| 781 |
mb.mb_api, "get_release", lambda id_: release_by_id[id_] |
| 782 |
) |
| 783 |
|
| 784 |
@pytest.mark.parametrize( |
| 785 |
"release_id, expected_country", |
| 786 |
[ |
| 787 |
_p(ACTUAL_RELEASE["id"], "US", id="actual release"), |
| 788 |
_p(PSEUDO_WITHOUT_LINKS["id"], None, id="pseudo without links"), |
| 789 |
_p(PSEUDO_INVALID_LINK["id"], None, id="pseudo with invalid link"), |
| 790 |
_p(PSEUDO_VALID_LINK["id"], "US", id="pseudo with valid link"), |
| 791 |
], |
| 792 |
) |
| 793 |
def test_follow_pseudo_release(self, mb, release_id, expected_country): |
| 794 |
album = mb.album_for_id(release_id) |
| 795 |
|
| 796 |
assert album |
| 797 |
assert album.country == expected_country |
| 798 |
|
| 799 |
|
| 800 |
class TestMusicBrainzPlugin(MusicBrainzPluginTestMixin): |
| 801 |
mbid = "d2a6f856-b553-40a0-ac54-a321e8e2da99" |
| 802 |
RECORDING: ClassVar[mb.Recording] = recording_factory() |
| 803 |
|
| 804 |
@pytest.mark.parametrize( |
| 805 |
"plugin_config,va_likely,expected_additional_criteria", |
| 806 |
[ |
| 807 |
_p({}, False, {"artist": "Artist "}, id="default"), |
| 808 |
_p( |
| 809 |
{}, |
| 810 |
True, |
| 811 |
{"arid": "89ad4ac3-39f7-470e-963a-56509c546377"}, |
| 812 |
id="va likely", |
| 813 |
), |
| 814 |
_p( |
| 815 |
{"extra_tags": ["label", "catalognum"]}, |
| 816 |
False, |
| 817 |
{"artist": "Artist ", "label": "abc", "catno": "ABC123"}, |
| 818 |
id="value-based extra_tags", |
| 819 |
), |
| 820 |
_p( |
| 821 |
{"extra_tags": ["alias", "tracks"]}, |
| 822 |
False, |
| 823 |
{"artist": "Artist ", "alias": " Album", "tracks": "3"}, |
| 824 |
id="non-value-based extra_tags", |
| 825 |
), |
| 826 |
], |
| 827 |
) |
| 828 |
def test_get_album_criteria( |
| 829 |
self, mb, va_likely, expected_additional_criteria |
| 830 |
): |
| 831 |
items = [ |
| 832 |
Item(catalognum="ABC 123", label="abc"), |
| 833 |
Item(catalognum="ABC 123", label="abc"), |
| 834 |
Item(catalognum="ABC 123", label="def"), |
| 835 |
] |
| 836 |
|
| 837 |
assert mb.get_album_criteria(items, "Artist ", " Album", va_likely) == { |
| 838 |
"release": " Album", |
| 839 |
**expected_additional_criteria, |
| 840 |
} |
| 841 |
|
| 842 |
def test_item_candidates(self, monkeypatch, mb): |
| 843 |
monkeypatch.setattr( |
| 844 |
"beetsplug._utils.musicbrainz.MusicBrainzAPI.get_json", |
| 845 |
lambda *_, **__: {"recordings": [self.RECORDING]}, |
| 846 |
) |
| 847 |
monkeypatch.setattr( |
| 848 |
"beetsplug._utils.musicbrainz.MusicBrainzAPI.get_recording", |
| 849 |
lambda *_, **__: self.RECORDING, |
| 850 |
) |
| 851 |
|
| 852 |
candidates = list(mb.item_candidates(Item(), "hello", "there")) |
| 853 |
|
| 854 |
assert len(candidates) == 1 |
| 855 |
assert candidates[0].track_id == self.RECORDING["id"] |
| 856 |
|
| 857 |
def test_candidates(self, monkeypatch, mb): |
| 858 |
monkeypatch.setattr( |
| 859 |
"beetsplug._utils.musicbrainz.MusicBrainzAPI.get_json", |
| 860 |
lambda *_, **__: {"releases": [{"id": self.mbid}]}, |
| 861 |
) |
| 862 |
monkeypatch.setattr( |
| 863 |
"beetsplug._utils.musicbrainz.MusicBrainzAPI.get_release", |
| 864 |
lambda *_, **__: release_factory( |
| 865 |
id=self.mbid, media=[medium_factory()] |
| 866 |
), |
| 867 |
) |
| 868 |
candidates = list(mb.candidates([], "hello", "there", False)) |
| 869 |
|
| 870 |
assert len(candidates) == 1 |
| 871 |
assert ( |
| 872 |
candidates[0].tracks[0].track_id |
| 873 |
== "00000000-0000-0000-0000-000000001001" |
| 874 |
) |
| 875 |
assert candidates[0].album == "Album" |
| 876 |
|
| 877 |
def test_import_handles_404_gracefully(self, mb, requests_mock): |
| 878 |
id_ = uuid.uuid4() |
| 879 |
response = requests.Response() |
| 880 |
response.status_code = 404 |
| 881 |
requests_mock.get( |
| 882 |
f"/ws/2/release/{id_}", |
| 883 |
exc=requests.exceptions.HTTPError(response=response), |
| 884 |
) |
| 885 |
res = mb.album_for_id(str(id_)) |
| 886 |
assert res is None |
| 887 |
|
| 888 |
def test_import_propagates_non_404_errors(self, mb): |
| 889 |
class DummyResponse: |
| 890 |
status_code = 500 |
| 891 |
|
| 892 |
error = requests.exceptions.HTTPError(response=DummyResponse()) |
| 893 |
|
| 894 |
def raise_error(*args, **kwargs): |
| 895 |
raise error |
| 896 |
|
| 897 |
# Simulate mb.mb_api.get_release raising a non-404 HTTP error |
| 898 |
mb.mb_api.get_release = raise_error |
| 899 |
|
| 900 |
with pytest.raises(requests.exceptions.HTTPError) as excinfo: |
| 901 |
mb.album_for_id(str(uuid.uuid4())) |
| 902 |
|
| 903 |
# Ensure the exact error is propagated, not swallowed |
| 904 |
assert excinfo.value is error |
| 905 |
|
| 906 |
@pytest.mark.parametrize( |
| 907 |
"input_,expected", |
| 908 |
[ |
| 909 |
("??-??-??", (None, None, None)), |
| 910 |
("??-01-??", (None, 1, None)), |
| 911 |
("??-??-02", (None, None, 2)), |
| 912 |
("??-01-02", (None, 1, 2)), |
| 913 |
("2010-??-01", (2010, None, 1)), |
| 914 |
("2010-01-not_an_int", (2010, 1, None)), |
| 915 |
("2010", (2010, None, None)), |
| 916 |
("2010-01", (2010, 1, None)), |
| 917 |
("2010-01-02", (2010, 1, 2)), |
| 918 |
], |
| 919 |
) |
| 920 |
def test_get_date(self, input_, expected): |
| 921 |
assert musicbrainz._get_date(input_) == expected |