Debian Codegraph @

test/plugins/utils/test_playcount.py

raw
1 import pytest
2
3 from beets import logging
4 from beets.library import Item
5 from beets.test.helper import TestHelper
6 from beetsplug._utils.playcount import (
7 get_items,
8 process_track,
9 update_play_counts,
10 )
11
12 LOGGER_NAME = "beets.test_playcount"
13
14
15 class TestPlayCount(TestHelper):
16 @pytest.fixture
17 def log(self):
18 log = logging.getLogger(LOGGER_NAME)
19 log.set_global_level(logging.DEBUG)
20 return log
21
22 def track(self, **overrides):
23 return {
24 "mbid": "",
25 "artist": "Artist",
26 "name": "Song",
27 "playcount": 1,
28 **overrides,
29 }
30
31 def get_playcount(self, item_id, source="lastfm"):
32 field = f"{source}_play_count"
33 return int(self.lib.get_item(item_id).get(field, 0))
34
35 def add_item(
36 self,
37 *,
38 title="Song",
39 artist="Artist",
40 album="Album",
41 mb_trackid="",
42 play_count=None,
43 source="lastfm",
44 ):
45 item = Item(
46 title=title, artist=artist, album=album, mb_trackid=mb_trackid
47 )
48 self.lib.add(item)
49
50 if play_count is not None:
51 item[f"{source}_play_count"] = play_count
52 item.store()
53
54 return item
55
56 @pytest.mark.parametrize(
57 "item_kwargs, track_kwargs",
58 [
59 pytest.param(
60 {"title": "Song", "artist": "Artist"},
61 {"playcount": 7},
62 id="artist-and-title",
63 ),
64 pytest.param(
65 {
66 "title": "Different Song",
67 "artist": "Different Artist",
68 "mb_trackid": "track-id",
69 },
70 {"mbid": "track-id", "playcount": 5},
71 id="musicbrainz-track-id",
72 ),
73 pytest.param(
74 {
75 "title": "Song",
76 "artist": "Different Artist",
77 "album": "Album",
78 },
79 {"album": "Album", "playcount": 3},
80 id="album-and-title",
81 ),
82 pytest.param(
83 {"title": "Don\u2019t Stop", "artist": "Artist"},
84 {"name": "Don't Stop", "playcount": 11},
85 id="apostrophe-normalized",
86 ),
87 ],
88 )
89 def test_get_items_matches_supported_query_paths(
90 self, log, item_kwargs, track_kwargs
91 ):
92 item = self.add_item(**item_kwargs)
93 matched_ids = [
94 matched.id
95 for matched in get_items(self.lib, self.track(**track_kwargs), log)
96 ]
97
98 assert matched_ids == [item.id]
99
100 def test_get_items_mbid_match_suppresses_title_match(self, log):
101 by_mbid = self.add_item(
102 title="Song", artist="Artist", mb_trackid="track-id"
103 )
104 self.add_item(title="Song", artist="Artist", mb_trackid="other-id")
105
106 matched_ids = [
107 matched.id
108 for matched in get_items(self.lib, self.track(mbid="track-id"), log)
109 ]
110
111 assert matched_ids == [by_mbid.id]
112
113 def test_get_items_prefers_exact_match(self, log):
114 exact_match = self.add_item(title="Song", artist="Artist")
115 self.add_item(title="Song (inst.)", artist="Artist")
116
117 matched_ids = [
118 matched.id for matched in get_items(self.lib, self.track(), log)
119 ]
120
121 assert matched_ids == [exact_match.id]
122
123 def test_get_items_falls_back_to_substring_match(self, log):
124 substring_match = self.add_item(
125 title="Song (feat. Artist)", artist="Artist"
126 )
127
128 matched_ids = [
129 matched.id for matched in get_items(self.lib, self.track(), log)
130 ]
131
132 assert matched_ids == [substring_match.id]
133
134 def test_process_track_updates_every_matching_song(self, log):
135 first = self.add_item(
136 title="Song", artist="Artist", album="First Album", play_count=1
137 )
138 second = self.add_item(
139 title="Song", artist="Artist", album="Second Album", play_count=9
140 )
141
142 assert (
143 process_track(self.lib, self.track(playcount=0), log, "lastfm")
144 is True
145 )
146
147 assert self.get_playcount(first.id) == 0
148 assert self.get_playcount(second.id) == 0
149
150 def test_process_track_returns_false_when_nothing_matches(self, log):
151 assert (
152 process_track(
153 self.lib,
154 self.track(
155 artist="Missing Artist",
156 name="Missing Song",
157 album="Missing Album",
158 playcount=4,
159 ),
160 log,
161 "lastfm",
162 )
163 is False
164 )
165
166 def test_process_track_updates_requested_source_field(self, log):
167 new_count = 6
168 item = self.add_item(play_count=1, source="lastfm")
169
170 assert (
171 process_track(
172 self.lib, self.track(playcount=new_count), log, "listenbrainz"
173 )
174 is True
175 )
176
177 assert self.get_playcount(item.id, "lastfm") == 1
178 assert self.get_playcount(item.id, "listenbrainz") == new_count
179
180 @pytest.mark.parametrize(
181 "tracks, expected_counts, expected_summary, expected_playcount",
182 [
183 pytest.param([], (0, 0), None, None, id="empty-page"),
184 pytest.param(
185 [
186 {"artist": "Artist", "name": "Known Song", "playcount": 8},
187 {
188 "artist": "Missing Artist",
189 "name": "Missing Song",
190 "playcount": 2,
191 },
192 ],
193 (1, 1),
194 "Acquired 1/2 play-counts (1 unknown)",
195 8,
196 id="mixed-results",
197 ),
198 ],
199 )
200 def test_update_play_counts_counts_and_logs_summary(
201 self,
202 log,
203 tracks,
204 expected_counts,
205 expected_summary,
206 expected_playcount,
207 caplog,
208 ):
209 item = self.add_item(title="Known Song", artist="Artist", play_count=1)
210
211 with caplog.at_level("DEBUG", logger=LOGGER_NAME):
212 assert (
213 update_play_counts(
214 self.lib,
215 [self.track(**track) for track in tracks],
216 log,
217 "lastfm",
218 )
219 == expected_counts
220 )
221
222 assert any(
223 f"Received {len(tracks)} tracks in this page, processing..." in msg
224 for msg in caplog.messages
225 )
226 if expected_summary is None:
227 assert not any("Acquired" in msg for msg in caplog.messages)
228 else:
229 assert expected_summary in caplog.text
230 assert self.get_playcount(item.id) == expected_playcount