db: Give Artists a PlaylistState property
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
3abf2d3cf7
commit
e9a2a6f06a
23
db/artist.py
23
db/artist.py
|
@ -1,23 +1,29 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: artists
|
||||
# +----------+------+------+
|
||||
# | artistid | name | sort |
|
||||
# +----------+------+------+
|
||||
# +----------+-----------+------+------+
|
||||
# | artistid | plstateid | name | sort |
|
||||
# +----------+-----------+------+------+
|
||||
#
|
||||
# Index: artist_index
|
||||
# +------------------+
|
||||
# | name -> artistid |
|
||||
# +------------------+
|
||||
from gi.repository import GObject
|
||||
from . import album
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
class Artist(objects.Tag):
|
||||
def do_get_column(self, column):
|
||||
return execute(f"SELECT {column} FROM artists "
|
||||
"WHERE artistid=?", [ self.rowid ])
|
||||
|
||||
@GObject.Property
|
||||
def playlist_state(self):
|
||||
return state.PlaylistState(self.get_column("plstateid"))
|
||||
|
||||
def albums(self):
|
||||
cursor = execute(f"SELECT albumid FROM albums "
|
||||
"WHERE artistid=?", [ self.rowid ])
|
||||
|
@ -31,16 +37,21 @@ class ArtistTable(objects.Table):
|
|||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS artists "
|
||||
"(artistid INTEGER PRIMARY KEY, "
|
||||
" plstateid INTEGER NOT NULL, "
|
||||
" name TEXT UNIQUE, "
|
||||
" sort TEXT)")
|
||||
" sort TEXT, "
|
||||
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid))")
|
||||
execute("CREATE INDEX IF NOT EXISTS artist_index "
|
||||
"ON artists(name)")
|
||||
|
||||
def do_insert(self, name, sort):
|
||||
return execute("INSERT INTO artists (name, sort) "
|
||||
"VALUES (?, ?)", (name, sort.casefold()))
|
||||
plstate = state.Table.insert(random=False, loop=False)
|
||||
return execute("INSERT INTO artists (plstateid, name, sort) "
|
||||
"VALUES (?, ?, ?)",
|
||||
[ int(plstate), name, sort.casefold() ])
|
||||
|
||||
def do_delete(self, artist):
|
||||
state.Table.delete(artist.playlist_state)
|
||||
return execute("DELETE FROM artists WHERE artistid=?", [ int(artist) ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
|
|
|
@ -17,7 +17,7 @@ class TestArtistTable(unittest.TestCase):
|
|||
|
||||
def test_artist_table_init(self):
|
||||
self.assertIsInstance(db.artist.Table, db.artist.ArtistTable)
|
||||
db.execute("SELECT artistid,name,sort FROM artists")
|
||||
db.execute("SELECT artistid,plstateid,name,sort FROM artists")
|
||||
|
||||
def test_artist_table_insert(self):
|
||||
db.artist.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
@ -29,16 +29,19 @@ class TestArtistTable(unittest.TestCase):
|
|||
|
||||
self.assertEqual(artist.name, "Test Artist")
|
||||
self.assertEqual(artist.sort, "test sort")
|
||||
self.assertIsInstance(artist.playlist_state, db.state.PlaylistState)
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
|
||||
def test_artist_table_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
state = artist.playlist_state
|
||||
db.artist.Table.connect("row-deleted", self.on_row_deleted)
|
||||
|
||||
db.artist.Table.delete(artist)
|
||||
self.assertIsNone(db.artist.Table.lookup("Test Artist"))
|
||||
self.assertIsNone(db.state.Table.get(int(state)))
|
||||
|
||||
def test_artist_table_get(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
|
|
Loading…
Reference in New Issue