| 1 |
"""Tests for autotagging functionality.""" |
| 2 |
|
| 3 |
import operator |
| 4 |
from contextlib import nullcontext as does_not_warn |
| 5 |
from functools import cached_property |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from beets.autotag import ( |
| 10 |
AlbumInfo, |
| 11 |
AlbumMatch, |
| 12 |
Distance, |
| 13 |
TrackInfo, |
| 14 |
TrackMatch, |
| 15 |
correct_list_fields, |
| 16 |
) |
| 17 |
from beets.library import Item |
| 18 |
from beets.test.helper import BeetsTestCase |
| 19 |
from beets.util import AttrDict |
| 20 |
|
| 21 |
str_field_deprecation = pytest.warns( |
| 22 |
DeprecationWarning, match="The 'genre' field is deprecated" |
| 23 |
) |
| 24 |
|
| 25 |
_p = pytest.param |
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
"str_value, list_value, expected_warning, expected_list_value", |
| 30 |
[ |
| 31 |
_p( |
| 32 |
"", |
| 33 |
None, |
| 34 |
str_field_deprecation, |
| 35 |
None, |
| 36 |
id="empty str value should become None, warning raised", |
| 37 |
), |
| 38 |
_p( |
| 39 |
"value", |
| 40 |
None, |
| 41 |
str_field_deprecation, |
| 42 |
["value"], |
| 43 |
id="str value only, warning raised", |
| 44 |
), |
| 45 |
_p( |
| 46 |
"value; another value", |
| 47 |
None, |
| 48 |
str_field_deprecation, |
| 49 |
["value", "another value"], |
| 50 |
id="str value only and split, warning raised", |
| 51 |
), |
| 52 |
_p( |
| 53 |
"value", |
| 54 |
["list value"], |
| 55 |
str_field_deprecation, |
| 56 |
["list value"], |
| 57 |
id="list value wins, warning raised", |
| 58 |
), |
| 59 |
_p(None, None, does_not_warn(), None, id="no str value, no warning"), |
| 60 |
], |
| 61 |
) |
| 62 |
class TestLegacyStringField: |
| 63 |
def test_init_info( |
| 64 |
self, str_value, list_value, expected_warning, expected_list_value |
| 65 |
): |
| 66 |
with expected_warning: |
| 67 |
actual_list_value = TrackInfo._get_list_from_string_value( |
| 68 |
"genre", "genres", str_value, list_value |
| 69 |
) |
| 70 |
|
| 71 |
assert actual_list_value == expected_list_value |
| 72 |
|
| 73 |
def test_set_str_value( |
| 74 |
self, str_value, list_value, expected_warning, expected_list_value |
| 75 |
): |
| 76 |
info = TrackInfo(genres=list_value) |
| 77 |
with expected_warning: |
| 78 |
info["genre"] = str_value |
| 79 |
|
| 80 |
assert info["genres"] == expected_list_value |
| 81 |
|
| 82 |
|
| 83 |
class ApplyTest(BeetsTestCase): |
| 84 |
def _apply( |
| 85 |
self, |
| 86 |
per_disc_numbering=False, |
| 87 |
artist_credit=False, |
| 88 |
original_date=False, |
| 89 |
from_scratch=False, |
| 90 |
): |
| 91 |
info = self.info |
| 92 |
mapping = dict(zip(self.items, info.tracks)) |
| 93 |
self.config["per_disc_numbering"] = per_disc_numbering |
| 94 |
self.config["artist_credit"] = artist_credit |
| 95 |
self.config["original_date"] = original_date |
| 96 |
self.config["import"]["from_scratch"] = from_scratch |
| 97 |
amatch = AlbumMatch(Distance(), self.info, mapping) |
| 98 |
amatch.apply_metadata() |
| 99 |
return amatch |
| 100 |
|
| 101 |
def setUp(self): |
| 102 |
super().setUp() |
| 103 |
|
| 104 |
self.items = [Item(), Item()] |
| 105 |
self.info = AlbumInfo( |
| 106 |
tracks=[ |
| 107 |
TrackInfo( |
| 108 |
title="title", |
| 109 |
track_id="dfa939ec-118c-4d0f-84a0-60f3d1e6522c", |
| 110 |
medium=1, |
| 111 |
medium_index=1, |
| 112 |
medium_total=1, |
| 113 |
index=1, |
| 114 |
artist="trackArtist", |
| 115 |
artist_credit="trackArtistCredit", |
| 116 |
artists_credit=["trackArtistCredit"], |
| 117 |
artist_sort="sortLast, sortFirst and sortLast2, sortFirst2", |
| 118 |
artists_sort=[ |
| 119 |
"sortLast, sortFirst", |
| 120 |
"sortLast2, sortFirst2", |
| 121 |
], |
| 122 |
arranger="trackArranger1, trackArranger2", |
| 123 |
composer="trackComposer1, trackComposer2", |
| 124 |
genres=["Rock"], |
| 125 |
lyricist="trackLyricist1, trackLyricist2", |
| 126 |
remixer="trackRemixer1, trackRemixer2", |
| 127 |
), |
| 128 |
TrackInfo( |
| 129 |
title="title2", |
| 130 |
track_id="40130ed1-a27c-42fd-a328-1ebefb6caef4", |
| 131 |
medium=2, |
| 132 |
medium_index=1, |
| 133 |
index=2, |
| 134 |
medium_total=1, |
| 135 |
arrangers=["trackArranger1", "trackArranger2"], |
| 136 |
composers=["trackComposer1", "trackComposer2"], |
| 137 |
lyricists=["trackLyricist1", "trackLyricist2"], |
| 138 |
remixers=["trackRemixer1", "trackRemixer2"], |
| 139 |
), |
| 140 |
], |
| 141 |
artist="albumArtist", |
| 142 |
artists=["albumArtist", "albumArtist2"], |
| 143 |
album="album", |
| 144 |
album_id="7edb51cb-77d6-4416-a23c-3a8c2994a2c7", |
| 145 |
artist_id="a6623d39-2d8e-4f70-8242-0a9553b91e50", |
| 146 |
artists_ids=None, |
| 147 |
artist_credit="albumArtistCredit", |
| 148 |
artists_credit=["albumArtistCredit1", "albumArtistCredit2"], |
| 149 |
artist_sort=None, |
| 150 |
artists_sort=["albumArtistSort", "albumArtistSort2"], |
| 151 |
albumtype="album", |
| 152 |
va=True, |
| 153 |
mediums=2, |
| 154 |
data_source="MusicBrainz", |
| 155 |
year=2013, |
| 156 |
month=12, |
| 157 |
day=18, |
| 158 |
genre="Rock; Pop", |
| 159 |
) |
| 160 |
|
| 161 |
common_expected = { |
| 162 |
"album": "album", |
| 163 |
"albumartist_credit": "albumArtistCredit", |
| 164 |
"albumartist": "albumArtist", |
| 165 |
"albumartists": ["albumArtist", "albumArtist2"], |
| 166 |
"albumartists_credit": [ |
| 167 |
"albumArtistCredit", |
| 168 |
"albumArtistCredit1", |
| 169 |
"albumArtistCredit2", |
| 170 |
], |
| 171 |
"albumartist_sort": "albumArtistSort", |
| 172 |
"albumartists_sort": ["albumArtistSort", "albumArtistSort2"], |
| 173 |
"albumtype": "album", |
| 174 |
"albumtypes": ["album"], |
| 175 |
"comp": True, |
| 176 |
"disctotal": 2, |
| 177 |
"mb_albumartistid": "a6623d39-2d8e-4f70-8242-0a9553b91e50", |
| 178 |
"mb_albumartistids": ["a6623d39-2d8e-4f70-8242-0a9553b91e50"], |
| 179 |
"mb_albumid": "7edb51cb-77d6-4416-a23c-3a8c2994a2c7", |
| 180 |
"mb_artistid": "a6623d39-2d8e-4f70-8242-0a9553b91e50", |
| 181 |
"mb_artistids": ["a6623d39-2d8e-4f70-8242-0a9553b91e50"], |
| 182 |
"arrangers": ["trackArranger1", "trackArranger2"], |
| 183 |
"composers": ["trackComposer1", "trackComposer2"], |
| 184 |
"remixers": ["trackRemixer1", "trackRemixer2"], |
| 185 |
"lyricists": ["trackLyricist1", "trackLyricist2"], |
| 186 |
"tracktotal": 2, |
| 187 |
"year": 2013, |
| 188 |
"month": 12, |
| 189 |
"day": 18, |
| 190 |
} |
| 191 |
|
| 192 |
self.expected_tracks = [ |
| 193 |
{ |
| 194 |
**common_expected, |
| 195 |
"artist": "trackArtist", |
| 196 |
"artists": ["trackArtist"], |
| 197 |
"artist_credit": "trackArtistCredit", |
| 198 |
"artist_sort": "sortLast, sortFirst and sortLast2, sortFirst2", |
| 199 |
"artists_credit": ["trackArtistCredit"], |
| 200 |
"artists_sort": [ |
| 201 |
"sortLast, sortFirst", |
| 202 |
"sortLast2, sortFirst2", |
| 203 |
], |
| 204 |
"disc": 1, |
| 205 |
"genres": ["Rock"], |
| 206 |
"mb_trackid": "dfa939ec-118c-4d0f-84a0-60f3d1e6522c", |
| 207 |
"title": "title", |
| 208 |
"track": 1, |
| 209 |
}, |
| 210 |
{ |
| 211 |
**common_expected, |
| 212 |
"artist": "albumArtist", |
| 213 |
"artists": ["albumArtist", "albumArtist2"], |
| 214 |
"artist_credit": "albumArtistCredit", |
| 215 |
"artist_sort": "albumArtistSort", |
| 216 |
"artists_credit": [ |
| 217 |
"albumArtistCredit", |
| 218 |
"albumArtistCredit1", |
| 219 |
"albumArtistCredit2", |
| 220 |
], |
| 221 |
"artists_sort": ["albumArtistSort", "albumArtistSort2"], |
| 222 |
"disc": 2, |
| 223 |
"genres": ["Rock", "Pop"], |
| 224 |
"mb_trackid": "40130ed1-a27c-42fd-a328-1ebefb6caef4", |
| 225 |
"title": "title2", |
| 226 |
"track": 2, |
| 227 |
}, |
| 228 |
] |
| 229 |
|
| 230 |
def test_autotag_items(self): |
| 231 |
self._apply() |
| 232 |
|
| 233 |
keys = self.expected_tracks[0].keys() |
| 234 |
get_values = operator.itemgetter(*keys) |
| 235 |
|
| 236 |
applied_data = [ |
| 237 |
dict(zip(keys, get_values(dict(i)))) for i in self.items |
| 238 |
] |
| 239 |
|
| 240 |
assert applied_data == self.expected_tracks |
| 241 |
|
| 242 |
def test_per_disc_numbering(self): |
| 243 |
self._apply(per_disc_numbering=True) |
| 244 |
|
| 245 |
assert self.items[0].track == 1 |
| 246 |
assert self.items[1].track == 1 |
| 247 |
assert self.items[0].tracktotal == 1 |
| 248 |
assert self.items[1].tracktotal == 1 |
| 249 |
|
| 250 |
def test_artist_credit_prefers_artist_over_albumartist_credit(self): |
| 251 |
self.info.tracks[0].update(artist="oldArtist", artist_credit=None) |
| 252 |
|
| 253 |
self._apply(artist_credit=True) |
| 254 |
|
| 255 |
assert self.items[0].artist == "oldArtist" |
| 256 |
|
| 257 |
def test_artist_credit_falls_back_to_albumartist(self): |
| 258 |
self.info.artist_credit = None |
| 259 |
|
| 260 |
self._apply(artist_credit=True) |
| 261 |
|
| 262 |
assert self.items[1].artist == "albumArtist" |
| 263 |
|
| 264 |
def test_date_only_zeroes_month_and_day(self): |
| 265 |
self.items = [Item(year=1, month=2, day=3)] |
| 266 |
self.info.update(year=2013, month=None, day=None) |
| 267 |
|
| 268 |
self._apply() |
| 269 |
|
| 270 |
assert self.items[0].year == 2013 |
| 271 |
assert self.items[0].month == 0 |
| 272 |
assert self.items[0].day == 0 |
| 273 |
|
| 274 |
def test_missing_date_applies_nothing(self): |
| 275 |
self.items = [Item(year=1, month=2, day=3)] |
| 276 |
self.info.update(year=None, month=None, day=None) |
| 277 |
|
| 278 |
self._apply() |
| 279 |
|
| 280 |
assert self.items[0].year == 1 |
| 281 |
assert self.items[0].month == 2 |
| 282 |
assert self.items[0].day == 3 |
| 283 |
|
| 284 |
def test_original_date_overrides_album_metadata(self): |
| 285 |
self.info.update( |
| 286 |
year=2013, |
| 287 |
month=12, |
| 288 |
day=18, |
| 289 |
original_year=1999, |
| 290 |
original_month=4, |
| 291 |
original_day=7, |
| 292 |
) |
| 293 |
match = self._apply(original_date=True) |
| 294 |
album = self.lib.add_album(self.items) |
| 295 |
match.apply_album_metadata(album) |
| 296 |
|
| 297 |
assert album.year == 1999 |
| 298 |
assert album.month == 4 |
| 299 |
assert album.day == 7 |
| 300 |
|
| 301 |
|
| 302 |
class TestFromScratch: |
| 303 |
@pytest.fixture(autouse=True) |
| 304 |
def config(self, config): |
| 305 |
config["import"]["from_scratch"] = True |
| 306 |
|
| 307 |
@pytest.fixture |
| 308 |
def album_info(self): |
| 309 |
return AlbumInfo( |
| 310 |
tracks=[TrackInfo(title="title", artist="track artist", index=1)] |
| 311 |
) |
| 312 |
|
| 313 |
@pytest.fixture |
| 314 |
def item(self): |
| 315 |
return Item(artist="old artist", comments="stale comment") |
| 316 |
|
| 317 |
def test_album_match_clears_stale_metadata(self, album_info, item): |
| 318 |
match = AlbumMatch(Distance(), album_info, {item: album_info.tracks[0]}) |
| 319 |
|
| 320 |
match.apply_metadata() |
| 321 |
|
| 322 |
assert item.artist == "track artist" |
| 323 |
assert item.comments == "" |
| 324 |
|
| 325 |
def test_singleton_match_clears_stale_metadata(self, item): |
| 326 |
match = TrackMatch(Distance(), TrackInfo(artist="track artist"), item) |
| 327 |
|
| 328 |
match.apply_metadata() |
| 329 |
|
| 330 |
assert item.artist == "track artist" |
| 331 |
assert item.comments == "" |
| 332 |
|
| 333 |
|
| 334 |
@pytest.mark.parametrize( |
| 335 |
"overwrite_fields,expected_item_artist", |
| 336 |
[ |
| 337 |
pytest.param(["artist"], "", id="overwrite artist"), |
| 338 |
pytest.param([], "artist", id="do not overwrite artist"), |
| 339 |
], |
| 340 |
) |
| 341 |
class TestOverwriteNull: |
| 342 |
@pytest.fixture(autouse=True) |
| 343 |
def config(self, config, overwrite_fields): |
| 344 |
config["overwrite_null"]["album"] = overwrite_fields |
| 345 |
config["overwrite_null"]["track"] = overwrite_fields |
| 346 |
config["import"]["from_scratch"] = False |
| 347 |
|
| 348 |
@pytest.fixture |
| 349 |
def item(self): |
| 350 |
return Item(artist="artist") |
| 351 |
|
| 352 |
@pytest.fixture |
| 353 |
def track_info(self): |
| 354 |
return TrackInfo(artist=None) |
| 355 |
|
| 356 |
def test_album(self, item, track_info, expected_item_artist): |
| 357 |
match = AlbumMatch( |
| 358 |
Distance(), AlbumInfo([track_info]), {item: track_info} |
| 359 |
) |
| 360 |
|
| 361 |
match.apply_metadata() |
| 362 |
|
| 363 |
assert item.artist == expected_item_artist |
| 364 |
|
| 365 |
def test_singleton(self, item, track_info, expected_item_artist): |
| 366 |
match = TrackMatch(Distance(), track_info, item) |
| 367 |
|
| 368 |
match.apply_metadata() |
| 369 |
|
| 370 |
assert item.artist == expected_item_artist |
| 371 |
|
| 372 |
|
| 373 |
@pytest.mark.parametrize( |
| 374 |
"single_field,list_field", |
| 375 |
[ |
| 376 |
("albumtype", "albumtypes"), |
| 377 |
("artist", "artists"), |
| 378 |
("artist_credit", "artists_credit"), |
| 379 |
("artist_id", "artists_ids"), |
| 380 |
("artist_sort", "artists_sort"), |
| 381 |
], |
| 382 |
) |
| 383 |
@pytest.mark.parametrize( |
| 384 |
"single_value,list_value,expected_values", |
| 385 |
[ |
| 386 |
(None, [], (None, [])), |
| 387 |
(None, ["1"], ("1", ["1"])), |
| 388 |
(None, ["1", "2"], ("1", ["1", "2"])), |
| 389 |
("1", [], ("1", ["1"])), |
| 390 |
("1", ["1"], ("1", ["1"])), |
| 391 |
("1", ["1", "2"], ("1", ["1", "2"])), |
| 392 |
("1", ["2", "1"], ("1", ["1", "2"])), |
| 393 |
("1", ["2"], ("1", ["1", "2"])), |
| 394 |
("1 ft 2", ["1", "1 ft 2"], ("1 ft 2", ["1 ft 2", "1"])), |
| 395 |
("1 FT 2", ["1", "1 ft 2"], ("1 FT 2", ["1", "1 ft 2"])), |
| 396 |
("a", ["b", "A"], ("a", ["b", "A"])), |
| 397 |
("1 ft 2", ["2", "1"], ("1 ft 2", ["2", "1"])), |
| 398 |
( |
| 399 |
"a, b and c, d", |
| 400 |
["a, b", "c, d"], |
| 401 |
("a, b and c, d", ["a, b", "c, d"]), |
| 402 |
), |
| 403 |
], |
| 404 |
) |
| 405 |
def test_correct_list_fields( |
| 406 |
single_field, list_field, single_value, list_value, expected_values |
| 407 |
): |
| 408 |
"""Verify that singular and plural field variants are kept consistent. |
| 409 |
|
| 410 |
Checks that when both a single-value field and its list counterpart are |
| 411 |
present, the function reconciles them: ensuring the single value appears |
| 412 |
in the list and the list drives the canonical single value when needed. |
| 413 |
""" |
| 414 |
input_data = {single_field: single_value, list_field: list_value} |
| 415 |
|
| 416 |
data = correct_list_fields(input_data) |
| 417 |
|
| 418 |
assert (data[single_field], data[list_field]) == expected_values |
| 419 |
|
| 420 |
|
| 421 |
class TestAttrDictCachedPropertyMasking: |
| 422 |
"""Regression tests for #6558. |
| 423 |
|
| 424 |
When a ``cached_property`` on an :class:`AttrDict` subclass raises |
| 425 |
``AttributeError`` during its own computation, Python's default |
| 426 |
attribute lookup falls back to ``__getattr__`` with the property's |
| 427 |
name and the real missing field gets masked behind a misleading |
| 428 |
"object has no attribute 'raw_data'". We intercept cached_property |
| 429 |
lookups in ``__getattribute__``: when ``super().__getattribute__`` |
| 430 |
raises ``AttributeError`` and the attribute resolves to a |
| 431 |
``cached_property``, the error is re-raised as ``RuntimeError`` that |
| 432 |
keeps the original traceback so it still points at the real failing |
| 433 |
line inside the property body. ``raise ... from None`` suppresses |
| 434 |
the chained-exception block in CLI tracebacks. |
| 435 |
""" |
| 436 |
|
| 437 |
def test_inner_error_surfaces_with_original_message(self): |
| 438 |
class Sample(AttrDict[object]): |
| 439 |
@cached_property |
| 440 |
def derived(self): |
| 441 |
return self.missing_field |
| 442 |
|
| 443 |
obj = Sample() |
| 444 |
with pytest.raises(RuntimeError, match="missing_field"): |
| 445 |
obj.derived |
| 446 |
|
| 447 |
def test_cause_chain_is_suppressed(self): |
| 448 |
class Sample(AttrDict[object]): |
| 449 |
@cached_property |
| 450 |
def derived(self): |
| 451 |
return self.missing_field |
| 452 |
|
| 453 |
obj = Sample() |
| 454 |
with pytest.raises(RuntimeError) as excinfo: |
| 455 |
obj.derived |
| 456 |
# ``raise ... from None`` suppresses the chained traceback block. |
| 457 |
assert excinfo.value.__cause__ is None |
| 458 |
assert excinfo.value.__suppress_context__ is True |
| 459 |
|
| 460 |
def test_traceback_points_at_property_body(self): |
| 461 |
class Sample(AttrDict[object]): |
| 462 |
@cached_property |
| 463 |
def derived(self): |
| 464 |
return self.missing_field |
| 465 |
|
| 466 |
obj = Sample() |
| 467 |
with pytest.raises(RuntimeError) as excinfo: |
| 468 |
obj.derived |
| 469 |
# The wrapper reuses the original AttributeError traceback, so the |
| 470 |
# RuntimeError itself points back at the line that actually failed. |
| 471 |
tb = excinfo.value.__traceback__ |
| 472 |
frames = [] |
| 473 |
while tb is not None: |
| 474 |
frames.append(tb.tb_frame.f_code.co_name) |
| 475 |
tb = tb.tb_next |
| 476 |
assert "derived" in frames |
| 477 |
|
| 478 |
def test_cached_property_success_returns_value(self): |
| 479 |
class Sample(AttrDict[object]): |
| 480 |
@cached_property |
| 481 |
def doubled(self): |
| 482 |
return self["n"] * 2 |
| 483 |
|
| 484 |
obj = Sample({"n": 21}) |
| 485 |
assert obj.doubled == 42 |
| 486 |
|
| 487 |
def test_plain_missing_attr_still_raises_attributeerror(self): |
| 488 |
obj = AttrDict[str]() |
| 489 |
with pytest.raises(AttributeError, match="nope"): |
| 490 |
obj.nope |
| 491 |
|
| 492 |
def test_existing_dict_key_returns_value(self): |
| 493 |
obj = AttrDict[str]({"title": "ok"}) |
| 494 |
assert obj.title == "ok" |