54 lines
2.0 KiB
Python
54 lines
2.0 KiB
Python
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from zyrarbot.music.history_entry import HistoryEntry
|
|
from zyrarbot.storage.history_repository import HistoryRepository
|
|
from zyrarbot.storage.json_file_store import JsonFileStore
|
|
|
|
|
|
class HistoryRepositoryTest(unittest.TestCase):
|
|
def test_history_is_stored_in_separate_files_and_legacy_data_is_migrated(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
legacy_path = Path(directory) / "history.json"
|
|
entry = HistoryEntry(
|
|
id="track-1",
|
|
title="Track",
|
|
url="https://example.test/track",
|
|
duration=10,
|
|
is_local=False,
|
|
filepath=None,
|
|
)
|
|
legacy_path.write_text(json.dumps({"10": [entry.to_dict()]}), encoding="utf-8")
|
|
repository = HistoryRepository(JsonFileStore(str(legacy_path)))
|
|
|
|
repository.migrate_legacy(10)
|
|
|
|
self.assertEqual(repository.list(10)[0].id, "track-1")
|
|
self.assertEqual(repository.list(20), [])
|
|
self.assertTrue((Path(directory) / "history" / "10.json").exists())
|
|
self.assertFalse((Path(directory) / "history" / "20.json").exists())
|
|
|
|
def test_new_entries_are_available_after_repository_reload(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
store = JsonFileStore(str(Path(directory) / "history.json"))
|
|
entry = HistoryEntry(
|
|
id="track-2",
|
|
title="Another track",
|
|
url="https://example.test/another-track",
|
|
)
|
|
|
|
HistoryRepository(store).add(42, entry)
|
|
|
|
reloaded = HistoryRepository(store)
|
|
self.assertEqual(reloaded.list(42), [entry])
|
|
persisted = json.loads(
|
|
(Path(directory) / "history" / "42.json").read_text(encoding="utf-8")
|
|
)
|
|
self.assertEqual(persisted["entries"][0]["id"], "track-2")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|