Debian Codegraph @

test/plugins/test_playlist.py

raw
1 import os
2 from pathlib import Path
3 from shlex import quote
4
5 import beets
6 from beets.test.helper import PluginTestCase
7
8
9 class PlaylistTestCase(PluginTestCase):
10 plugin = "playlist"
11 preload_plugin = False
12 c_track_path = Path("a") / "b" / "c.mp3"
13 f_track_path = Path("d") / "e" / "f.mp3"
14 i_track_path = Path("g") / "h" / "i.mp3"
15 w_track_path = Path("u") / "v" / "w.mp3"
16 z_track_path = Path("x") / "y" / "z.mp3"
17 nonexisting_track_path = Path("nonexisting.mp3")
18
19 def setUp(self):
20 super().setUp()
21
22 self.music_dir = (Path("~") / "Music").expanduser()
23
24 for p, title, album in [
25 (self.c_track_path, "some item", "some album"),
26 (self.f_track_path, "another item", "another album"),
27 (self.z_track_path, "yet another item", "yet another album"),
28 ]:
29 self.add_album(path=self.music_dir / p, title=title, album=album)
30
31 self.playlist_dir = self.temp_path / "playlists"
32 self.playlist_dir.mkdir(parents=True, exist_ok=True)
33 self.config["directory"] = str(self.music_dir)
34 self.config["playlist"]["playlist_dir"] = str(self.playlist_dir)
35 self.absolute_playlist_path = self.playlist_dir / "absolute.m3u"
36 self.relative_playlist_path = self.playlist_dir / "relative.m3u"
37
38 self.write_absolute_playlist()
39 self.write_relative_playlist()
40 self.setup_test()
41 self.load_plugins()
42
43 def write_absolute_playlist(self):
44 lines = [
45 self.music_dir / self.c_track_path,
46 self.music_dir / self.f_track_path,
47 self.music_dir / self.nonexisting_track_path,
48 ]
49 self.absolute_playlist_path.write_text("\n".join(map(str, lines)))
50
51 def write_relative_playlist(self):
52 lines = [
53 self.c_track_path,
54 self.f_track_path,
55 self.nonexisting_track_path,
56 ]
57 self.relative_playlist_path.write_text("\n".join(map(str, lines)))
58
59 def setup_test(self):
60 raise NotImplementedError
61
62
63 class PlaylistQueryTest:
64 def test_name_query_with_absolute_paths_in_playlist(self):
65 q = "playlist:absolute"
66 results = self.lib.items(q)
67 assert {i.title for i in results} == {"some item", "another item"}
68
69 def test_path_query_with_absolute_paths_in_playlist(self):
70 q = f"playlist:{quote(str(self.absolute_playlist_path))}"
71 results = self.lib.items(q)
72 assert {i.title for i in results} == {"some item", "another item"}
73
74 def test_name_query_with_relative_paths_in_playlist(self):
75 q = "playlist:relative"
76 results = self.lib.items(q)
77 assert {i.title for i in results} == {"some item", "another item"}
78
79 def test_path_query_with_relative_paths_in_playlist(self):
80 q = f"playlist:{quote(str(self.relative_playlist_path))}"
81 results = self.lib.items(q)
82 assert {i.title for i in results} == {"some item", "another item"}
83
84 def test_name_query_with_nonexisting_playlist(self):
85 q = "playlist:nonexisting"
86 results = self.lib.items(q)
87 assert set(results) == set()
88
89 def test_path_query_with_nonexisting_playlist(self):
90 q = f"playlist:{quote(str(self.nonexisting_track_path))}"
91 results = self.lib.items(q)
92 assert set(results) == set()
93
94
95 class PlaylistTestRelativeToLib(PlaylistQueryTest, PlaylistTestCase):
96 def setup_test(self):
97 self.config["playlist"]["relative_to"] = "library"
98
99
100 class PlaylistTestRelativeToDir(PlaylistQueryTest, PlaylistTestCase):
101 def setup_test(self):
102 self.config["playlist"]["relative_to"] = str(self.music_dir)
103
104
105 class PlaylistTestRelativeToPls(PlaylistQueryTest, PlaylistTestCase):
106 def write_relative_playlist(self):
107 lines = [
108 os.path.relpath(
109 self.music_dir / self.c_track_path, self.playlist_dir
110 ),
111 os.path.relpath(
112 self.music_dir / self.f_track_path, self.playlist_dir
113 ),
114 os.path.relpath(
115 self.music_dir / self.nonexisting_track_path, self.playlist_dir
116 ),
117 ]
118 self.relative_playlist_path.write_text("\n".join(lines) + "\n")
119
120 def setup_test(self):
121 self.config["playlist"]["relative_to"] = "playlist"
122 self.config["playlist"]["playlist_dir"] = str(self.playlist_dir)
123
124
125 class PlaylistUpdateTest:
126 def setup_test(self):
127 self.config["playlist"]["auto"] = True
128 self.config["playlist"]["relative_to"] = "library"
129
130
131 class PlaylistTestItemMoved(PlaylistUpdateTest, PlaylistTestCase):
132 def test_item_moved(self):
133 # Emit item_moved event for an item that is in a playlist
134 q = f"path:{quote(str(self.music_dir / self.f_track_path))}"
135 results = self.lib.items(q)
136 item = results[0]
137 beets.plugins.send(
138 "item_moved",
139 item=item,
140 source=item.path,
141 destination=os.fsencode(self.music_dir / self.i_track_path),
142 )
143
144 # Emit item_moved event for an item that is not in a playlist
145 results = self.lib.items(
146 f"path:{quote(str(self.music_dir / self.z_track_path))}"
147 )
148 item = results[0]
149 beets.plugins.send(
150 "item_moved",
151 item=item,
152 source=item.path,
153 destination=os.fsencode(self.music_dir / self.w_track_path),
154 )
155
156 # Emit cli_exit event
157 beets.plugins.send("cli_exit", lib=self.lib)
158
159 expected_paths = [
160 self.c_track_path,
161 self.i_track_path,
162 self.nonexisting_track_path,
163 ]
164 # Check playlist with absolute paths
165 lines = list(
166 map(Path, self.absolute_playlist_path.read_text().splitlines())
167 )
168 assert lines == [self.music_dir / p for p in expected_paths]
169
170 # Check playlist with relative paths
171 lines = list(
172 map(Path, self.relative_playlist_path.read_text().splitlines())
173 )
174 assert lines == expected_paths
175
176
177 class PlaylistTestItemRemoved(PlaylistUpdateTest, PlaylistTestCase):
178 def test_item_removed(self):
179 # Emit item_removed event for an item that is in a playlist
180 q = f"path:{quote(str(self.music_dir / self.f_track_path))}"
181 results = self.lib.items(q)
182 item = results[0]
183 beets.plugins.send("item_removed", item=item)
184
185 # Emit item_removed event for an item that is not in a playlist
186 q = f"path:{quote(str(self.music_dir / self.z_track_path))}"
187 results = self.lib.items(q)
188 item = results[0]
189 beets.plugins.send("item_removed", item=item)
190
191 # Emit cli_exit event
192 beets.plugins.send("cli_exit", lib=self.lib)
193
194 expected_paths = [self.c_track_path, self.nonexisting_track_path]
195 # Check playlist with absolute paths
196 lines = list(
197 map(Path, self.absolute_playlist_path.read_text().splitlines())
198 )
199 assert lines == [self.music_dir / p for p in expected_paths]
200
201 # Check playlist with relative paths
202 lines = list(
203 map(Path, self.relative_playlist_path.read_text().splitlines())
204 )
205 assert lines == expected_paths