From ad2529adb0203cf0ce2ea4063647fc33de60489d Mon Sep 17 00:00:00 2001 From: Pierre Ayoub Date: Sat, 22 Mar 2025 10:18:32 +0100 Subject: [PATCH] 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` --- test/plugins/test_smartplaylist.py | 82 ++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/plugins/test_smartplaylist.py b/test/plugins/test_smartplaylist.py index d1125158f..64994b320 100644 --- a/test/plugins/test_smartplaylist.py +++ b/test/plugins/test_smartplaylist.py @@ -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.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"