Skip to content

Commit fd61dc5

Browse files
committed
test(plugins): write dest_regen test for smartplaylist
Test functions inspired from `test_playlist_update_output_extm3u()` in `test_smartplaylist.py`. Test successfully passed using: `poetry run pytest test/plugins/test_smartplaylist.py`
1 parent 46e3f33 commit fd61dc5

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

test/plugins/test_smartplaylist.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,87 @@ def test_playlist_update_uri_format(self):
332332

333333
assert content == b"http://beets:8337/item/3/file\n"
334334

335+
def test_playlist_update_dest_regen(self):
336+
spl = SmartPlaylistPlugin()
337+
338+
i = MagicMock()
339+
type(i).artist = PropertyMock(return_value="fake artist")
340+
type(i).title = PropertyMock(return_value="fake title")
341+
type(i).length = PropertyMock(return_value=300.123)
342+
# Set a path which is not equal to the one returned by `item.destination`.
343+
type(i).path = PropertyMock(return_value=b"/imported/path/with/dont/move/tagada.mp3")
344+
# Set a path which would be equal to the one returned by `item.destination`.
345+
type(i).destination = PropertyMock(return_value=lambda: b"/tagada.mp3")
346+
i.evaluate_template.side_effect = lambda pl, _: pl.replace(
347+
b"$title",
348+
b"ta:ga:da",
349+
).decode()
350+
351+
lib = Mock()
352+
lib.replacements = CHAR_REPLACE
353+
lib.items.return_value = [i]
354+
lib.albums.return_value = []
355+
356+
q = Mock()
357+
a_q = Mock()
358+
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
359+
spl._matched_playlists = [pl]
360+
361+
dir = bytestring_path(mkdtemp())
362+
config["smartplaylist"]["output"] = "extm3u"
363+
config["smartplaylist"]["prefix"] = "http://beets:8337/files"
364+
config["smartplaylist"]["relative_to"] = False
365+
config["smartplaylist"]["playlist_dir"] = fsdecode(dir)
366+
367+
# Test when `dest_regen` is set to True:
368+
# Intended behavior is to use the path of `i.destination`.
369+
370+
config["smartplaylist"]["dest_regen"] = True
371+
try:
372+
spl.update_playlists(lib)
373+
except Exception:
374+
rmtree(syspath(dir))
375+
raise
376+
377+
lib.items.assert_called_once_with(q, None)
378+
lib.albums.assert_called_once_with(a_q, None)
379+
380+
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u")
381+
self.assertExists(m3u_filepath)
382+
with open(syspath(m3u_filepath), "rb") as f:
383+
content = f.read()
384+
rmtree(syspath(dir))
385+
386+
assert (
387+
content
388+
== b"#EXTM3U\n"
389+
+ b"#EXTINF:300,fake artist - fake title\n"
390+
+ b"http://beets:8337/files/tagada.mp3\n"
391+
)
392+
393+
# Test when `dest_regen` is set to False:
394+
# Intended behavior is to use the path of `i.path`.
395+
396+
config["smartplaylist"]["dest_regen"] = False
397+
398+
try:
399+
spl.update_playlists(lib)
400+
except Exception:
401+
rmtree(syspath(dir))
402+
raise
403+
404+
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u")
405+
self.assertExists(m3u_filepath)
406+
with open(syspath(m3u_filepath), "rb") as f:
407+
content = f.read()
408+
rmtree(syspath(dir))
409+
410+
assert (
411+
content
412+
== b"#EXTM3U\n"
413+
+ b"#EXTINF:300,fake artist - fake title\n"
414+
+ b"http://beets:8337/files/imported/path/with/dont/move/tagada.mp3\n"
415+
)
335416

336417
class SmartPlaylistCLITest(PluginTestCase):
337418
plugin = "smartplaylist"

0 commit comments

Comments
 (0)