emmental/tests/db/test_playlists.py
Anna Schumaker d57509425b db: Add a Playlists Table
This inherits from our base playlist Table class, and implements
functions for creating and renaming playlists. Additionally, the
Playlist object allows for setting a custom image to display as an icon
in the sidebar.

Finally, I add in a custom sqlite3 adapter and converter to support
pathlib.Path types in the database.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 10:44:34 -04:00

260 lines
10 KiB
Python

# Copyright 2022 (c) Anna Schumaker
"""Tests our playlist Gio.ListModel."""
import pathlib
import unittest.mock
import emmental.db
import tests.util
class TestPlaylistObject(tests.util.TestCase):
"""Tests our playlist object."""
def setUp(self):
"""Set up common variables."""
super().setUp()
self.table = self.sql.playlists
self.playlist = emmental.db.playlists.Playlist(table=self.table,
playlistid=12345,
propertyid=67890,
name="Test Playlist")
def test_init(self):
"""Test that the Playlist is set up properly."""
self.assertIsInstance(self.playlist, emmental.db.playlist.Playlist)
self.assertEqual(self.playlist.table, self.table)
self.assertEqual(self.playlist.propertyid, 67890)
self.assertEqual(self.playlist.playlistid, 12345)
self.assertEqual(self.playlist.primary_key, 12345)
self.assertEqual(self.playlist.name, "Test Playlist")
self.assertIsNone(self.playlist.image)
self.assertIsNone(self.playlist.parent)
def test_image_path(self):
"""Test the image-path property."""
path = pathlib.Path("/a/b/c.jpg")
playlist = emmental.db.playlists.Playlist(table=self.table,
playlistid=1, propertyid=1,
image=path, name="Test")
self.assertEqual(playlist.image, path)
def test_rename(self):
"""Test the rename() function."""
with unittest.mock.patch.object(self.table, "rename",
return_value=True) as mock_rename:
self.assertTrue(self.playlist.rename("New Name"))
mock_rename.assert_called_with(self.playlist, "New Name")
class TestPlaylistTable(tests.util.TestCase):
"""Tests our playlist table."""
def setUp(self):
"""Set up common variables."""
super().setUp()
self.sql("DELETE FROM playlists")
self.table = self.sql.playlists
def test_init(self):
"""Test that the playlist model is configured correctly."""
self.assertIsInstance(self.table, emmental.db.playlist.Table)
self.assertEqual(len(self.table), 0)
self.assertIsNone(self.table.collection)
self.assertIsNone(self.table.favorites)
self.assertIsNone(self.table.most_played)
self.assertIsNone(self.table.new_tracks)
self.assertIsNone(self.table.previous)
self.assertIsNone(self.table.queued)
self.assertIsNone(self.table.unplayed)
def test_construct(self):
"""Test constructing a playlist."""
playlist = self.table.construct(playlistid=1, propertyid=1,
name="Test Playlist")
self.assertIsInstance(playlist, emmental.db.playlists.Playlist)
self.assertEqual(playlist.table, self.table)
self.assertEqual(playlist.propertyid, 1)
self.assertEqual(playlist.playlistid, 1)
self.assertEqual(playlist.name, "Test Playlist")
self.assertIsNone(playlist.image)
def test_create(self):
"""Test creating a playlist."""
playlist = self.table.create(" Test Playlist ")
self.assertIsInstance(playlist, emmental.db.playlists.Playlist)
self.assertEqual(playlist.name, "Test Playlist")
self.assertIsNone(playlist.image)
cur = self.sql("SELECT COUNT(name) FROM playlists")
self.assertEqual(cur.fetchone()["COUNT(name)"], 1)
self.assertEqual(len(self.table), 1)
self.assertEqual(self.table.get_item(0), playlist)
cur = self.sql("SELECT COUNT(*) FROM playlist_properties")
self.assertEqual(cur.fetchone()["COUNT(*)"], 1)
for name in ["", " ", "Test Playlist", "test playlist"]:
self.assertIsNone(self.table.create(name))
self.assertEqual(len(self.table), 1)
cur = self.sql("SELECT COUNT(rowid) FROM playlists")
self.assertEqual(cur.fetchone()["COUNT(rowid)"], 1)
def test_delete(self):
"""Test deleting a playlist."""
playlist = self.table.create("Test Playlist")
self.assertTrue(playlist.delete())
self.assertIsNone(self.table.index(playlist))
cur = self.sql("SELECT COUNT(name) FROM playlists")
self.assertEqual(cur.fetchone()["COUNT(name)"], 0)
self.assertEqual(len(self.table), 0)
self.assertIsNone(self.table.get_item(0))
cur = self.sql("SELECT COUNT(*) FROM playlist_properties")
self.assertEqual(cur.fetchone()["COUNT(*)"], 0)
self.assertFalse(playlist.delete())
def test_filter(self):
"""Test filtering the playlist model."""
self.table.create("Playlist 1")
self.table.create("Playlist 2")
self.table.filter("*1", now=True)
self.assertSetEqual(self.table.get_filter().keys, {1})
self.table.filter("playlist*", now=True)
self.assertSetEqual(self.table.get_filter().keys, {1, 2})
def test_load(self):
"""Test loading playlists from the database."""
self.table.create("Playlist 1").image = tests.util.COVER_JPG
self.table.create("Playlist 2")
playlists2 = emmental.db.playlists.Table(self.sql)
playlists2.load(now=True)
self.assertEqual(len(playlists2), 2)
self.assertEqual(playlists2[0].name, "Playlist 1")
self.assertEqual(playlists2[0].image, tests.util.COVER_JPG)
self.assertEqual(playlists2[1].name, "Playlist 2")
self.assertIsNone(playlists2[1].image)
def test_lookup(self):
"""Test looking up a playlist."""
playlist = self.table.create("Test Playlist")
self.assertEqual(self.table.lookup("Test Playlist"), playlist)
self.assertEqual(self.table.lookup("test playlist"), playlist)
self.assertIsNone(self.table.lookup("No Playlist"))
def test_rename(self):
"""Test renaming a playlist."""
playlist = self.table.create("Test Playlist")
self.table.store.append = unittest.mock.Mock()
self.table.store.remove = unittest.mock.Mock()
self.assertTrue(playlist.rename(" New Name "))
self.assertEqual(playlist.name, "New Name")
self.table.store.remove.assert_called_with(playlist)
self.table.store.append.assert_called_with(playlist)
rows = self.sql("SELECT name FROM playlists").fetchall()
self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["name"], "New Name")
self.table.create("Other Name")
self.assertFalse(playlist.rename("New Name"))
self.assertFalse(playlist.rename("Other Name"))
def test_update(self):
"""Test updating playlist properties."""
playlist = self.table.create("Test Playlist")
playlist.image = tests.util.COVER_JPG
playlist.active = True
cur = self.sql("""SELECT image, active FROM playlists_view
WHERE playlistid=?""", playlist.playlistid)
row = cur.fetchone()
self.assertEqual(row["image"], tests.util.COVER_JPG)
self.assertTrue(row["active"])
class TestSystemPlaylists(tests.util.TestCase):
"""Tests our system playlists."""
def setUp(self):
"""Set up common variables."""
super().setUp()
self.table = self.sql.playlists
self.table.load(now=True)
def test_collection(self):
"""Test the Collection playlist."""
self.assertIsInstance(self.table.collection,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.collection.name, "Collection")
self.assertTrue(self.table.collection.active)
self.assertEqual(self.table.lookup("Collection"),
self.table.collection)
def test_favorites(self):
"""Test the favorite tracks playlist."""
self.assertIsInstance(self.table.favorites,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.favorites.name, "Favorite Tracks")
self.assertFalse(self.table.favorites.active)
self.assertEqual(self.table.lookup("Favorite Tracks"),
self.table.favorites)
def test_most_played(self):
"""Test the most-played tracks playlist."""
self.assertIsInstance(self.table.most_played,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.most_played.name, "Most Played Tracks")
self.assertFalse(self.table.most_played.active)
self.assertEqual(self.table.lookup("Most Played Tracks"),
self.table.most_played)
def test_new_tracks(self):
"""Test the new tracks playlist."""
self.assertIsInstance(self.table.new_tracks,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.new_tracks.name, "New Tracks")
self.assertFalse(self.table.new_tracks.active)
self.assertEqual(self.table.lookup("New Tracks"),
self.table.new_tracks)
def test_previous(self):
"""Test the previous tracks playlist."""
self.assertIsInstance(self.table.previous,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.previous.name, "Previous Tracks")
self.assertFalse(self.table.previous.active)
self.assertEqual(self.table.lookup("Previous Tracks"),
self.table.previous)
def test_queued(self):
"""Test the queued tracks playlist."""
self.assertIsInstance(self.table.queued,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.queued.name, "Queued Tracks")
self.assertFalse(self.table.queued.active)
self.assertEqual(self.table.lookup("Queued Tracks"),
self.table.queued)
def test_unplayed(self):
"""Test the unplayed tracks playlist."""
self.assertIsInstance(self.table.unplayed,
emmental.db.playlists.Playlist)
self.assertEqual(self.table.unplayed.name, "Unplayed Tracks")
self.assertFalse(self.table.unplayed.active)
self.assertEqual(self.table.lookup("Unplayed Tracks"),
self.table.unplayed)