Anna Schumaker
1b38c4d6ec
This table allows us to work with Album playlists that have a name, album artist, release date, (optional) mbid, and (optional) cover. Note that we can insert multiple albums with the same name as long as their mbid, artist, or release date is different. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
207 lines
9.0 KiB
Python
207 lines
9.0 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""Tests our album Gio.ListModel."""
|
|
import pathlib
|
|
import emmental.db
|
|
import tests.util
|
|
|
|
|
|
class TestAlbumObject(tests.util.TestCase):
|
|
"""Tests our album object."""
|
|
|
|
def setUp(self):
|
|
"""Set up common variables."""
|
|
tests.util.TestCase.setUp(self)
|
|
self.table = self.sql.albums
|
|
self.album = emmental.db.albums.Album(table=self.table,
|
|
albumid=123, propertyid=456,
|
|
name="Test Album")
|
|
|
|
def test_init(self):
|
|
"""Test that the Artist is set up properly."""
|
|
self.assertIsInstance(self.album, emmental.db.playlist.Playlist)
|
|
self.assertEqual(self.album.table, self.table)
|
|
self.assertEqual(self.album.propertyid, 456)
|
|
self.assertEqual(self.album.albumid, 123)
|
|
self.assertEqual(self.album.primary_key, 123)
|
|
self.assertEqual(self.album.artist, "")
|
|
self.assertEqual(self.album.release, "")
|
|
self.assertEqual(self.album.mbid, "")
|
|
self.assertIsNone(self.album.cover)
|
|
self.assertIsNone(self.album.parent)
|
|
|
|
cover = pathlib.Path("/a/b/c.jpg")
|
|
album2 = emmental.db.albums.Album(table=self.table, propertyid=123,
|
|
albumid=456, mbid="ab-cd-ef",
|
|
artist="Test Artist", name="Album",
|
|
release="1988-06", cover=cover)
|
|
self.assertEqual(album2.artist, "Test Artist")
|
|
self.assertEqual(album2.release, "1988-06")
|
|
self.assertEqual(album2.mbid, "ab-cd-ef")
|
|
self.assertEqual(album2.cover, cover)
|
|
|
|
|
|
class TestAlbumTable(tests.util.TestCase):
|
|
"""Tests our album table."""
|
|
|
|
def setUp(self):
|
|
"""Set up common variables."""
|
|
tests.util.TestCase.setUp(self)
|
|
self.table = self.sql.albums
|
|
|
|
def test_init(self):
|
|
"""Test that the album model is configured correctly."""
|
|
self.assertIsInstance(self.table, emmental.db.playlist.Table)
|
|
self.assertEqual(len(self.table), 0)
|
|
|
|
def test_construct(self):
|
|
"""Test constructing an album playlist."""
|
|
album = self.table.construct(propertyid=1, albumid=1,
|
|
name="Test Album", mbid="ab-cd-ef",
|
|
artist="Test Artist", release="1988-06",
|
|
cover=tests.util.COVER_JPG)
|
|
self.assertIsInstance(album, emmental.db.albums.Album)
|
|
self.assertEqual(album.table, self.table)
|
|
self.assertEqual(album.propertyid, 1)
|
|
self.assertEqual(album.albumid, 1)
|
|
self.assertEqual(album.name, "Test Album")
|
|
self.assertEqual(album.artist, "Test Artist")
|
|
self.assertEqual(album.release, "1988-06")
|
|
self.assertEqual(album.mbid, "ab-cd-ef")
|
|
self.assertEqual(album.cover, tests.util.COVER_JPG)
|
|
self.assertFalse(album.active)
|
|
|
|
def test_create(self):
|
|
"""Test creating an album playlist."""
|
|
album1 = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
self.assertIsInstance(album1, emmental.db.albums.Album)
|
|
self.assertEqual(album1.name, "Test Album")
|
|
self.assertEqual(album1.artist, "Album Artist")
|
|
self.assertEqual(album1.release, "2023-03")
|
|
self.assertEqual(album1.mbid, "")
|
|
self.assertIsNone(album1.cover)
|
|
self.assertEqual(self.table[0], album1)
|
|
|
|
cur = self.sql("SELECT COUNT(name) FROM albums")
|
|
self.assertEqual(cur.fetchone()["COUNT(name)"], 1)
|
|
|
|
row = self.sql("""SELECT COUNT(*) FROM playlist_properties
|
|
WHERE propertyid=?""", album1.propertyid).fetchone()
|
|
self.assertEqual(row["COUNT(*)"], 1)
|
|
|
|
album2 = self.table.create("Test Album", "Album Artist", "1988-06",
|
|
cover=tests.util.COVER_JPG,
|
|
mbid="AB-CD-EF")
|
|
self.assertEqual(album2.artist, "Album Artist")
|
|
self.assertEqual(album2.release, "1988-06")
|
|
self.assertEqual(album2.mbid, "ab-cd-ef")
|
|
self.assertEqual(album2.cover, tests.util.COVER_JPG)
|
|
self.assertEqual(self.table[0], album2)
|
|
self.assertEqual(self.table[1], album1)
|
|
|
|
cur = self.sql("SELECT COUNT(name) FROM albums")
|
|
self.assertEqual(cur.fetchone()["COUNT(name)"], 2)
|
|
|
|
self.assertIsNone(self.table.create("Test Album",
|
|
"Album Artist", "2023-03"))
|
|
|
|
def test_delete(self):
|
|
"""Test deleting an album playlist."""
|
|
album = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
self.assertTrue(album.delete())
|
|
self.assertIsNone(self.table.index(album))
|
|
|
|
cur = self.sql("SELECT COUNT(name) FROM albums")
|
|
self.assertEqual(cur.fetchone()["COUNT(name)"], 0)
|
|
self.assertEqual(len(self.table), 0)
|
|
self.assertIsNone(self.table.get_item(0))
|
|
|
|
row = self.sql("""SELECT COUNT(*) FROM playlist_properties
|
|
WHERE propertyid=?""", album.propertyid).fetchone()
|
|
self.assertEqual(row["COUNT(*)"], 0)
|
|
|
|
self.assertFalse(album.delete())
|
|
|
|
def test_filter(self):
|
|
"""Test filtering an album playlist."""
|
|
self.table.create("Album 1", "Album Artist", "2023-03")
|
|
self.table.create("Album 2", "Album Artist", "2023-03")
|
|
|
|
self.table.filter("*1", now=True)
|
|
self.assertSetEqual(self.table.get_filter().keys, {1})
|
|
self.table.filter("album*", now=True)
|
|
self.assertSetEqual(self.table.get_filter().keys, {1, 2})
|
|
|
|
def test_get_sort_key(self):
|
|
"""Test the get_sort_key() function."""
|
|
album1 = self.table.create("Album 1", "Album Artist", "2023-02")
|
|
album2 = self.table.create("Album 2", "Album Artist", "2023-02",
|
|
mbid="ab-cd-ef")
|
|
|
|
self.assertTupleEqual(self.table.get_sort_key(album1),
|
|
(("album", "1"), True, "",
|
|
("album", "artist"), "2023-02"))
|
|
self.assertTupleEqual(self.table.get_sort_key(album2),
|
|
(("album", "2"), False, "ab-cd-ef",
|
|
("album", "artist"), "2023-02"))
|
|
|
|
def test_load(self):
|
|
"""Test loading the album table."""
|
|
self.table.create("Album 1", "Album Artist", "2023-03")
|
|
self.table.create("Album 2", "Album Artist", "2023-03",
|
|
mbid="ab-cd-ef", cover=tests.util.COVER_JPG)
|
|
|
|
albums2 = emmental.db.albums.Table(self.sql)
|
|
self.assertEqual(len(albums2), 0)
|
|
|
|
albums2.load(now=True)
|
|
self.assertEqual(len(albums2), 2)
|
|
|
|
self.assertEqual(albums2.get_item(0).name, "Album 1")
|
|
self.assertEqual(albums2.get_item(0).artist, "Album Artist")
|
|
self.assertEqual(albums2.get_item(0).release, "2023-03")
|
|
self.assertEqual(albums2.get_item(0).mbid, "")
|
|
self.assertIsNone(albums2.get_item(0).cover)
|
|
|
|
self.assertEqual(albums2.get_item(1).name, "Album 2")
|
|
self.assertEqual(albums2.get_item(1).artist, "Album Artist")
|
|
self.assertEqual(albums2.get_item(1).release, "2023-03")
|
|
self.assertEqual(albums2.get_item(1).mbid, "ab-cd-ef")
|
|
self.assertEqual(albums2.get_item(1).cover,
|
|
tests.util.COVER_JPG)
|
|
|
|
def test_lookup(self):
|
|
"""Test looking up album playlists."""
|
|
album1 = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
album2 = self.table.create("Test Album", "Album Artist", "2023-03",
|
|
mbid="ab-cd-ef")
|
|
|
|
self.assertEqual(self.table.lookup("Test Album", "Album Artist",
|
|
"2023-03"), album1)
|
|
self.assertEqual(self.table.lookup("test album", "album artist",
|
|
"2023-03"), album1)
|
|
self.assertEqual(self.table.lookup("Test Album", "Album Artist",
|
|
"2023-03", mbid="ab-cd-ef"), album2)
|
|
self.assertEqual(self.table.lookup("test album", "album artist",
|
|
"2023-03", mbid="ab-cd-ef"), album2)
|
|
self.assertIsNone(self.table.lookup("No Album", "No Artist", "0000"))
|
|
|
|
self.assertEqual(self.table.lookup(mbid="ab-cd-ef"), album2)
|
|
self.assertEqual(self.table.lookup(mbid="AB-CD-EF"), album2)
|
|
self.assertIsNone(self.table.lookup(mbid="gh-ij-kl"))
|
|
|
|
def test_update(self):
|
|
"""Test updating album attributes."""
|
|
album = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
album.cover = tests.util.COVER_JPG
|
|
album.active = True
|
|
|
|
row = self.sql("SELECT cover, active FROM albums_view WHERE albumid=?",
|
|
album.albumid).fetchone()
|
|
self.assertEqual(row["cover"], tests.util.COVER_JPG)
|
|
self.assertEqual(row["active"], True)
|
|
|
|
album.cover = None
|
|
row = self.sql("SELECT cover FROM albums WHERE albumid=?",
|
|
album.albumid).fetchone()
|
|
self.assertIsNone(row["cover"], tests.util.COVER_JPG)
|