Debian Codegraph @

test/plugins/test_missing.py

raw
1 """Tests for the `missing` plugin."""
2
3 import re
4 import uuid
5 from unittest.mock import patch
6
7 import pytest
8
9 from beets.autotag import AlbumInfo, TrackInfo
10 from beets.library import Album, Item
11 from beets.test.helper import IOMixin, PluginTestHelper
12
13
14 class MissingTestHelper(IOMixin, PluginTestHelper):
15 plugin = "missing"
16
17
18 class TestMissingAlbums(MissingTestHelper):
19 """Tests for missing albums functionality."""
20
21 @pytest.mark.parametrize(
22 "release_from_mb,expected_output",
23 [
24 pytest.param(
25 {"id": "other", "title": "Other Album"},
26 "Artist - Other Album\n",
27 id="missing",
28 ),
29 pytest.param(
30 {"id": "release_group_in_lib", "title": "Album"},
31 "",
32 id="not missing",
33 ),
34 ],
35 )
36 def test_missing_artist_albums(
37 self, requests_mock, release_from_mb, expected_output
38 ):
39 artist_mbid = str(uuid.uuid4())
40 self.lib.add(
41 Album(
42 album="Album",
43 albumartist="Artist",
44 mb_albumartistid=artist_mbid,
45 mb_albumid="album",
46 mb_releasegroupid="release_group_in_lib",
47 )
48 )
49 requests_mock.get(
50 re.compile(
51 rf"/ws/2/release-group\?artist={artist_mbid}&.*type=album"
52 ),
53 json={"release-groups": [release_from_mb]},
54 )
55
56 with self.configure_plugin({}):
57 assert self.run_with_output("missing", "--album") == expected_output
58
59 def test_missing_albums_custom_format(self, requests_mock):
60 """Test -f/--format is honored when listing missing albums."""
61 artist_mbid = str(uuid.uuid4())
62 self.lib.add(
63 Album(
64 album="Album",
65 albumartist="Artist",
66 mb_albumartistid=artist_mbid,
67 mb_albumid="album",
68 mb_releasegroupid="release_group_in_lib",
69 )
70 )
71 requests_mock.get(
72 re.compile(
73 rf"/ws/2/release-group\?artist={artist_mbid}&.*type=album"
74 ),
75 json={
76 "release-groups": [
77 {
78 "id": "other",
79 "title": "Other Album",
80 "primary-type": "Album",
81 }
82 ]
83 },
84 )
85
86 with self.configure_plugin({}):
87 output = self.run_with_output(
88 "missing",
89 "-a",
90 "-f",
91 "$mb_releasegroupid | $albumtype | $album",
92 )
93
94 assert output == "other | album | Other Album\n"
95
96 def test_release_types_filters_results(self, requests_mock):
97 """Test --release-types filters to only show specified type."""
98 artist_mbid = str(uuid.uuid4())
99 self.lib.add(
100 Album(
101 album="album",
102 albumartist="artist",
103 mb_albumartistid=artist_mbid,
104 mb_albumid="album",
105 mb_releasegroupid="album_id",
106 )
107 )
108 requests_mock.get(
109 re.compile(r"/ws/2/release-group.*type=compilation"),
110 json={
111 "release-groups": [
112 {"id": "compilation_id", "title": "compilation"}
113 ]
114 },
115 )
116
117 with self.configure_plugin({}):
118 output = self.run_with_output(
119 "missing", "-a", "--release-types", "compilation"
120 )
121
122 assert "artist - compilation" in output
123
124 def test_release_types_comma_separated(self, requests_mock):
125 """Test --release-types with comma-separated values."""
126 artist_mbid = str(uuid.uuid4())
127 self.lib.add(
128 Album(
129 album="album",
130 albumartist="artist",
131 mb_albumartistid=artist_mbid,
132 mb_albumid="album",
133 mb_releasegroupid="album_id",
134 )
135 )
136 requests_mock.get(
137 re.compile(r"/ws/2/release-group.*type=compilation%7Calbum"),
138 json={
139 "release-groups": [
140 {"id": "album2_id", "title": "title 2"},
141 {"id": "compilation_id", "title": "compilation"},
142 ]
143 },
144 )
145
146 with self.configure_plugin({}):
147 output = self.run_with_output(
148 "missing", "-a", "--release-types", "compilation,album"
149 )
150
151 assert "artist - compilation" in output
152 assert "artist - title 2" in output
153
154 def test_empty_release_types_config_sends_empty_type(self, requests_mock):
155 """Test that release_types: [] in config sends type="" to the API."""
156 artist_mbid = str(uuid.uuid4())
157 self.lib.add(
158 Album(
159 album="album",
160 albumartist="artist",
161 mb_albumartistid=artist_mbid,
162 mb_albumid="album",
163 mb_releasegroupid="album_id",
164 )
165 )
166 adapter = requests_mock.get(
167 re.compile(r"/ws/2/release-group"), json={"release-groups": []}
168 )
169
170 with self.configure_plugin({"release_types": []}):
171 self.run_with_output("missing", "-a")
172
173 assert adapter.last_request.qs["type"] == [""]
174
175 def test_missing_albums_total(self, requests_mock):
176 """Test -t flag with --album shows total count of missing albums."""
177 artist_mbid = str(uuid.uuid4())
178 self.lib.add(
179 Album(
180 album="album",
181 albumartist="artist",
182 mb_albumartistid=artist_mbid,
183 mb_albumid="album",
184 mb_releasegroupid="album_id",
185 )
186 )
187 requests_mock.get(
188 re.compile(
189 rf"/ws/2/release-group\?artist={artist_mbid}&.*type=album"
190 ),
191 json={
192 "release-groups": [
193 {"id": "album_id", "title": "album"},
194 {"id": "other_id", "title": "other"},
195 ]
196 },
197 )
198
199 with self.configure_plugin({}):
200 output = self.run_with_output("missing", "-a", "-t")
201
202 assert output == "1\n"
203
204
205 class TestMissingTracks(MissingTestHelper):
206 """Tests for missing tracks functionality."""
207
208 @pytest.mark.parametrize(
209 "total,count,expected",
210 [(True, False, "1\n"), (False, True, "artist - album: 1")],
211 )
212 @patch("beets.metadata_plugins.album_for_id")
213 def test_missing_tracks(self, album_for_id, total, count, expected):
214 """Test getting missing tracks works with expected output."""
215 artist_mbid = str(uuid.uuid4())
216 album_items = [
217 Item(
218 album="album",
219 mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6",
220 mb_releasegroupid="album_id",
221 mb_trackid="track_1",
222 mb_albumartistid=artist_mbid,
223 albumartist="artist",
224 tracktotal=3,
225 ),
226 Item(
227 album="album",
228 mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6",
229 mb_releasegroupid="album_id",
230 mb_albumartistid=artist_mbid,
231 albumartist="artist",
232 tracktotal=3,
233 ),
234 Item(
235 album="album",
236 mb_albumid="81ae60d4-5b75-38df-903a-db2cfa51c2c6",
237 mb_releasegroupid="album_id",
238 mb_trackid="track_3",
239 mb_albumartistid=artist_mbid,
240 albumartist="artist",
241 tracktotal=3,
242 ),
243 ]
244 self.lib.add_album(album_items[:2])
245
246 album_for_id.return_value = AlbumInfo(
247 album_id="album_id",
248 album="album",
249 tracks=[
250 TrackInfo(track_id=item.mb_trackid) for item in album_items
251 ],
252 )
253
254 command = ["missing"]
255 if total:
256 command.append("-t")
257 if count:
258 command.append("-c")
259
260 with self.configure_plugin({}):
261 assert expected in self.run_with_output(*command)