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`
This commit is contained in:
Pierre Ayoub 2025-03-22 10:18:32 +01:00 committed by Šarūnas Nejus
parent ca9990c8c2
commit ad2529adb0

View file

@ -457,6 +457,88 @@ class SmartPlaylistTest(BeetsTestCase):
# Verify i2 is not duplicated
assert content.count(b"/item2.mp3") == 1
def test_playlist_update_dest_regen(self):
spl = SmartPlaylistPlugin()
i = MagicMock()
type(i).artist = PropertyMock(return_value="fake artist")
type(i).title = PropertyMock(return_value="fake title")
type(i).length = PropertyMock(return_value=300.123)
# Set a path which is not equal to the one returned by `item.destination`.
type(i).path = PropertyMock(return_value=b"/imported/path/with/dont/move/tagada.mp3")
# Set a path which would be equal to the one returned by `item.destination`.
type(i).destination = PropertyMock(return_value=lambda: b"/tagada.mp3")
i.evaluate_template.side_effect = lambda pl, _: pl.replace(
b"$title",
b"ta:ga:da",
).decode()
lib = Mock()
lib.replacements = CHAR_REPLACE
lib.items.return_value = [i]
lib.albums.return_value = []
q = Mock()
a_q = Mock()
pl = b"$title-my<playlist>.m3u", (q, None), (a_q, None)
spl._matched_playlists = [pl]
dir = bytestring_path(mkdtemp())
config["smartplaylist"]["output"] = "extm3u"
config["smartplaylist"]["prefix"] = "http://beets:8337/files"
config["smartplaylist"]["relative_to"] = False
config["smartplaylist"]["playlist_dir"] = fsdecode(dir)
# Test when `dest_regen` is set to True:
# Intended behavior is to use the path of `i.destination`.
config["smartplaylist"]["dest_regen"] = True
try:
spl.update_playlists(lib)
except Exception:
rmtree(syspath(dir))
raise
lib.items.assert_called_once_with(q, None)
lib.albums.assert_called_once_with(a_q, None)
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath)
with open(syspath(m3u_filepath), "rb") as f:
content = f.read()
rmtree(syspath(dir))
assert (
content
== b"#EXTM3U\n"
+ b"#EXTINF:300,fake artist - fake title\n"
+ b"http://beets:8337/files/tagada.mp3\n"
)
# Test when `dest_regen` is set to False:
# Intended behavior is to use the path of `i.path`.
config["smartplaylist"]["dest_regen"] = False
try:
spl.update_playlists(lib)
except Exception:
rmtree(syspath(dir))
raise
m3u_filepath = path.join(dir, b"ta_ga_da-my_playlist_.m3u")
self.assertExists(m3u_filepath)
with open(syspath(m3u_filepath), "rb") as f:
content = f.read()
rmtree(syspath(dir))
assert (
content
== b"#EXTM3U\n"
+ b"#EXTINF:300,fake artist - fake title\n"
+ b"http://beets:8337/files/imported/path/with/dont/move/tagada.mp3\n"
)
class SmartPlaylistCLITest(IOMixin, PluginTestCase):
plugin = "smartplaylist"