158 lines
6.1 KiB
Python
158 lines
6.1 KiB
Python
|
# Copyright 2022 (c) Anna Schumaker.
|
||
|
"""Tests our artist Gio.ListModel."""
|
||
|
import emmental.db
|
||
|
import tests.util
|
||
|
|
||
|
|
||
|
class TestArtistObject(tests.util.TestCase):
|
||
|
"""Tests our artist object."""
|
||
|
|
||
|
def setUp(self):
|
||
|
"""Set up common variables."""
|
||
|
tests.util.TestCase.setUp(self)
|
||
|
self.table = self.sql.artists
|
||
|
self.artist = emmental.db.artists.Artist(table=self.table,
|
||
|
artistid=123, propertyid=456,
|
||
|
name="Test Artist")
|
||
|
|
||
|
def test_init(self):
|
||
|
"""Test that the Artist is set up properly."""
|
||
|
self.assertIsInstance(self.artist, emmental.db.playlist.Playlist)
|
||
|
self.assertEqual(self.artist.table, self.table)
|
||
|
self.assertEqual(self.artist.propertyid, 456)
|
||
|
self.assertEqual(self.artist.artistid, 123)
|
||
|
self.assertEqual(self.artist.primary_key, 123)
|
||
|
self.assertEqual(self.artist.mbid, "")
|
||
|
self.assertIsNone(self.artist.parent)
|
||
|
|
||
|
|
||
|
class TestArtistTable(tests.util.TestCase):
|
||
|
"""Tests our artist table."""
|
||
|
|
||
|
def setUp(self):
|
||
|
"""Set up common variables."""
|
||
|
tests.util.TestCase.setUp(self)
|
||
|
self.table = self.sql.artists
|
||
|
|
||
|
def test_init(self):
|
||
|
"""Test that the artist model is configured correctly."""
|
||
|
self.assertIsInstance(self.table, emmental.db.playlist.Table)
|
||
|
self.assertEqual(len(self.table), 0)
|
||
|
|
||
|
def test_construct(self):
|
||
|
"""Test constructing a new artist playlist."""
|
||
|
artist = self.table.construct(artistid=1, propertyid=1,
|
||
|
name="Test Artist", mbid="ab-cd-ef")
|
||
|
self.assertIsInstance(artist, emmental.db.artists.Artist)
|
||
|
self.assertEqual(artist.table, self.table)
|
||
|
self.assertEqual(artist.propertyid, 1)
|
||
|
self.assertEqual(artist.artistid, 1)
|
||
|
self.assertEqual(artist.name, "Test Artist")
|
||
|
self.assertEqual(artist.mbid, "ab-cd-ef")
|
||
|
self.assertFalse(artist.active)
|
||
|
|
||
|
def test_create(self):
|
||
|
"""Test creating an artist playlist."""
|
||
|
artist1 = self.table.create("Test Artist")
|
||
|
self.assertIsInstance(artist1, emmental.db.artists.Artist)
|
||
|
self.assertEqual(artist1.name, "Test Artist")
|
||
|
self.assertEqual(artist1.mbid, "")
|
||
|
self.assertEqual(self.table[0], artist1)
|
||
|
|
||
|
cur = self.sql("SELECT COUNT(name) FROM artists")
|
||
|
self.assertEqual(cur.fetchone()["COUNT(name)"], 1)
|
||
|
|
||
|
row = self.sql("""SELECT COUNT(*) FROM playlist_properties
|
||
|
WHERE propertyid=?""", artist1.propertyid).fetchone()
|
||
|
self.assertEqual(row["COUNT(*)"], 1)
|
||
|
|
||
|
artist2 = self.table.create("Test Artist", mbid="AB-CD-EF")
|
||
|
self.assertEqual(artist2.mbid, "ab-cd-ef")
|
||
|
self.assertEqual(self.table[0], artist2)
|
||
|
self.assertEqual(self.table[1], artist1)
|
||
|
|
||
|
cur = self.sql("SELECT COUNT(name) FROM artists")
|
||
|
self.assertEqual(cur.fetchone()["COUNT(name)"], 2)
|
||
|
|
||
|
self.assertIsNone(self.table.create("Test Artist"))
|
||
|
|
||
|
def test_delete(self):
|
||
|
"""Test deleting an artist playlist."""
|
||
|
artist = self.table.create("Test Artist")
|
||
|
self.assertTrue(artist.delete())
|
||
|
self.assertIsNone(self.table.index(artist))
|
||
|
|
||
|
cur = self.sql("SELECT COUNT(name) FROM artists")
|
||
|
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=?""", artist.propertyid).fetchone()
|
||
|
self.assertEqual(row["COUNT(*)"], 0)
|
||
|
|
||
|
self.assertFalse(artist.delete())
|
||
|
|
||
|
def test_filter(self):
|
||
|
"""Test filtering an artist playlist."""
|
||
|
self.table.create("Artist 1")
|
||
|
self.table.create("Artist 2")
|
||
|
|
||
|
self.table.filter("*1", now=True)
|
||
|
self.assertSetEqual(self.table.get_filter().keys, {1})
|
||
|
self.table.filter("artist*", now=True)
|
||
|
self.assertSetEqual(self.table.get_filter().keys, {1, 2})
|
||
|
|
||
|
def test_get_sort_key(self):
|
||
|
"""Test the get_sort_key() function."""
|
||
|
artist1 = self.table.create("Artist 1")
|
||
|
artist2 = self.table.create("Artist 2", mbid="ab-cd-ef")
|
||
|
|
||
|
self.assertTupleEqual(self.table.get_sort_key(artist1),
|
||
|
(("artist", "1"), True, ""))
|
||
|
self.assertTupleEqual(self.table.get_sort_key(artist2),
|
||
|
(("artist", "2"), False, "ab-cd-ef"))
|
||
|
|
||
|
def test_load(self):
|
||
|
"""Test loading the artist table."""
|
||
|
self.table.create("Artist 1")
|
||
|
self.table.create("Artist 2", mbid="ab-cd-ef")
|
||
|
|
||
|
artists2 = emmental.db.artists.Table(self.sql)
|
||
|
self.assertEqual(len(artists2), 0)
|
||
|
|
||
|
artists2.load(now=True)
|
||
|
self.assertEqual(len(artists2), 2)
|
||
|
|
||
|
self.assertEqual(artists2.get_item(0).name, "Artist 1")
|
||
|
self.assertEqual(artists2.get_item(0).mbid, "")
|
||
|
|
||
|
self.assertEqual(artists2.get_item(1).name, "Artist 2")
|
||
|
self.assertEqual(artists2.get_item(1).mbid, "ab-cd-ef")
|
||
|
|
||
|
def test_lookup(self):
|
||
|
"""Test looking up artist playlists."""
|
||
|
artist1 = self.table.create("Test Artist")
|
||
|
artist2 = self.table.create("Test Artist", mbid="ab-cd-ef")
|
||
|
|
||
|
self.assertEqual(self.table.lookup("Test Artist"), artist1)
|
||
|
self.assertEqual(self.table.lookup("test artist"), artist1)
|
||
|
self.assertEqual(self.table.lookup("Test Artist", mbid="ab-cd-ef"),
|
||
|
artist2)
|
||
|
self.assertEqual(self.table.lookup("test artist", mbid="AB-CD-EF"),
|
||
|
artist2)
|
||
|
self.assertIsNone(self.table.lookup("No Artist"))
|
||
|
|
||
|
self.assertEqual(self.table.lookup(mbid="ab-cd-ef"), artist2)
|
||
|
self.assertEqual(self.table.lookup(mbid="AB-CD-EF"), artist2)
|
||
|
self.assertIsNone(self.table.lookup(mbid="gh-ij-kl"))
|
||
|
|
||
|
def test_update(self):
|
||
|
"""Test updating artist attributes."""
|
||
|
artist = self.table.create("Test Artist")
|
||
|
artist.active = True
|
||
|
|
||
|
row = self.sql("""SELECT active FROM playlist_properties
|
||
|
WHERE propertyid=?""", artist.propertyid).fetchone()
|
||
|
self.assertTrue(row["active"])
|