| 1 |
import re |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from beets.autotag import ( |
| 6 |
AlbumInfo, |
| 7 |
Distance, |
| 8 |
Source, |
| 9 |
TrackInfo, |
| 10 |
distance, |
| 11 |
string_dist, |
| 12 |
track_distance, |
| 13 |
) |
| 14 |
from beets.library import Item |
| 15 |
from beets.metadata_plugins import MetadataSourcePlugin, get_penalty |
| 16 |
from beets.plugins import BeetsPlugin |
| 17 |
|
| 18 |
_p = pytest.param |
| 19 |
|
| 20 |
|
| 21 |
class TestDistance: |
| 22 |
@pytest.fixture(autouse=True) |
| 23 |
def setup_config(self, config): |
| 24 |
config["match"]["distance_weights"]["data_source"] = 2.0 |
| 25 |
config["match"]["distance_weights"]["album"] = 4.0 |
| 26 |
config["match"]["distance_weights"]["medium"] = 2.0 |
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def dist(self): |
| 30 |
return Distance() |
| 31 |
|
| 32 |
def test_add(self, dist): |
| 33 |
dist.add("add", 1.0) |
| 34 |
|
| 35 |
assert dist._penalties == {"add": [1.0]} |
| 36 |
|
| 37 |
@pytest.mark.parametrize( |
| 38 |
"key, args_with_expected", |
| 39 |
[ |
| 40 |
( |
| 41 |
"equality", |
| 42 |
[ |
| 43 |
(("ghi", ["abc", "def", "ghi"]), [0.0]), |
| 44 |
(("xyz", ["abc", "def", "ghi"]), [0.0, 1.0]), |
| 45 |
(("abc", re.compile(r"ABC", re.I)), [0.0, 1.0, 0.0]), |
| 46 |
], |
| 47 |
), |
| 48 |
("expr", [((True,), [1.0]), ((False,), [1.0, 0.0])]), |
| 49 |
( |
| 50 |
"number", |
| 51 |
[ |
| 52 |
((1, 1), [0.0]), |
| 53 |
((1, 2), [0.0, 1.0]), |
| 54 |
((2, 1), [0.0, 1.0, 1.0]), |
| 55 |
((-1, 2), [0.0, 1.0, 1.0, 1.0, 1.0, 1.0]), |
| 56 |
], |
| 57 |
), |
| 58 |
( |
| 59 |
"priority", |
| 60 |
[ |
| 61 |
(("abc", "abc"), [0.0]), |
| 62 |
(("def", ["abc", "def"]), [0.0, 0.5]), |
| 63 |
(("gh", ["ab", "cd", "ef", re.compile("GH", re.I)]), [0.0, 0.5, 0.75]), # noqa: E501 |
| 64 |
(("xyz", ["abc", "def"]), [0.0, 0.5, 0.75, 1.0]), |
| 65 |
], |
| 66 |
), |
| 67 |
( |
| 68 |
"ratio", |
| 69 |
[ |
| 70 |
((25, 100), [0.25]), |
| 71 |
((10, 5), [0.25, 1.0]), |
| 72 |
((-5, 5), [0.25, 1.0, 0.0]), |
| 73 |
((5, 0), [0.25, 1.0, 0.0, 0.0]), |
| 74 |
], |
| 75 |
), |
| 76 |
( |
| 77 |
"string", |
| 78 |
[ |
| 79 |
(("abc", "bcd"), [2 / 3]), |
| 80 |
(("abc", None), [2 / 3, 1]), |
| 81 |
((None, None), [2 / 3, 1, 0]), |
| 82 |
], |
| 83 |
), |
| 84 |
], |
| 85 |
) # fmt: skip |
| 86 |
def test_add_methods(self, dist, key, args_with_expected): |
| 87 |
method = getattr(dist, f"add_{key}") |
| 88 |
for arg_set, expected in args_with_expected: |
| 89 |
method(key, *arg_set) |
| 90 |
assert dist._penalties[key] == expected |
| 91 |
|
| 92 |
def test_distance(self, dist): |
| 93 |
dist.add("album", 0.5) |
| 94 |
dist.add("media", 0.25) |
| 95 |
dist.add("media", 0.75) |
| 96 |
|
| 97 |
assert dist.distance == 0.5 |
| 98 |
assert dist.max_distance == 6.0 |
| 99 |
assert dist.raw_distance == 3.0 |
| 100 |
|
| 101 |
assert dist["album"] == 1 / 3 |
| 102 |
assert dist["media"] == 1 / 6 |
| 103 |
|
| 104 |
def test_operators(self, dist): |
| 105 |
dist.add("data_source", 0.0) |
| 106 |
dist.add("album", 0.5) |
| 107 |
dist.add("medium", 0.25) |
| 108 |
dist.add("medium", 0.75) |
| 109 |
assert len(dist) == 2 |
| 110 |
assert list(dist) == [("album", 0.2), ("medium", 0.2)] |
| 111 |
assert dist == 0.4 |
| 112 |
assert dist < 1.0 |
| 113 |
assert dist > 0.0 |
| 114 |
assert dist - 0.4 == 0.0 |
| 115 |
assert 0.4 - dist == 0.0 |
| 116 |
assert float(dist) == 0.4 |
| 117 |
|
| 118 |
def test_penalties_sort(self, dist): |
| 119 |
dist.add("album", 0.1875) |
| 120 |
dist.add("medium", 0.75) |
| 121 |
assert dist.items() == [("medium", 0.25), ("album", 0.125)] |
| 122 |
|
| 123 |
# Sort by key if distance is equal. |
| 124 |
dist = Distance() |
| 125 |
dist.add("album", 0.375) |
| 126 |
dist.add("medium", 0.75) |
| 127 |
assert dist.items() == [("album", 0.25), ("medium", 0.25)] |
| 128 |
|
| 129 |
def test_generic_penalty_keys_excludes_zero_penalties(self, dist): |
| 130 |
dist.add("album", 0.5) |
| 131 |
dist.add("label", 0.0) |
| 132 |
dist.add("medium", 0.25) |
| 133 |
assert dist.generic_penalty_keys == ["album", "medium"] |
| 134 |
|
| 135 |
def test_update(self, dist): |
| 136 |
dist1 = dist |
| 137 |
dist1.add("album", 0.5) |
| 138 |
dist1.add("media", 1.0) |
| 139 |
|
| 140 |
dist2 = Distance() |
| 141 |
dist2.add("album", 0.75) |
| 142 |
dist2.add("album", 0.25) |
| 143 |
dist2.add("media", 0.05) |
| 144 |
|
| 145 |
dist1.update(dist2) |
| 146 |
|
| 147 |
assert dist1._penalties == { |
| 148 |
"album": [0.5, 0.75, 0.25], |
| 149 |
"media": [1.0, 0.05], |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
class TestTrackDistance: |
| 154 |
@pytest.fixture(scope="class") |
| 155 |
def info(self): |
| 156 |
return TrackInfo(title="title", artist="artist") |
| 157 |
|
| 158 |
@pytest.mark.parametrize( |
| 159 |
"title, artist, expected_penalty", |
| 160 |
[ |
| 161 |
_p("title", "artist", False, id="identical"), |
| 162 |
_p("title", "Various Artists", False, id="tolerate-va"), |
| 163 |
_p("title", "different artist", True, id="different-artist"), |
| 164 |
_p("different title", "artist", True, id="different-title"), |
| 165 |
], |
| 166 |
) |
| 167 |
def test_track_distance(self, info, title, artist, expected_penalty): |
| 168 |
item = Item(artist=artist, title=title) |
| 169 |
|
| 170 |
dist = track_distance(item, info, incl_artist=True) |
| 171 |
assert bool(dist) == expected_penalty, dist._penalties |
| 172 |
|
| 173 |
|
| 174 |
class TestAlbumDistance: |
| 175 |
@pytest.fixture(scope="class") |
| 176 |
def items(self): |
| 177 |
return [ |
| 178 |
Item( |
| 179 |
title=title, |
| 180 |
track=track, |
| 181 |
artist="artist", |
| 182 |
album="album", |
| 183 |
length=1, |
| 184 |
) |
| 185 |
for title, track in [("one", 1), ("two", 2), ("three", 3)] |
| 186 |
] |
| 187 |
|
| 188 |
@pytest.fixture |
| 189 |
def get_dist(self, items): |
| 190 |
def inner(info: AlbumInfo): |
| 191 |
return distance( |
| 192 |
Source.from_items(items).data, |
| 193 |
info, |
| 194 |
list(zip(items, info.tracks)), |
| 195 |
len(items) - len(info.tracks), |
| 196 |
) |
| 197 |
|
| 198 |
return inner |
| 199 |
|
| 200 |
@pytest.fixture |
| 201 |
def info(self, items): |
| 202 |
return AlbumInfo( |
| 203 |
artist="artist", |
| 204 |
album="album", |
| 205 |
tracks=[ |
| 206 |
TrackInfo( |
| 207 |
title=i.title, |
| 208 |
artist=i.artist, |
| 209 |
index=i.track, |
| 210 |
length=i.length, |
| 211 |
) |
| 212 |
for i in items |
| 213 |
], |
| 214 |
va=False, |
| 215 |
) |
| 216 |
|
| 217 |
def test_identical_albums(self, get_dist, info): |
| 218 |
assert get_dist(info) == 0 |
| 219 |
|
| 220 |
def test_incomplete_album(self, get_dist, info): |
| 221 |
info.tracks.pop(2) |
| 222 |
|
| 223 |
assert 0 < float(get_dist(info)) < 0.2 |
| 224 |
|
| 225 |
def test_overly_complete_album(self, get_dist, info): |
| 226 |
info.tracks.append( |
| 227 |
Item(index=4, title="four", artist="artist", length=1) |
| 228 |
) |
| 229 |
|
| 230 |
assert 0 < float(get_dist(info)) < 0.2 |
| 231 |
|
| 232 |
@pytest.mark.parametrize("va", [True, False]) |
| 233 |
def test_albumartist(self, get_dist, info, va): |
| 234 |
info.artist = "another artist" |
| 235 |
info.va = va |
| 236 |
|
| 237 |
assert bool(get_dist(info)) is not va |
| 238 |
|
| 239 |
def test_comp_no_track_artists(self, get_dist, info): |
| 240 |
# Some VA releases don't have track artists (incomplete metadata). |
| 241 |
info.artist = "another artist" |
| 242 |
info.va = True |
| 243 |
for track in info.tracks: |
| 244 |
track.artist = None |
| 245 |
|
| 246 |
assert get_dist(info) == 0 |
| 247 |
|
| 248 |
def test_comp_track_artists_do_not_match(self, get_dist, info): |
| 249 |
info.va = True |
| 250 |
info.tracks[0].artist = "another artist" |
| 251 |
|
| 252 |
assert get_dist(info) != 0 |
| 253 |
|
| 254 |
def test_tracks_out_of_order(self, get_dist, info): |
| 255 |
tracks = info.tracks |
| 256 |
tracks[1].title, tracks[2].title = tracks[2].title, tracks[1].title |
| 257 |
|
| 258 |
assert 0 < float(get_dist(info)) < 0.2 |
| 259 |
|
| 260 |
def test_two_medium_release(self, get_dist, info): |
| 261 |
info.tracks[0].medium_index = 1 |
| 262 |
info.tracks[1].medium_index = 2 |
| 263 |
info.tracks[2].medium_index = 1 |
| 264 |
|
| 265 |
assert get_dist(info) == 0 |
| 266 |
|
| 267 |
|
| 268 |
class TestStringDistance: |
| 269 |
@pytest.mark.parametrize( |
| 270 |
"string1, string2", |
| 271 |
[ |
| 272 |
("Some String", "Some String"), |
| 273 |
("Some String", "Some.String!"), |
| 274 |
("Some String", "sOME sTring"), |
| 275 |
("My Song (EP)", "My Song"), |
| 276 |
("The Song Title", "Song Title, The"), |
| 277 |
("A Song Title", "Song Title, A"), |
| 278 |
("An Album Title", "Album Title, An"), |
| 279 |
("", ""), |
| 280 |
("Untitled", "[Untitled]"), |
| 281 |
("And", "&"), |
| 282 |
("\xe9\xe1\xf1", "ean"), |
| 283 |
], |
| 284 |
) |
| 285 |
def test_matching_distance(self, string1, string2): |
| 286 |
assert string_dist(string1, string2) == 0.0 |
| 287 |
|
| 288 |
def test_different_distance(self): |
| 289 |
assert string_dist("Some String", "Totally Different") != 0.0 |
| 290 |
|
| 291 |
@pytest.mark.parametrize( |
| 292 |
"string1, string2", |
| 293 |
[ |
| 294 |
("Draft Beer", "Draft Whiskey"), |
| 295 |
("Left Field", "Left Symphony"), |
| 296 |
("Gift Ideas", "Gift Cards"), |
| 297 |
("Craft Beer", "Craft Wine"), |
| 298 |
], |
| 299 |
) |
| 300 |
def test_featuring_pattern_does_not_match_mid_word(self, string1, string2): |
| 301 |
# The "featuring"/"feat"/"ft" pattern must not match "ft" embedded |
| 302 |
# inside an ordinary word (draft, left, gift, craft, ...) -- doing so |
| 303 |
# would treat everything after "ft" as a low-weight suffix and make |
| 304 |
# genuinely different strings look almost identical instead of |
| 305 |
# correctly registering as a large distance. |
| 306 |
assert string_dist(string1, string2) > 0.3 |
| 307 |
|
| 308 |
@pytest.mark.parametrize( |
| 309 |
"string1, string2, reference", |
| 310 |
[ |
| 311 |
("XXX Band Name", "The Band Name", "Band Name"), |
| 312 |
("One .Two.", "One (Two)", "One"), |
| 313 |
("One .Two.", "One [Two]", "One"), |
| 314 |
("My Song blah Someone", "My Song feat Someone", "My Song"), |
| 315 |
], |
| 316 |
) |
| 317 |
def test_relative_weights(self, string1, string2, reference): |
| 318 |
assert string_dist(string2, reference) < string_dist(string1, reference) |
| 319 |
|
| 320 |
def test_solo_pattern(self): |
| 321 |
# Just make sure these don't crash. |
| 322 |
string_dist("The ", "") |
| 323 |
string_dist("(EP)", "(EP)") |
| 324 |
string_dist(", An", "") |
| 325 |
|
| 326 |
|
| 327 |
class TestDataSourceDistance: |
| 328 |
MATCH = 0.0 |
| 329 |
MISMATCH = 0.125 |
| 330 |
|
| 331 |
@pytest.fixture(autouse=True) |
| 332 |
def setup(self, monkeypatch, penalty, weight, multiple_data_sources): |
| 333 |
monkeypatch.setitem(Distance._weights, "data_source", weight) |
| 334 |
get_penalty.cache_clear() |
| 335 |
|
| 336 |
class TestMetadataSourcePlugin(MetadataSourcePlugin): |
| 337 |
def album_for_id(self, *args, **kwargs): ... |
| 338 |
def track_for_id(self, *args, **kwargs): ... |
| 339 |
def candidates(self, *args, **kwargs): ... |
| 340 |
def item_candidates(self, *args, **kwargs): ... |
| 341 |
|
| 342 |
# We use BeetsPlugin here to check if our compatibility layer |
| 343 |
# for pre 2.4.0 MetadataPlugins is working as expected |
| 344 |
# TODO: Replace BeetsPlugin with TestMetadataSourcePlugin in v3.0.0 |
| 345 |
with pytest.deprecated_call(): |
| 346 |
|
| 347 |
class OriginalPlugin(BeetsPlugin): |
| 348 |
data_source = "Original" |
| 349 |
|
| 350 |
class OtherPlugin(TestMetadataSourcePlugin): |
| 351 |
@property |
| 352 |
def data_source_mismatch_penalty(self): |
| 353 |
return penalty |
| 354 |
|
| 355 |
monkeypatch.setattr( |
| 356 |
"beets.metadata_plugins.find_metadata_source_plugins", |
| 357 |
lambda: ( |
| 358 |
[OriginalPlugin(), OtherPlugin()] |
| 359 |
if multiple_data_sources |
| 360 |
else [OtherPlugin()] |
| 361 |
), |
| 362 |
) |
| 363 |
|
| 364 |
@pytest.mark.parametrize( |
| 365 |
"item,info,penalty,weight,multiple_data_sources,expected_distance", |
| 366 |
[ |
| 367 |
_p("Original", "Original", 0.5, 1.0, True, MATCH, id="match"), |
| 368 |
_p("Original", "Other", 0.5, 1.0, True, MISMATCH, id="mismatch"), |
| 369 |
_p("Other", "Original", 0.5, 1.0, True, MISMATCH, id="mismatch"), |
| 370 |
_p("Original", "unknown", 0.5, 1.0, True, MISMATCH, id="mismatch-unknown"), |
| 371 |
_p("Original", None, 0.5, 1.0, True, MISMATCH, id="mismatch-no-info"), |
| 372 |
_p(None, "Other", 0.5, 1.0, True, MISMATCH, id="mismatch-no-original-multiple-sources"), # noqa: E501 |
| 373 |
_p(None, "Other", 0.5, 1.0, False, MATCH, id="match-no-original-but-single-source"), # noqa: E501 |
| 374 |
_p("unknown", "unknown", 0.5, 1.0, True, MATCH, id="match-unknown"), |
| 375 |
_p("Original", "Other", 1.0, 1.0, True, 0.25, id="mismatch-max-penalty"), |
| 376 |
_p("Original", "Other", 0.5, 5.0, True, 0.3125, id="mismatch-high-weight"), |
| 377 |
_p("Original", "Other", 0.0, 1.0, True, MATCH, id="match-no-penalty"), |
| 378 |
_p("Original", "Other", 0.5, 0.0, True, MATCH, id="match-no-weight"), |
| 379 |
], |
| 380 |
) # fmt: skip |
| 381 |
def test_distance(self, item, info, expected_distance): |
| 382 |
item = Item(data_source=item) |
| 383 |
info = TrackInfo(data_source=info, title="") |
| 384 |
|
| 385 |
dist = track_distance(item, info) |
| 386 |
|
| 387 |
assert dist.distance == expected_distance |