97 lines
4.4 KiB
Python
97 lines
4.4 KiB
Python
import io
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
|
|
class ExportBundlesTests(unittest.TestCase):
|
|
def _prepare_playlist_dir(self, root: Path, dirname: str) -> Path:
|
|
playlist_dir = root / dirname
|
|
covers_dir = playlist_dir / "covers"
|
|
covers_dir.mkdir(parents=True, exist_ok=True)
|
|
(playlist_dir / "playlist.yaml").write_text("playlist_id: 1\n", encoding="utf-8")
|
|
(playlist_dir / ".playlist_meta.json").write_text('{"playlist_id": 1}', encoding="utf-8")
|
|
(covers_dir / "playlist-cover.jpg").write_bytes(b"cover")
|
|
return playlist_dir
|
|
|
|
def test_create_single_playlist_bundle_includes_playlist_dir_files(self):
|
|
from musicdl.catalogsync.export_bundles import create_single_playlist_bundle
|
|
|
|
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
|
|
root = Path(tmpdir)
|
|
playlist_dir = self._prepare_playlist_dir(root, "Playlist One_101")
|
|
bundle_root = root / "bundles"
|
|
|
|
bundle_path = create_single_playlist_bundle(
|
|
playlist_dir=playlist_dir,
|
|
bundle_root=bundle_root,
|
|
platform="qq",
|
|
playlist_id=101,
|
|
playlist_name="Playlist One",
|
|
)
|
|
|
|
self.assertTrue(bundle_path.exists())
|
|
self.assertEqual("playlist-qq-101-Playlist One.zip", bundle_path.name)
|
|
self.assertEqual(bundle_root.resolve(), bundle_path.parent.resolve())
|
|
with zipfile.ZipFile(bundle_path, "r") as archive:
|
|
members = set(archive.namelist())
|
|
self.assertIn("Playlist One_101/playlist.yaml", members)
|
|
self.assertIn("Playlist One_101/.playlist_meta.json", members)
|
|
self.assertIn("Playlist One_101/covers/playlist-cover.jpg", members)
|
|
|
|
def test_create_multi_playlist_bundle_wraps_under_playlists_root(self):
|
|
from musicdl.catalogsync.export_bundles import create_multi_playlist_bundle
|
|
|
|
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
|
|
root = Path(tmpdir)
|
|
playlist_dir_a = self._prepare_playlist_dir(root, "Playlist A_201")
|
|
playlist_dir_b = self._prepare_playlist_dir(root, "Playlist B_202")
|
|
bundle_root = root / "bundles"
|
|
|
|
bundle_path = create_multi_playlist_bundle(
|
|
playlist_dirs=[playlist_dir_a, playlist_dir_b],
|
|
bundle_root=bundle_root,
|
|
created_at=datetime(2026, 4, 19, 12, 34, 56),
|
|
)
|
|
|
|
self.assertTrue(bundle_path.exists())
|
|
self.assertTrue(bundle_path.name.endswith("playlists-export-20260419-123456.zip"))
|
|
self.assertEqual(bundle_root.resolve(), bundle_path.parent.resolve())
|
|
with zipfile.ZipFile(io.BytesIO(bundle_path.read_bytes()), "r") as archive:
|
|
members = set(archive.namelist())
|
|
self.assertIn("playlists/Playlist A_201/playlist.yaml", members)
|
|
self.assertIn("playlists/Playlist B_202/.playlist_meta.json", members)
|
|
|
|
def test_create_multi_playlist_bundle_uses_unique_storage_path_for_same_timestamp(self):
|
|
from musicdl.catalogsync.export_bundles import create_multi_playlist_bundle
|
|
|
|
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as tmpdir:
|
|
root = Path(tmpdir)
|
|
playlist_dir_a = self._prepare_playlist_dir(root, "Playlist A_201")
|
|
playlist_dir_b = self._prepare_playlist_dir(root, "Playlist B_202")
|
|
bundle_root = root / "bundles"
|
|
created_at = datetime(2026, 4, 19, 12, 34, 56)
|
|
|
|
first_path = create_multi_playlist_bundle(
|
|
playlist_dirs=[playlist_dir_a, playlist_dir_b],
|
|
bundle_root=bundle_root,
|
|
created_at=created_at,
|
|
)
|
|
second_path = create_multi_playlist_bundle(
|
|
playlist_dirs=[playlist_dir_a, playlist_dir_b],
|
|
bundle_root=bundle_root,
|
|
created_at=created_at,
|
|
)
|
|
|
|
self.assertNotEqual(first_path, second_path)
|
|
self.assertTrue(first_path.exists())
|
|
self.assertTrue(second_path.exists())
|
|
self.assertTrue(first_path.name.endswith("playlists-export-20260419-123456.zip"))
|
|
self.assertTrue(second_path.name.endswith("playlists-export-20260419-123456.zip"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|