Anna Schumaker
1730b7e92c
I use a sql link table to accomplish this so a single album can be added to multiple album-artists. Additionally, I set up a view on Artists and Albums to make filtering easier without needing to use a complicated join every time. Additionally, I use the Playlist.add_children() function to set up a filter on the Album list model for each Artist's albums. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
236 lines
10 KiB
Python
236 lines
10 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""Tests our album Gio.ListModel."""
|
|
import pathlib
|
|
import unittest.mock
|
|
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)
|
|
|
|
def test_get_artists(self):
|
|
"""Test getting the list of artists for this album."""
|
|
with unittest.mock.patch.object(self.table, "get_artists",
|
|
return_value=[1, 2, 3]) as mock:
|
|
self.assertListEqual(self.album.get_artists(), [1, 2, 3])
|
|
mock.assert_called_with(self.album)
|
|
self.assertEqual(self.album.parent, 1)
|
|
|
|
|
|
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."""
|
|
artist = self.sql.artists.create("Test Artist")
|
|
album = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
artist.add_album(album)
|
|
|
|
self.assertTrue(album.delete())
|
|
self.assertIsNone(self.table.index(album))
|
|
self.assertFalse(artist.has_album(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)
|
|
|
|
cur = self.sql("SELECT COUNT(artistid) FROM album_artist_link")
|
|
self.assertEqual(cur.fetchone()["COUNT(artistid)"], 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)
|
|
|
|
def test_get_artists(self):
|
|
"""Test getting the list of artists an album is attached to."""
|
|
artist1 = self.sql.artists.create("Artist 1")
|
|
artist2 = self.sql.artists.create("Artist 2")
|
|
album = self.table.create("Test Album", "Album Artist", "2023-03")
|
|
|
|
artist1.add_album(album)
|
|
artist2.add_album(album)
|
|
self.assertListEqual(self.table.get_artists(album), [artist1, artist2])
|
|
|
|
del self.sql.artists.rows[artist1.artistid]
|
|
self.assertListEqual(self.table.get_artists(album), [artist2])
|