Debian Codegraph @

test/plugins/test_discogs.py

raw
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
246 class TestStripDisambiguation(DiscogsTestMixin):
247 @pytest.fixture
248 def album_info(self, plugin):
249 data = {
250 "id": 123,
251 "uri": "https://www.discogs.com/release/123456-something",
252 "tracklist": [
253 {
254 "title": "track",
255 "position": "A",
256 "type_": "track",
257 "duration": "5:44",
258 "artists": [_artist("TEST ARTIST (5)", id=11146)],
259 }
260 ],
261 "artists": [
262 _artist("ARTIST NAME (2)", id=321, join="&"),
263 _artist("OTHER ARTIST (5)", id=322),
264 ],
265 "title": "title",
266 "labels": [{"name": "LABEL NAME (5)", "catno": "catalog number"}],
267 }
268 return plugin.get_album_info(get_release(data))
269
270 @pytest.mark.parametrize("plugin_config", [{"strip_disambiguation": True}])
271 def test_strip_disambiguation(self, album_info):
272 """Test removing disambiguation from all disambiguated fields."""
273 assert album_info.artist == "ARTIST NAME & OTHER ARTIST"
274 assert album_info.artists == ["ARTIST NAME", "OTHER ARTIST"]
275 assert album_info.artists_ids == ["321", "322"]
276 assert album_info.tracks[0].artist == "TEST ARTIST"
277 assert album_info.tracks[0].artists == ["TEST ARTIST"]
278 assert album_info.tracks[0].artist_id == "11146"
279 assert album_info.tracks[0].artists_ids == ["11146"]
280 assert album_info.label == "LABEL NAME"
281
282 @pytest.mark.parametrize("plugin_config", [{"strip_disambiguation": False}])
283 def test_dont_strip_disambiguation(self, album_info):
284 """Test disabling disambiguation removal from all disambiguated fields."""
285 assert album_info.artist == "ARTIST NAME (2) & OTHER ARTIST (5)"
286 assert album_info.artists == ["ARTIST NAME (2)", "OTHER ARTIST (5)"]
287 assert album_info.tracks[0].artist == "TEST ARTIST (5)"
288 assert album_info.tracks[0].artists == ["TEST ARTIST (5)"]
289 assert album_info.label == "LABEL NAME (5)"
290
291
292 class TestTracklist(DiscogsTestMixin):
293 @pytest.mark.parametrize(
294 "positions,expected_mediums,expected_tracks",
295 [
296 _p(["1", "2", "3"], 1, 3, id="without-sides"),
297 _p(["A1", "A2", "B1", "B2"], 1, 4, id="with-sides"),
298 _p(["A1", "A2", "B1", "C1"], 2, 4, id="multiple-lp"),
299 _p(["1-1", "1-2", "2-1", "3-1"], 3, 4, id="multiple-cd"),
300 _p(["I", "II", "III", "IV"], 1, 4, id="non-standard"),
301 _p(["1", "2.1", "2.2", "3"], 1, 3, id="subtracks-dot"),
302 _p(["A1", "A2.1", "A2.2", "A3"], 1, 3, id="subtracks-dot-with-sides"),
303 _p(["A1", "A2a", "A2b", "A3"], 1, 3, id="subtracks-letter"),
304 _p(["A1", "A2.a", "A2.b", "A3"], 1, 3, id="subtracks-letter-with-dot"),
305 _p(["1.1", "1.2", "2.1", "2.2"], 1, 2, id="multiple-subtrack-groups"),
306 _p(["1", "2", "Video 1"], 2, 3, id="subtracks-extra-material"),
307 ],
308 ) # fmt: skip
309 def test_parse_tracklist_positions(
310 self, plugin, positions, expected_mediums, expected_tracks
311 ):
312 release = self._make_release_from_positions(positions)
313 d = plugin.get_album_info(release)
314
315 assert d.mediums == expected_mediums
316 assert len(d.tracks) == expected_tracks
317
318 @pytest.mark.parametrize(
319 "plugin_config,tracks,expected_mediums,expected_tracks",
320 [
321 _p(
322 {"index_tracks": False},
323 [
324 heading_track("MEDIUM TITLE"),
325 audio_track("TITLE ONE", "1.1"),
326 audio_track("TITLE TWO", "1.2"),
327 ],
328 1,
329 [("TITLE ONE / TITLE TWO", "MEDIUM TITLE")],
330 id="flat-logical-subtracks",
331 ),
332 _p(
333 {"index_tracks": False},
334 [
335 audio_track("TITLE ONE", "1"),
336 index_track(
337 "TRACK GROUP TITLE",
338 [
339 audio_track("SUBTITLE ONE", "2.1", "01:01"),
340 audio_track("SUBTITLE TWO", "2.2", "02:02"),
341 ],
342 ),
343 audio_track("TITLE THREE", "3"),
344 ],
345 1,
346 [
347 ("TITLE ONE", None),
348 ("TRACK GROUP TITLE", None),
349 ("TITLE THREE", None),
350 ],
351 id="nested-logical-subtracks",
352 ),
353 _p(
354 {"index_tracks": False},
355 [
356 audio_track("TITLE ONE", "1"),
357 index_track(
358 "TRACK GROUP TITLE",
359 [
360 audio_track("SUBTITLE ONE", "2", "01:01"),
361 audio_track("SUBTITLE TWO", "3", "02:02"),
362 ],
363 ),
364 audio_track("TITLE FOUR", "4"),
365 ],
366 1,
367 [
368 ("TITLE ONE", None),
369 ("SUBTITLE ONE", None),
370 ("SUBTITLE TWO", None),
371 ("TITLE FOUR", None),
372 ],
373 id="nested-physical-subtracks",
374 ),
375 _p(
376 {"index_tracks": True},
377 [
378 audio_track("TITLE ONE", "1"),
379 index_track(
380 "TRACK GROUP TITLE",
381 [
382 audio_track("SUBTITLE ONE", "2", "01:01"),
383 audio_track("SUBTITLE TWO", "3", "02:02"),
384 ],
385 ),
386 audio_track("TITLE FOUR", "4"),
387 ],
388 1,
389 [
390 ("TITLE ONE", None),
391 ("TRACK GROUP TITLE: SUBTITLE ONE", None),
392 ("TRACK GROUP TITLE: SUBTITLE TWO", None),
393 ("TITLE FOUR", None),
394 ],
395 id="nested-physical-subtracks-with-index-tracks",
396 ),
397 _p(
398 {"index_tracks": False},
399 [
400 heading_track("MEDIUM TITLE CD1"),
401 audio_track("TITLE ONE", "1-1"),
402 audio_track("TITLE TWO", "1-2"),
403 heading_track("MEDIUM TITLE CD2"),
404 audio_track("TITLE THREE", "2-1"),
405 ],
406 2,
407 [
408 ("TITLE ONE", "MEDIUM TITLE CD1"),
409 ("TITLE TWO", "MEDIUM TITLE CD1"),
410 ("TITLE THREE", "MEDIUM TITLE CD2"),
411 ],
412 id="medium-titles",
413 ),
414 ],
415 ) # fmt: skip
416 def test_parse_tracklist_index_tracks(
417 self, plugin, tracks, expected_mediums, expected_tracks
418 ):
419 release = self._make_release(tracks)
420 album = plugin.get_album_info(release)
421
422 assert album.mediums == expected_mediums
423 assert [(t.title, t.disctitle) for t in album.tracks] == expected_tracks
424
425 def test_parse_tracklist_inherited_artists(self, plugin):
426 """Verify grouped tracks combine explicit and inherited artist credits.
427
428 This covers releases where a track group provides the default artist for
429 sub-tracks that do not declare one themselves.
430
431 Note: this is based on the following release:
432 https://www.discogs.com/release/3647530
433 """
434 track_artist = "TRACK ARTIST"
435 group_artist = "GROUP ARTIST"
436 tracks = [
437 index_track(
438 "TRACK GROUP TITLE",
439 [
440 audio_track(
441 "TRACK ONE", "2", artists=[_artist(track_artist)]
442 ),
443 audio_track("SUBTITLE TWO", "3"),
444 ],
445 artists=[_artist(group_artist)],
446 )
447 ]
448 release = self._make_release(tracks)
449 album = plugin.get_album_info(release)
450
451 assert album.mediums == 1
452 assert {t.artist for t in album.tracks} == {track_artist, group_artist}
453
454 def test_coalesce_logical_index_as_audio_track(self, plugin):
455 index = index_track(
456 "TRACK GROUP TITLE",
457 [
458 audio_track("SUBTITLE ONE", "2.1"),
459 audio_track("SUBTITLE TWO", "2.2"),
460 ],
461 duration="03:03",
462 )
463
464 assert plugin._coalesce_tracks([index]) == [
465 audio_track("TRACK GROUP TITLE", "2", "03:03")
466 ]
467
468
469 class TestDGSearchQuery(TestHelper):
470 def test_default_search_filters_without_extra_tags(self):
471 """Discogs search uses only the type filter when no extra_tags are set."""
472 plugin = DiscogsPlugin()
473 items = [Item()]
474
475 query, filters = plugin.get_search_query_with_filters(
476 "album", items, "Artist", "Album", False
477 )
478
479 assert "Album" in query
480 assert filters == {"type": "release"}
481
482 def test_extra_tags_populate_discogs_filters(self):
483 """Configured extra_tags should populate Discogs search filters."""
484 plugin = DiscogsPlugin()
485 plugin.config["extra_tags"] = ["label", "catalognum"]
486
487 items = [
488 Item(catalognum="ABC 123", label="abc"),
489 Item(catalognum="ABC 123", label="abc"),
490 Item(catalognum="ABC 123", label="def"),
491 ]
492
493 _query, filters = plugin.get_search_query_with_filters(
494 "album", items, "Artist", "Album", False
495 )
496
497 assert filters["type"] == "release"
498 assert filters["label"] == "abc"
499 # Catalog number should have whitespace removed.
500 assert filters["catno"] == "ABC123"
501 config["discogs"]["extra_tags"] = []
502
503
504 class TestDGSearchResponse(DiscogsTestMixin):
505 @staticmethod
506 def _decode_error():
507 return json.JSONDecodeError("Expecting value", "", 0)
508
509 @pytest.fixture
510 def params(self):
511 return SearchParams("album", "Album", {"type": "release"}, 5)
512
513 @pytest.fixture
514 def client(self, plugin):
515 plugin.discogs_client = MagicMock()
516 return plugin.discogs_client
517
518 def test_retries_invalid_json_response(self, plugin, client, params):
519 result = MagicMock(data={"id": 123})
520 results = client.search.return_value
521 results.page.side_effect = [self._decode_error(), [result]]
522
523 assert plugin.get_search_response(params) == [{"id": 123}]
524 assert client.search.call_count == 2
525 assert results.page.call_count == 2
526 assert results.per_page == params.limit
527
528 def test_raises_after_invalid_json_retry(self, plugin, client, params):
529 results = client.search.return_value
530 results.page.side_effect = [self._decode_error(), self._decode_error()]
531
532 with pytest.raises(json.JSONDecodeError):
533 plugin.get_search_response(params)
534
535 assert client.search.call_count == 2
536 assert results.page.call_count == 2
537
538
539 class TestAnv:
540 @pytest.fixture
541 def album_info(self, monkeypatch, anv_config):
542 monkeypatch.setattr(
543 "beetsplug.discogs.DiscogsPlugin.setup", lambda _: None
544 )
545 data = {
546 "id": 123,
547 "uri": "https://www.discogs.com/release/123456-something",
548 "tracklist": [
549 {
550 "title": "track",
551 "position": "A",
552 "type_": "track",
553 "duration": "5:44",
554 "artists": [
555 _artist("ARTIST", id=11146, anv="ART", join="Feat."),
556 _artist("PERFORMER", id=787),
557 ],
558 "extraartists": [
559 _artist("PERFORMER", id=787, role="Featuring")
560 ],
561 }
562 ],
563 "artists": [
564 _artist("DRUMMER", id=445, anv="DRUM", join=", "),
565 _artist("ARTIST (4)", id=321, anv="ARTY", join="&"),
566 _artist("SOLOIST", id=446, anv="SOLO"),
567 ],
568 "title": "title",
569 }
570 release = get_release(data)
571 plugin = DiscogsPlugin()
572 plugin.config["anv"].set(
573 {"artist": False, "album_artist": False, "artist_credit": False}
574 | anv_config
575 )
576 return plugin.get_album_info(release)
577
578 @staticmethod
579 def _assert_fields(obj, expected):
580 for field, value in expected.items():
581 assert getattr(obj, field) == value
582
583 @pytest.mark.parametrize(
584 "anv_config,expected_track_fields",
585 [
586 (
587 {"artist": False},
588 {
589 "artist": "ARTIST Feat. PERFORMER",
590 "artists": ["ARTIST", "PERFORMER"],
591 },
592 ),
593 (
594 {"artist": True},
595 {
596 "artist": "ART Feat. PERFORMER",
597 "artists": ["ART", "PERFORMER"],
598 },
599 ),
600 ],
601 )
602 def test_track_artist_fields(self, album_info, expected_track_fields):
603 self._assert_fields(album_info.tracks[0], expected_track_fields)
604
605 @pytest.mark.parametrize(
606 "anv_config,expected_album_fields,expected_track_fields",
607 [
608 (
609 {"artist_credit": False},
610 {
611 "artist_credit": "DRUMMER, ARTIST & SOLOIST",
612 "artists_credit": ["DRUMMER", "ARTIST", "SOLOIST"],
613 },
614 {
615 "artist_credit": "ARTIST Feat. PERFORMER",
616 "artists_credit": ["ARTIST", "PERFORMER"],
617 },
618 ),
619 (
620 {"artist_credit": True},
621 {
622 "artist_credit": "DRUM, ARTY & SOLO",
623 "artists_credit": ["DRUM", "ARTY", "SOLO"],
624 },
625 {
626 "artist_credit": "ART Feat. PERFORMER",
627 "artists_credit": ["ART", "PERFORMER"],
628 },
629 ),
630 ],
631 )
632 def test_artist_credit_fields(
633 self, album_info, expected_album_fields, expected_track_fields
634 ):
635 self._assert_fields(album_info, expected_album_fields)
636 self._assert_fields(album_info.tracks[0], expected_track_fields)
637
638 @pytest.mark.parametrize(
639 "anv_config,expected_album_fields",
640 [
641 (
642 {"album_artist": False},
643 {
644 "artist": "DRUMMER, ARTIST & SOLOIST",
645 "artists": ["DRUMMER", "ARTIST", "SOLOIST"],
646 },
647 ),
648 (
649 {"album_artist": True},
650 {
651 "artist": "DRUM, ARTY & SOLO",
652 "artists": ["DRUM", "ARTY", "SOLO"],
653 },
654 ),
655 ],
656 )
657 def test_album_artist_fields(self, album_info, expected_album_fields):
658 self._assert_fields(album_info, expected_album_fields)
659
660
661 def test_anv_album_artist():
662 """Test using artist name variations when the album artist
663 is the same as the track artist, but only the track artist
664 should use the artist name variation."""
665 data = {
666 "id": 123,
667 "uri": "https://www.discogs.com/release/123456-something",
668 "tracklist": [
669 {
670 "title": "track",
671 "position": "A",
672 "type_": "track",
673 "duration": "5:44",
674 }
675 ],
676 "artists": [_artist("ARTIST (4)", id=321, anv="VARIATION")],
677 "title": "title",
678 }
679 release = get_release(data)
680 config["discogs"]["anv"]["album_artist"] = False
681 config["discogs"]["anv"]["artist"] = True
682 config["discogs"]["anv"]["artist_credit"] = False
683 r = DiscogsPlugin().get_album_info(release)
684 assert r.artist == "ARTIST"
685 assert r.artists == ["ARTIST"]
686 assert r.artist_credit == "ARTIST"
687 assert r.artist_id == "321"
688 assert r.artists_credit == ["ARTIST"]
689 assert r.tracks[0].artist == "VARIATION"
690 assert r.tracks[0].artists == ["VARIATION"]
691 assert r.tracks[0].artist_credit == "ARTIST"
692 assert r.tracks[0].artists_credit == ["ARTIST"]
693
694
695 def test_parse_featured_artists():
696 """Tests the plugins ability to parse a featured artist.
697 Ignores artists that are not listed as featured."""
698 track = {
699 "type_": "track",
700 "title": "track",
701 "position": "1",
702 "duration": "5:00",
703 "artists": [
704 _artist("NEW ARTIST", id=11146, join="&"),
705 _artist("VOCALIST", id=344, join="feat."),
706 ],
707 "extraartists": [
708 _artist("SOLOIST", id=3, role="Featuring"),
709 _artist("PERFORMER (1)", id=5, role="Other Role, Featuring"),
710 _artist("RANDOM", id=8, role="Written-By"),
711 _artist("MUSICIAN", id=10, role="Featuring [Uncredited]"),
712 ],
713 }
714
715 plugin = DiscogsPlugin()
716 artistinfo = ArtistState.from_config(plugin.config, [_artist("ARTIST")])
717 t, _, _ = plugin.get_track_info(track, 1, 1, artistinfo)
718 assert (
719 t.artist == "NEW ARTIST & VOCALIST feat. SOLOIST, PERFORMER, MUSICIAN"
720 )
721 assert t.artists == [
722 "NEW ARTIST",
723 "VOCALIST",
724 "SOLOIST",
725 "PERFORMER",
726 "MUSICIAN",
727 ]
728 assert t.artists_ids == ["11146", "344", "3", "5", "10"]
729 assert t.composers == ["RANDOM"]
730
731
732 def test_parse_extraartist_roles():
733 plugin = DiscogsPlugin()
734 artistinfo = ArtistState.from_config(plugin.config, [_artist("ARTIST")])
735 track = {
736 "type_": "track",
737 "title": "track",
738 "position": "1",
739 "duration": "5:00",
740 "artists": [_artist("TRACK ARTIST", id=11)],
741 "extraartists": [
742 _artist("LYRICIST", id=2, role="Lyrics By"),
743 _artist("ARRANGER", id=3, role="Arranged By"),
744 _artist("REMIXER", id=5, role="Remixed By"),
745 _artist("COMPOSER", id=6, role="Written-By"),
746 ],
747 }
748
749 t, _, _ = plugin.get_track_info(track, 1, 1, artistinfo)
750
751 assert t.artist == "TRACK ARTIST"
752 assert t.artists == ["TRACK ARTIST"]
753 assert t.lyricists == ["LYRICIST"]
754 assert t.arrangers == ["ARRANGER"]
755 assert t.remixers == ["REMIXER"]
756 assert t.composers == ["COMPOSER"]
757
758
759 @pytest.mark.parametrize(
760 "formats, expected_media, expected_albumtype",
761 [
762 (None, None, None),
763 (
764 [
765 {
766 "descriptions": ['7"', "Single", "45 RPM"],
767 "name": "Vinyl",
768 "qty": 1,
769 }
770 ],
771 "Vinyl",
772 '7", Single, 45 RPM',
773 ),
774 ],
775 )
776 def test_get_media_and_albumtype(formats, expected_media, expected_albumtype):
777 result = DiscogsPlugin.get_media_and_albumtype(formats)
778
779 assert result == (expected_media, expected_albumtype)
780
781
782 def test_va_buildartistinfo():
783 config["va_name"] = "VARIOUS ARTISTS"
784 expected_info = {
785 "artist": "VARIOUS ARTISTS",
786 "artist_id": "1",
787 "artists": ["VARIOUS ARTISTS"],
788 "artists_ids": ["1"],
789 "artist_credit": "VARIOUS ARTISTS",
790 "artists_credit": ["VARIOUS ARTISTS"],
791 "arrangers": None,
792 "composers": None,
793 "remixers": None,
794 "lyricists": None,
795 }
796
797 assert (
798 ArtistState.from_config(
799 DiscogsPlugin().config, [_artist("Various")]
800 ).info
801 == expected_info
802 )
803
804
805 @pytest.mark.parametrize(
806 "position, medium, index, subindex",
807 [
808 ("1", None, "1", None),
809 ("A12", "A", "12", None),
810 ("12-34", "12-", "34", None),
811 ("CD1-1", "CD1-", "1", None),
812 ("1.12", None, "1", "12"),
813 ("12.a", None, "12", "A"),
814 ("12.34", None, "12", "34"),
815 ("1ab", None, "1", "AB"),
816 # Non-standard
817 ("IV", "IV", None, None),
818 ],
819 )
820 def test_get_track_index(position, medium, index, subindex):
821 assert DiscogsPlugin.get_track_index(position) == (medium, index, subindex)