Debian Codegraph @

test/autotag/test_match.py

raw
1 from typing import ClassVar
2
3 import pytest
4
5 from beets import metadata_plugins
6 from beets.autotag import (
7 AlbumInfo,
8 Source,
9 TrackInfo,
10 assign_items,
11 tag_album,
12 tag_item,
13 )
14 from beets.library import Item
15
16
17 class TestAssignment:
18 A = "one"
19 B = "two"
20 C = "three"
21
22 @pytest.fixture(autouse=True)
23 def config(self, config):
24 config["match"]["track_length_grace"] = 10
25 config["match"]["track_length_max"] = 30
26
27 @pytest.mark.parametrize(
28 # 'expected' is a tuple of expected (mapping, extra_items, extra_tracks)
29 "item_titles, track_titles, expected",
30 [
31 # items ordering gets corrected
32 ([A, C, B], [A, B, C], ({A: A, B: B, C: C}, [], [])),
33 # unmatched tracks are returned as 'extra_tracks'
34 # the first track is unmatched
35 ([B, C], [A, B, C], ({B: B, C: C}, [], [A])),
36 # the middle track is unmatched
37 ([A, C], [A, B, C], ({A: A, C: C}, [], [B])),
38 # the last track is unmatched
39 ([A, B], [A, B, C], ({A: A, B: B}, [], [C])),
40 # unmatched items are returned as 'extra_items'
41 ([A, C, B], [A, C], ({A: A, C: C}, [B], [])),
42 ],
43 )
44 def test_assign_tracks(self, item_titles, track_titles, expected):
45 expected_mapping, expected_extra_items, expected_extra_tracks = expected
46
47 items = [Item(title=title) for title in item_titles]
48 tracks = [TrackInfo(title=title) for title in track_titles]
49
50 item_info_pairs, extra_items, extra_tracks = assign_items(items, tracks)
51
52 assert (
53 {i.title: t.title for i, t in item_info_pairs},
54 [i.title for i in extra_items],
55 [t.title for t in extra_tracks],
56 ) == (expected_mapping, expected_extra_items, expected_extra_tracks)
57
58 def test_order_works_when_track_names_are_entirely_wrong(self):
59 # A real-world test case contributed by a user.
60 def item(i, length):
61 return Item(
62 artist="ben harper",
63 album="burn to shine",
64 title=f"ben harper - Burn to Shine {i}",
65 track=i,
66 length=length,
67 )
68
69 items = []
70 items.append(item(1, 241.37243007106997))
71 items.append(item(2, 342.27781704375036))
72 items.append(item(3, 245.95070222338137))
73 items.append(item(4, 472.87662515485437))
74 items.append(item(5, 279.1759535763187))
75 items.append(item(6, 270.33333768012))
76 items.append(item(7, 247.83435613222923))
77 items.append(item(8, 216.54504531525072))
78 items.append(item(9, 225.72775379800484))
79 items.append(item(10, 317.7643606963552))
80 items.append(item(11, 243.57001238834192))
81 items.append(item(12, 186.45916150485752))
82
83 def info(index, title, length):
84 return TrackInfo(title=title, length=length, index=index)
85
86 trackinfo = []
87 trackinfo.append(info(1, "Alone", 238.893))
88 trackinfo.append(info(2, "The Woman in You", 341.44))
89 trackinfo.append(info(3, "Less", 245.59999999999999))
90 trackinfo.append(info(4, "Two Hands of a Prayer", 470.49299999999999))
91 trackinfo.append(info(5, "Please Bleed", 277.86599999999999))
92 trackinfo.append(info(6, "Suzie Blue", 269.30599999999998))
93 trackinfo.append(info(7, "Steal My Kisses", 245.36000000000001))
94 trackinfo.append(info(8, "Burn to Shine", 214.90600000000001))
95 trackinfo.append(info(9, "Show Me a Little Shame", 224.0929999999999))
96 trackinfo.append(info(10, "Forgiven", 317.19999999999999))
97 trackinfo.append(info(11, "Beloved One", 243.733))
98 trackinfo.append(info(12, "In the Lord's Arms", 186.13300000000001))
99
100 expected = list(zip(items, trackinfo)), [], []
101
102 assert assign_items(items, trackinfo) == expected
103
104
105 class TestTagMultipleDataSources:
106 @pytest.fixture
107 def shared_track_id(self):
108 return "track-12345"
109
110 @pytest.fixture
111 def shared_album_id(self):
112 return "album-12345"
113
114 @pytest.fixture(autouse=True)
115 def _setup_plugins(self, monkeypatch, shared_album_id, shared_track_id):
116 class StubPlugin:
117 data_source: ClassVar[str]
118 data_source_mismatch_penalty = 0
119
120 @property
121 def track(self):
122 return TrackInfo(
123 artist="Artist",
124 title="Title",
125 track_id=shared_track_id,
126 data_source=self.data_source,
127 )
128
129 @property
130 def album(self):
131 return AlbumInfo(
132 [self.track],
133 artist="Albumartist",
134 album="Album",
135 album_id=shared_album_id,
136 data_source=self.data_source,
137 )
138
139 def albums_for_ids(self, *_):
140 yield self.album
141
142 def tracks_for_ids(self, *_):
143 yield self.track
144
145 def candidates(self, *_, **__):
146 yield self.album
147
148 def item_candidates(self, *_, **__):
149 yield self.track
150
151 class DeezerPlugin(StubPlugin):
152 data_source = "Deezer"
153
154 class DiscogsPlugin(StubPlugin):
155 data_source = "Discogs"
156
157 monkeypatch.setattr(
158 metadata_plugins,
159 "find_metadata_source_plugins",
160 lambda: [DeezerPlugin(), DiscogsPlugin()],
161 )
162
163 def check_proposal(self, proposal):
164 sources = [
165 candidate.info.data_source for candidate in proposal.candidates
166 ]
167 assert len(sources) == 2
168 assert set(sources) == {"Discogs", "Deezer"}
169
170 def test_search_album_ids(self, shared_album_id):
171 source = Source.from_items([Item()])
172
173 proposal = tag_album(source, search_ids=[shared_album_id])
174
175 self.check_proposal(proposal)
176
177 def test_search_album_current_id(self, shared_album_id):
178 source = Source.from_items([Item(mb_albumid=shared_album_id)])
179
180 proposal = tag_album(source)
181
182 self.check_proposal(proposal)
183
184 def test_search_track_ids(self, shared_track_id):
185 source = Source.from_item(Item())
186
187 proposal = tag_item(source, search_ids=[shared_track_id])
188
189 self.check_proposal(proposal)
190
191 def test_search_track_current_id(self, shared_track_id):
192 source = Source.from_item(Item(mb_trackid=shared_track_id))
193
194 proposal = tag_item(source)
195
196 self.check_proposal(proposal)