Debian Codegraph @

test/plugins/test_mbsync.py

raw
1 from unittest.mock import Mock, patch
2
3 from beets.autotag import AlbumInfo, TrackInfo
4 from beets.library import Item
5 from beets.test.helper import PluginTestHelper
6
7
8 class TestMbsyncCli(PluginTestHelper):
9 plugin = "mbsync"
10
11 @patch(
12 "beets.metadata_plugins.album_for_id",
13 Mock(
14 side_effect=lambda *_: AlbumInfo(
15 album_id="album id",
16 album="new album",
17 tracks=[TrackInfo(track_id="track id", title="new title")],
18 )
19 ),
20 )
21 @patch(
22 "beets.metadata_plugins.track_for_id",
23 Mock(
24 side_effect=lambda *_: TrackInfo(
25 track_id="singleton id", title="new title"
26 )
27 ),
28 )
29 def test_update_library(self):
30 album_item = Item(
31 album="old album",
32 mb_albumid="album id",
33 mb_trackid="track id",
34 data_source="data_source",
35 )
36 self.lib.add_album([album_item])
37
38 singleton = Item(
39 title="old title",
40 mb_trackid="singleton id",
41 data_source="data_source",
42 )
43 self.lib.add(singleton)
44
45 self.run_command("mbsync")
46
47 singleton.load()
48 assert singleton.title == "new title"
49
50 album_item.load()
51 assert album_item.title == "new title"
52 assert album_item.mb_trackid == "track id"
53 assert album_item.get_album().album == "new album"
54
55 def test_custom_format(self, caplog):
56 for item in [
57 Item(artist="albumartist", album="no id"),
58 Item(
59 artist="albumartist", album="invalid id", mb_albumid="a1b2c3d4"
60 ),
61 ]:
62 self.lib.add_album([item])
63
64 for item in [
65 Item(artist="artist", title="no id"),
66 Item(artist="artist", title="invalid id", mb_trackid="a1b2c3d4"),
67 ]:
68 self.lib.add(item)
69
70 with caplog.at_level("DEBUG", logger="beets.mbsync"):
71 self.run_command("mbsync", "-f", "'%if{$album,$album,$title}'")
72
73 assert "Skipping album with no mb_albumid: 'no id'" in caplog.messages
74 assert (
75 "Skipping singleton with no mb_trackid: 'no id'" in caplog.messages
76 )
77
78 @patch(
79 "beets.metadata_plugins.album_for_id",
80 Mock(
81 side_effect=lambda *_: AlbumInfo(
82 album_id="album id",
83 album="new album",
84 tracks=[TrackInfo(track_id="track id", title="new title")],
85 )
86 ),
87 )
88 @patch(
89 "beets.metadata_plugins.track_for_id",
90 Mock(
91 side_effect=lambda *_: TrackInfo(
92 track_id="singleton id", title="new title"
93 )
94 ),
95 )
96 def test_update_library_from_scratch_set(self):
97 self.config["import"]["from_scratch"] = True
98
99 test_lyrics = "Hello"
100
101 album_item = Item(
102 album="old album",
103 mb_albumid="album id",
104 mb_trackid="track id",
105 data_source="data_source",
106 lyrics=test_lyrics,
107 )
108 self.lib.add_album([album_item])
109
110 singleton = Item(
111 title="old title",
112 mb_trackid="singleton id",
113 data_source="data_source",
114 lyrics=test_lyrics,
115 )
116 self.lib.add(singleton)
117
118 self.run_command("mbsync")
119
120 singleton.load()
121 assert singleton.lyrics == test_lyrics
122
123 album_item.load()
124 assert album_item.lyrics == test_lyrics