db: Create a function to find playlists based on state id
So we can write the current playlist stateid to the settings file and look it up later. Implements: #36 (Find playlists based on playlist state id) Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
6b5b2a745e
commit
c658e873a6
|
@ -21,6 +21,31 @@ from . import track
|
|||
from . import state
|
||||
from . import user
|
||||
|
||||
from . import sql
|
||||
def _search_table(table, key, search):
|
||||
if row := sql.execute(f"SELECT {key} FROM {table} "
|
||||
f"WHERE plstateid=?", [ search ]).fetchone():
|
||||
return row[0]
|
||||
|
||||
def find_playlist(plstateid):
|
||||
if playlistid := _search_table("playlists", "playlistid", plstateid):
|
||||
return user.Table.get(playlistid)
|
||||
if artistid := _search_table("artists", "artistid", plstateid):
|
||||
return artist.Table.get(artistid)
|
||||
if albumid := _search_table("albums", "albumid", plstateid):
|
||||
return album.Table.get(albumid)
|
||||
if discid := _search_table("discs", "discid", plstateid):
|
||||
return disc.Table.get(discid)
|
||||
if genreid := _search_table("genres", "genreid", plstateid):
|
||||
return genre.Table.get(genreid)
|
||||
if decadeid := _search_table("decades", "decadeid", plstateid):
|
||||
return decade.Table.get(decadeid)
|
||||
if yearid := _search_table("years", "yearid", plstateid):
|
||||
return year.Table.get(yearid)
|
||||
if libraryid := _search_table("libraries", "libraryid", plstateid):
|
||||
return library.Table.get(libraryid)
|
||||
return None
|
||||
|
||||
def make_fake_track(trackno, length, title, path, lib="/a/b/c", art="Test Artist",
|
||||
alb="Test Album", disk=1, subtitle=None, yeer=2021):
|
||||
lib = library.Table.find(pathlib.Path(lib))
|
||||
|
|
|
@ -4,6 +4,17 @@ import unittest
|
|||
|
||||
class TestDB(unittest.TestCase):
|
||||
def test_new_db(self):
|
||||
db.reset()
|
||||
self.assertTrue(db.new_db())
|
||||
db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
|
||||
self.assertFalse(db.new_db())
|
||||
|
||||
def test_find_playlist(self):
|
||||
track = db.make_fake_track(1, 1, "Test Track", "/a/b/c/1.ogg")
|
||||
collection = db.user.Table.find("Collection")
|
||||
genre = db.genre.Table.find("Test Genre")
|
||||
|
||||
self.assertIsNone(db.find_playlist(123456))
|
||||
for plist in [ collection, track.artist, track.album, track.disc,
|
||||
genre, track.decade, track.year, track.library ]:
|
||||
self.assertEqual(db.find_playlist(plist.plist_state.rowid), plist)
|
||||
|
|
Loading…
Reference in New Issue