db: Give Playlists a PlaylistState property
And clean up how the default playlists are created so the test doesn't fall over with the new column. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
8a0364760d
commit
e94346fdd9
|
@ -43,3 +43,8 @@ def reset():
|
|||
genre.Map.reset()
|
||||
playlist.Map.reset()
|
||||
playlist.TempMap.reset()
|
||||
playlist.Table.create_default_playlists()
|
||||
|
||||
|
||||
if lib.version.TESTING: reset()
|
||||
playlist.Table.create_default_playlists()
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: playlists
|
||||
# +------ ---+------+------+
|
||||
# | playlistid | name | sort |
|
||||
# +-------- -+------+------+
|
||||
# +------ ---+-----------+------+------+
|
||||
# | playlistid | plstateid | name | sort |
|
||||
# +-------- -+-----------+------+------+
|
||||
#
|
||||
# Index: playlist_index
|
||||
# +--------------------+
|
||||
# | name -> playlistid |
|
||||
# +--------------------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from . import executemany
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import track
|
||||
|
||||
Default = [ "Collection", "Favorites", "New Tracks",
|
||||
|
@ -23,6 +25,10 @@ class Playlist(objects.Tag):
|
|||
return execute(f"SELECT {column} FROM playlists "
|
||||
"WHERE playlistid=?", [ self.rowid ])
|
||||
|
||||
@GObject.Property
|
||||
def playlist_state(self):
|
||||
return state.PlaylistState(self.get_column("plstateid"))
|
||||
|
||||
|
||||
class PlaylistTable(objects.Table):
|
||||
def __init__(self):
|
||||
|
@ -31,17 +37,17 @@ class PlaylistTable(objects.Table):
|
|||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS playlists "
|
||||
"(playlistid 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 playlist_index "
|
||||
"ON playlists(name)")
|
||||
executemany("INSERT OR IGNORE INTO playlists (name, sort) "
|
||||
"VALUES (?, ?)",
|
||||
[ (p, p.casefold()) for p in Default ])
|
||||
|
||||
def do_insert(self, name):
|
||||
return execute("INSERT INTO playlists (name, sort) "
|
||||
"VALUES (?, ?)", (name, name.casefold()))
|
||||
def do_insert(self, name, loop=False):
|
||||
plstate = state.Table.insert(random=False, loop=loop)
|
||||
return execute("INSERT INTO playlists (plstateid, name, sort) "
|
||||
"VALUES (?, ?, ?)", (int(plstate), name, name.casefold()))
|
||||
|
||||
def do_delete(self, playlist):
|
||||
Map.delete_playlist(playlist)
|
||||
|
@ -56,6 +62,12 @@ class PlaylistTable(objects.Table):
|
|||
return execute("SELECT playlistid FROM playlists "
|
||||
"WHERE name=?", [ name ])
|
||||
|
||||
def create_default_playlists(self):
|
||||
for name in Default:
|
||||
if (plist := self.lookup(name)) != None:
|
||||
continue
|
||||
self.do_insert(name, name == "Collection")
|
||||
|
||||
|
||||
class PlaylistMap(objects.Map):
|
||||
def __init__(self, temp=False):
|
||||
|
|
|
@ -4,11 +4,11 @@ import sqlite3
|
|||
import unittest
|
||||
from gi.repository import GObject
|
||||
|
||||
expected = [ (1, "Collection", "collection"),
|
||||
(2, "Favorites", "favorites"),
|
||||
(3, "New Tracks", "new tracks"),
|
||||
(4, "Previous", "previous"),
|
||||
(5, "Queued Tracks", "queued tracks") ]
|
||||
expected = [ (1, 1, "Collection", "collection", 0, 1),
|
||||
(2, 2, "Favorites", "favorites", 0, 0),
|
||||
(3, 3, "New Tracks", "new tracks", 0, 0),
|
||||
(4, 4, "Previous", "previous", 0, 0),
|
||||
(5, 5, "Queued Tracks", "queued tracks", 0, 0) ]
|
||||
|
||||
|
||||
class TestPlaylistTable(unittest.TestCase):
|
||||
|
@ -20,8 +20,15 @@ class TestPlaylistTable(unittest.TestCase):
|
|||
self.assertEqual(db.playlist.Default, [ "Collection", "Favorites", "New Tracks",
|
||||
"Previous", "Queued Tracks" ])
|
||||
|
||||
rows = db.execute("SELECT playlistid,name,sort "
|
||||
"FROM playlists").fetchall()
|
||||
cur = db.execute("SELECT playlistid, "
|
||||
"playlists.plstateid, "
|
||||
"name, "
|
||||
"playlists.sort, "
|
||||
"random,loop "
|
||||
"FROM playlists "
|
||||
"JOIN playlist_states "
|
||||
"ON playlists.plstateid = playlist_states.plstateid")
|
||||
rows = cur.fetchall()
|
||||
self.assertEqual(tuple(rows[0]), expected[0])
|
||||
self.assertEqual(tuple(rows[1]), expected[1])
|
||||
self.assertEqual(tuple(rows[2]), expected[2])
|
||||
|
@ -35,6 +42,7 @@ class TestPlaylistTable(unittest.TestCase):
|
|||
|
||||
self.assertEqual(playlist.name, "Test Playlist")
|
||||
self.assertEqual(playlist.sort, "test playlist")
|
||||
self.assertIsInstance(playlist.playlist_state, db.state.PlaylistState)
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.playlist.Table.insert("Test Playlist")
|
||||
|
@ -61,6 +69,9 @@ class TestPlaylistTable(unittest.TestCase):
|
|||
self.assertEqual(db.playlist.Table.lookup("Test Playlist"), playlist)
|
||||
self.assertIsNone(db.playlist.Table.lookup("none"))
|
||||
|
||||
def test_playlist_create_default_playlists(self):
|
||||
db.playlist.Table.create_default_playlists()
|
||||
|
||||
|
||||
class TestPlaylistMap(unittest.TestCase):
|
||||
def setUp(self):
|
||||
|
|
Loading…
Reference in New Issue