Debian Codegraph @

test/plugins/test_ftintitle.py

raw
1 """Tests for the 'ftintitle' plugin."""
2
3 from __future__ import annotations
4
5 from typing import cast
6
7 import pytest
8
9 from beets import plugins
10 from beets.autotag import AlbumInfo, TrackInfo
11 from beets.importer import ImportSession, SingletonImportTask
12 from beets.library import Album
13 from beets.test.helper import PluginTestHelper
14 from beetsplug import ftintitle
15
16
17 class TestFtInTitlePluginFunctional(PluginTestHelper):
18 plugin = "ftintitle"
19 preload_plugin = False
20
21 @pytest.mark.parametrize(
22 "cfg, cmd_args, given, expected",
23 [
24 pytest.param(
25 {},
26 ("ftintitle",),
27 ("Alice", "Song 1", "Alice"),
28 ("Alice", "Song 1"),
29 id="no-featured-artist",
30 ),
31 pytest.param(
32 {"format": "feat {0}"},
33 ("ftintitle",),
34 ("Alice ft. Bob", "Song 1", None),
35 ("Alice", "Song 1 feat Bob"),
36 id="no-albumartist-custom-format",
37 ),
38 pytest.param(
39 {},
40 ("ftintitle",),
41 ("Alice", "Song 1", None),
42 ("Alice", "Song 1"),
43 id="no-albumartist-no-feature",
44 ),
45 pytest.param(
46 {"format": "featuring {0}"},
47 ("ftintitle",),
48 ("Alice ft Bob", "Song 1", "George"),
49 ("Alice", "Song 1 featuring Bob"),
50 id="guest-artist-custom-format",
51 ),
52 pytest.param(
53 {},
54 ("ftintitle",),
55 ("Alice", "Song 1", "George"),
56 ("Alice", "Song 1"),
57 id="guest-artist-no-feature",
58 ),
59 # ---- drop (-d) variants ----
60 pytest.param(
61 {},
62 ("ftintitle", "-d"),
63 ("Alice ft Bob", "Song 1", "Alice"),
64 ("Alice", "Song 1"),
65 id="drop-self-ft",
66 ),
67 pytest.param(
68 {},
69 ("ftintitle", "-d"),
70 ("Alice", "Song 1", "Alice"),
71 ("Alice", "Song 1"),
72 id="drop-self-no-ft",
73 ),
74 pytest.param(
75 {},
76 ("ftintitle", "-d"),
77 ("Alice ft Bob", "Song 1", "George"),
78 ("Alice", "Song 1"),
79 id="drop-guest-ft",
80 ),
81 pytest.param(
82 {},
83 ("ftintitle", "-d"),
84 ("Alice", "Song 1", "George"),
85 ("Alice", "Song 1"),
86 id="drop-guest-no-ft",
87 ),
88 # ---- custom format variants ----
89 pytest.param(
90 {"format": "feat. {}"},
91 ("ftintitle",),
92 ("Alice ft Bob", "Song 1", "Alice"),
93 ("Alice", "Song 1 feat. Bob"),
94 id="custom-format-feat-dot",
95 ),
96 pytest.param(
97 {"format": "featuring {}"},
98 ("ftintitle",),
99 ("Alice feat. Bob", "Song 1", "Alice"),
100 ("Alice", "Song 1 featuring Bob"),
101 id="custom-format-featuring",
102 ),
103 pytest.param(
104 {"format": "with {}"},
105 ("ftintitle",),
106 ("Alice feat Bob", "Song 1", "Alice"),
107 ("Alice", "Song 1 with Bob"),
108 id="custom-format-with",
109 ),
110 pytest.param(
111 {"format": "ft. {}"},
112 ("ftintitle",),
113 ("Alice (ft. Bob)", "Song 1", "Alice"),
114 ("Alice", "Song 1 ft. Bob"),
115 id="featured-artist-in-parentheses",
116 ),
117 # ---- keep_in_artist variants ----
118 pytest.param(
119 {"format": "feat. {}", "keep_in_artist": True},
120 ("ftintitle",),
121 ("Alice ft Bob", "Song 1", "Alice"),
122 ("Alice ft Bob", "Song 1 feat. Bob"),
123 id="keep-in-artist-add-to-title",
124 ),
125 pytest.param(
126 {"format": "feat. {}", "keep_in_artist": True},
127 ("ftintitle", "-d"),
128 ("Alice ft Bob", "Song 1", "Alice"),
129 ("Alice ft Bob", "Song 1"),
130 id="keep-in-artist-drop-from-title",
131 ),
132 # ---- custom_words variants ----
133 pytest.param(
134 {"format": "featuring {}", "custom_words": ["med"]},
135 ("ftintitle",),
136 ("Alice med Bob", "Song 1", "Alice"),
137 ("Alice", "Song 1 featuring Bob"),
138 id="custom-feat-words",
139 ),
140 pytest.param(
141 {
142 "format": "featuring {}",
143 "keep_in_artist": True,
144 "custom_words": ["med"],
145 },
146 ("ftintitle",),
147 ("Alice med Bob", "Song 1", "Alice"),
148 ("Alice med Bob", "Song 1 featuring Bob"),
149 id="custom-feat-words-keep-in-artists",
150 ),
151 pytest.param(
152 {
153 "format": "featuring {}",
154 "keep_in_artist": True,
155 "custom_words": ["med"],
156 },
157 ("ftintitle", "-d"),
158 ("Alice med Bob", "Song 1", "Alice"),
159 ("Alice med Bob", "Song 1"),
160 id="custom-feat-words-keep-in-artists-drop-from-title",
161 ),
162 # ---- preserve_album_artist variants ----
163 pytest.param(
164 {"format": "feat. {}", "preserve_album_artist": True},
165 ("ftintitle",),
166 ("Alice feat. Bob", "Song 1", "Alice"),
167 ("Alice", "Song 1 feat. Bob"),
168 id="skip-if-artist-and-album-artists-is-the-same-different-match",
169 ),
170 pytest.param(
171 {"format": "feat. {}", "preserve_album_artist": False},
172 ("ftintitle",),
173 ("Alice feat. Bob", "Song 1", "Alice"),
174 ("Alice", "Song 1 feat. Bob"),
175 id="skip-if-artist-and-album-artists-is-the-same-different-match-b",
176 ),
177 pytest.param(
178 {"format": "feat. {}", "preserve_album_artist": True},
179 ("ftintitle",),
180 ("Alice feat. Bob", "Song 1", "Alice feat. Bob"),
181 ("Alice feat. Bob", "Song 1"),
182 id="skip-if-artist-and-album-artists-is-the-same-matching-match",
183 ),
184 pytest.param(
185 {"format": "feat. {}", "preserve_album_artist": False},
186 ("ftintitle",),
187 ("Alice feat. Bob", "Song 1", "Alice feat. Bob"),
188 ("Alice", "Song 1 feat. Bob"),
189 id="skip-if-artist-and-album-artists-is-the-same-matching-match-b",
190 ),
191 # ---- titles with brackets/parentheses ----
192 pytest.param(
193 {"format": "ft. {}", "bracket_keywords": ["mix"]},
194 ("ftintitle",),
195 ("Alice ft. Bob", "Song 1 (Club Mix)", "Alice"),
196 ("Alice", "Song 1 ft. Bob (Club Mix)"),
197 id="ft-inserted-before-matching-bracket-keyword",
198 ),
199 pytest.param(
200 {"format": "ft. {}", "bracket_keywords": ["nomatch"]},
201 ("ftintitle",),
202 ("Alice ft. Bob", "Song 1 (Club Remix)", "Alice"),
203 ("Alice", "Song 1 (Club Remix) ft. Bob"),
204 id="ft-inserted-at-end-no-bracket-keyword-match",
205 ),
206 ],
207 )
208 def test_ftintitle_functional(
209 self,
210 cfg: dict[str, str | bool | list[str]],
211 cmd_args: tuple[str, ...],
212 given: tuple[str, str, str | None],
213 expected: tuple[str, str],
214 ) -> None:
215 config = {
216 "drop": False,
217 "auto": True,
218 "keep_in_artist": False,
219 "custom_words": [],
220 **cfg,
221 }
222
223 artist, title, albumartist = given
224 item = self.add_item(
225 path="/",
226 artist=artist,
227 artist_sort=artist,
228 title=title,
229 albumartist=albumartist,
230 )
231
232 with self.configure_plugin(config):
233 self.run_command(*cmd_args)
234
235 item.load()
236
237 expected_artist, expected_title = expected
238 assert item["artist"] == expected_artist
239 assert item["title"] == expected_title
240
241 def test_trackinfo_received_rewrites_artist_when_artist_credit_enabled(
242 self,
243 ) -> None:
244 self.config["artist_credit"] = True
245 info = TrackInfo(
246 artist="Alice feat. Bob",
247 artist_credit="Alice",
248 artist_sort="Alice feat. Bob",
249 artists=["Alice", "Bob"],
250 artists_credit=["Alice"],
251 title="Song",
252 )
253
254 with self.configure_plugin({"auto": True}):
255 plugins.send("trackinfo_received", info=info)
256
257 assert info.artist == "Alice"
258 assert info.artist_credit == "Alice"
259 assert info.artist_sort == "Alice"
260 assert info.artists == ["Alice", "Bob"]
261 assert info.artists_credit == ["Alice"]
262 assert info.title == "Song feat. Bob"
263 assert info.item_data["title"] == "Song feat. Bob"
264
265 @pytest.mark.parametrize(
266 "config, artist, title, expected_artist, expected_title",
267 [
268 pytest.param(
269 {"auto": False},
270 "Alice feat. Bob",
271 "Song",
272 "Alice feat. Bob",
273 "Song",
274 id="auto-disabled",
275 ),
276 pytest.param(
277 {"auto": True},
278 "Alice",
279 "Song",
280 "Alice",
281 "Song",
282 id="no-feature",
283 ),
284 pytest.param(
285 {"auto": True},
286 "Alice feat. Bob",
287 "Song feat. Bob",
288 "Alice",
289 "Song feat. Bob",
290 id="existing-title-feature",
291 ),
292 ],
293 )
294 def test_trackinfo_received_rewrites_or_skips_simple_cases(
295 self,
296 config: dict[str, bool],
297 artist: str,
298 title: str,
299 expected_artist: str,
300 expected_title: str,
301 ) -> None:
302 info = TrackInfo(artist=artist, title=title)
303
304 with self.configure_plugin(config):
305 plugins.send("trackinfo_received", info=info)
306
307 assert info.artist == expected_artist
308 assert info.title == expected_title
309
310 def test_ft_in_title_reports_no_change_for_info_drop_keep_noop(
311 self,
312 ) -> None:
313 info = TrackInfo(artist="Alice feat. Bob", title="Song")
314
315 with self.configure_plugin({"drop": True, "keep_in_artist": True}):
316 plugin = next(iter(plugins.find_plugins()))
317 assert isinstance(plugin, ftintitle.FtInTitlePlugin)
318 assert plugin.ft_in_title(info, "") is False
319
320 assert info.artist == "Alice feat. Bob"
321 assert info.title == "Song"
322
323 def test_trackinfo_received_preserves_artist_credit_when_disabled(
324 self,
325 ) -> None:
326 self.config["artist_credit"] = False
327 info = TrackInfo(
328 artist="Alice feat. Bob",
329 artist_credit="Alice feat. Bobby",
330 title="Song",
331 )
332
333 with self.configure_plugin({"auto": True}):
334 plugins.send("trackinfo_received", info=info)
335
336 assert info.artist == "Alice"
337 assert info.artist_credit == "Alice feat. Bobby"
338 assert info.title == "Song feat. Bob"
339 assert info.item_data["artist_credit"] == "Alice feat. Bobby"
340
341 def test_ft_in_title_reports_no_change_for_item_drop_keep_noop(
342 self,
343 ) -> None:
344 item = self.add_item(
345 path="/",
346 artist="Alice feat. Bob",
347 artist_sort="",
348 title="Song",
349 albumartist="Alice",
350 )
351
352 with self.configure_plugin({"drop": True, "keep_in_artist": True}):
353 plugin = next(iter(plugins.find_plugins()))
354 assert isinstance(plugin, ftintitle.FtInTitlePlugin)
355 assert (
356 plugin.ft_in_title(item, item.get("albumartist") or "") is False
357 )
358
359 assert item.artist == "Alice feat. Bob"
360 assert item.title == "Song"
361
362 def test_imported_stage_rewrites_imported_items(self) -> None:
363 item = self.add_item(
364 path="/",
365 artist="Alice feat. Bob",
366 title="Song",
367 albumartist="Alice",
368 )
369
370 with self.configure_plugin({"auto": True}):
371 plugin = next(iter(plugins.find_plugins()))
372 assert isinstance(plugin, ftintitle.FtInTitlePlugin)
373 plugin.imported(
374 cast(ImportSession, None), SingletonImportTask(None, item)
375 )
376
377 item.load()
378 assert item.artist == "Alice"
379 assert item.title == "Song feat. Bob"
380
381 def test_albuminfo_received_rewrites_tracks_with_album_artist(self) -> None:
382 track_info = TrackInfo(
383 artist="Alice & Bob", artist_sort="Alice & Bob", title="Song"
384 )
385 info = AlbumInfo(artist="Alice", album="Album", tracks=[track_info])
386
387 with self.configure_plugin({"auto": True}):
388 plugins.send("albuminfo_received", info=info)
389
390 assert track_info.artist == "Alice"
391 assert track_info.artist_sort == "Alice"
392 assert track_info.title == "Song feat. Bob"
393 assert track_info.item_data["artist"] == "Alice"
394 assert track_info.item_data["title"] == "Song feat. Bob"
395
396
397 @pytest.mark.parametrize(
398 "artist,albumartist,expected",
399 [
400 ("Alice ft. Bob", "Alice", "Bob"),
401 ("Alice feat Bob", "Alice", "Bob"),
402 ("Alice featuring Bob", "Alice", "Bob"),
403 ("Alice & Bob", "Alice", "Bob"),
404 ("Alice and Bob", "Alice", "Bob"),
405 ("Alice With Bob", "Alice", "Bob"),
406 ("Alice (ft. Bob)", "Alice", "Bob"),
407 ("Alice [ft. Bob]", "Bob", "Alice"),
408 ("Alice defeat Bob", "Alice", None),
409 ("Alice & Bob", "Bob", "Alice"),
410 ("Alice ft. Bob", "Bob", "Alice"),
411 ("Alice ft. Carol", "Bob", "Carol"),
412 ],
413 )
414 def test_find_feat_part(
415 artist: str, albumartist: str, expected: str | None
416 ) -> None:
417 assert ftintitle.find_feat_part(artist, albumartist) == expected
418
419
420 @pytest.mark.parametrize(
421 "given,expected",
422 [
423 ("Alice ft. Bob", ("Alice", "Bob")),
424 ("Alice feat Bob", ("Alice", "Bob")),
425 ("Alice feat. Bob", ("Alice", "Bob")),
426 ("Alice featuring Bob", ("Alice", "Bob")),
427 ("Alice & Bob", ("Alice", "Bob")),
428 ("Alice, Bob & Charlie", ("Alice", "Bob & Charlie")),
429 (
430 "Alice, Bob & Charlie feat. Xavier",
431 ("Alice, Bob & Charlie", "Xavier"),
432 ),
433 ("Alice and Bob", ("Alice", "Bob")),
434 ("Alice With Bob", ("Alice", "Bob")),
435 ("Alice defeat Bob", ("Alice defeat Bob", None)),
436 ("Alice & Bob feat Charlie", ("Alice & Bob", "Charlie")),
437 ("Alice & Bob ft. Charlie", ("Alice & Bob", "Charlie")),
438 ("Alice & Bob featuring Charlie", ("Alice & Bob", "Charlie")),
439 ("Alice and Bob feat Charlie", ("Alice and Bob", "Charlie")),
440 ("Alice (ft. Bob)", ("Alice", "Bob")),
441 ("Alice [& Bob]", ("Alice", "Bob")),
442 ],
443 )
444 def test_split_on_feat(given: str, expected: tuple[str, str | None]) -> None:
445 assert ftintitle.split_on_feat(given) == expected
446
447
448 @pytest.mark.parametrize(
449 "given,keywords,expected",
450 [
451 ## default keywords
452 # different braces and keywords
453 ("Song (Remix)", None, "Song ft. Bob (Remix)"),
454 ("Song [Version]", None, "Song ft. Bob [Version]"),
455 ("Song {Extended Mix}", None, "Song ft. Bob {Extended Mix}"),
456 ("Song <Instrumental>", None, "Song ft. Bob <Instrumental>"),
457 # two keyword clauses
458 ("Song (Remix) (Live)", None, "Song ft. Bob (Remix) (Live)"),
459 # brace insensitivity
460 ("Song (Live) [Remix]", None, "Song ft. Bob (Live) [Remix]"),
461 ("Song [Edit] (Remastered)", None, "Song ft. Bob [Edit] (Remastered)"),
462 # negative cases
463 ("Song", None, "Song ft. Bob"), # no clause
464 ("Song (Arbitrary)", None, "Song (Arbitrary) ft. Bob"), # no keyword
465 ("Song (", None, "Song ( ft. Bob"), # no matching brace or keyword
466 ("Song (Live", None, "Song (Live ft. Bob"), # no matching brace with keyword
467 # one keyword clause, one non-keyword clause
468 ("Song (Live) (Arbitrary)", None, "Song ft. Bob (Live) (Arbitrary)"),
469 ("Song (Arbitrary) (Remix)", None, "Song (Arbitrary) ft. Bob (Remix)"),
470 # nested brackets - same type
471 ("Song (Remix (Extended))", None, "Song ft. Bob (Remix (Extended))"),
472 ("Song [Arbitrary [Description]]", None, "Song [Arbitrary [Description]] ft. Bob"),
473 # nested brackets - different types
474 ("Song (Remix [Extended])", None, "Song ft. Bob (Remix [Extended])"),
475 # nested - returns outer start position despite inner keyword
476 ("Song [Arbitrary {Extended}]", None, "Song ft. Bob [Arbitrary {Extended}]"),
477 ("Song {Live <Arbitrary>}", None, "Song ft. Bob {Live <Arbitrary>}"),
478 ("Song <Remaster (Arbitrary)>", None, "Song ft. Bob <Remaster (Arbitrary)>"),
479 ("Song <Extended> [Live]", None, "Song ft. Bob <Extended> [Live]"),
480 ("Song (Version) <Live>", None, "Song ft. Bob (Version) <Live>"),
481 ("Song (Arbitrary [Description])", None, "Song (Arbitrary [Description]) ft. Bob"),
482 ("Song [Description (Arbitrary)]", None, "Song [Description (Arbitrary)] ft. Bob"),
483 ## custom keywords
484 ("Song (Live)", ["live"], "Song ft. Bob (Live)"),
485 ("Song (Concert)", ["concert"], "Song ft. Bob (Concert)"),
486 ("Song (Remix)", ["custom"], "Song (Remix) ft. Bob"),
487 ("Song (Custom)", ["custom"], "Song ft. Bob (Custom)"),
488 ("Song", [], "Song ft. Bob"),
489 ("Song (", [], "Song ( ft. Bob"),
490 # Multi-word keyword tests
491 ("Song (Club Mix)", ["club mix"], "Song ft. Bob (Club Mix)"), # Positive: matches multi-word
492 ("Song (Club Remix)", ["club mix"], "Song (Club Remix) ft. Bob"), # Negative: no match
493 ],
494 ) # fmt: skip
495 def test_insert_ft_into_title(
496 given: str, keywords: list[str] | None, expected: str
497 ) -> None:
498 assert (
499 ftintitle.FtInTitlePlugin.insert_ft_into_title(
500 given, "ft. Bob", keywords
501 )
502 == expected
503 )
504
505
506 @pytest.mark.parametrize(
507 "given,expected",
508 [
509 ("Alice ft. Bob", True),
510 ("Alice feat. Bob", True),
511 ("Alice feat Bob", True),
512 ("Alice featuring Bob", True),
513 ("Alice (ft. Bob)", True),
514 ("Alice (feat. Bob)", True),
515 ("Alice [ft. Bob]", True),
516 ("Alice [feat. Bob]", True),
517 ("Alice defeat Bob", False),
518 ("Aliceft.Bob", False),
519 ("Alice (defeat Bob)", False),
520 ("Live and Let Go", False),
521 ("Come With Me", False),
522 ],
523 )
524 def test_contains_feat(given: str, expected: bool) -> None:
525 assert ftintitle.contains_feat(given) is expected
526
527
528 @pytest.mark.parametrize(
529 "given,custom_words,expected",
530 [
531 ("Alice ft. Bob", [], True),
532 ("Alice feat. Bob", [], True),
533 ("Alice feat Bob", [], True),
534 ("Alice featuring Bob", [], True),
535 ("Alice (ft. Bob)", [], True),
536 ("Alice (feat. Bob)", [], True),
537 ("Alice [ft. Bob]", [], True),
538 ("Alice [feat. Bob]", [], True),
539 ("Alice defeat Bob", [], False),
540 ("Aliceft.Bob", [], False),
541 ("Alice (defeat Bob)", [], False),
542 ("Live and Let Go", [], False),
543 ("Come With Me", [], False),
544 ("Alice x Bob", ["x"], True),
545 ("Alice x Bob", ["X"], True),
546 ("Alice och Xavier", ["x"], False),
547 ("Alice ft. Xavier", ["x"], True),
548 ("Alice med Carol", ["med"], True),
549 ("Alice med Carol", [], False),
550 ],
551 )
552 def test_custom_words(
553 given: str, custom_words: list[str], expected: bool
554 ) -> None:
555 assert ftintitle.contains_feat(given, custom_words) is expected
556
557
558 def test_album_template_value(config):
559 config["ftintitle"]["custom_words"] = []
560
561 album = Album()
562 album["albumartist"] = "Foo ft. Bar"
563 assert ftintitle._album_artist_no_feat(album) == "Foo"
564
565 album["albumartist"] = "Foobar"
566 assert ftintitle._album_artist_no_feat(album) == "Foobar"