db: Create a PlaylistStateTable and PlaylistState object
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
952d70c282
commit
38861bbabf
|
@ -21,6 +21,7 @@ from . import year
|
|||
from . import library
|
||||
from . import track
|
||||
from . import playlist
|
||||
from . import state
|
||||
|
||||
def make_fake_track(trackno, length, title, path, lib="/a/b/c", art="Test Artist",
|
||||
alb="Test Album", disk=1, subtitle=None, yeer=2021):
|
||||
|
@ -33,7 +34,8 @@ def make_fake_track(trackno, length, title, path, lib="/a/b/c", art="Test Artist
|
|||
length, title, pathlib.Path(path))
|
||||
|
||||
def reset():
|
||||
mods = [ artist, album, disc, genre, decade, year, library, track, playlist ]
|
||||
mods = [ artist, album, disc, genre, decade, year,
|
||||
library, track, playlist, state ]
|
||||
|
||||
for mod in mods: mod.Table.drop()
|
||||
for mod in mods: mod.Table.do_create()
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: playlist_states
|
||||
# +-----------+--------+------+---------+------+
|
||||
# | plstateid | random | loop | current | sort |
|
||||
# +-----------+--------+------+---------+------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from . import objects
|
||||
|
||||
class PlaylistState(objects.Row):
|
||||
def do_get_column(self, column):
|
||||
return execute(f"SELECT {column} FROM playlist_states "
|
||||
"WHERE plstateid=?", [ self.rowid ])
|
||||
|
||||
@GObject.Property(type=bool,default=False)
|
||||
def random(self):
|
||||
return bool(self.get_column("random"))
|
||||
|
||||
@random.setter
|
||||
def random(self, newval):
|
||||
execute("UPDATE playlist_states SET random=? WHERE plstateid=?",
|
||||
[ newval, self.rowid ])
|
||||
|
||||
@GObject.Property(type=bool,default=False)
|
||||
def loop(self):
|
||||
return bool(self.get_column("loop"))
|
||||
|
||||
@loop.setter
|
||||
def loop(self, newval):
|
||||
execute("UPDATE playlist_states SET loop=? WHERE plstateid=?",
|
||||
[ newval, self.rowid ])
|
||||
|
||||
@GObject.Property(type=int)
|
||||
def current(self):
|
||||
return self.get_column("current")
|
||||
|
||||
@current.setter
|
||||
def current(self, newval):
|
||||
execute("UPDATE playlist_states SET current=? WHERE plstateid=?",
|
||||
[ newval, self.rowid ])
|
||||
|
||||
@GObject.Property(type=str)
|
||||
def sort(self):
|
||||
return self.get_column("sort")
|
||||
|
||||
@sort.setter
|
||||
def sort(self, newval):
|
||||
execute("UPDATE playlist_states SET sort=? WHERE plstateid=?",
|
||||
[ newval, self.rowid ])
|
||||
|
||||
|
||||
class PlaylistStateTable(objects.Table):
|
||||
def __init__(self):
|
||||
objects.Table.__init__(self, "playlist_states", PlaylistState)
|
||||
|
||||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS playlist_states "
|
||||
"(plstateid INTEGER PRIMARY KEY, "
|
||||
" random INTEGER DEFAULT 0, "
|
||||
" loop INTEGER DEFAULT 0, "
|
||||
" current INTEGER DEFAULT -1, "
|
||||
" sort TEXT)")
|
||||
|
||||
def do_insert(self, random, loop):
|
||||
return execute("INSERT INTO playlist_states (random, loop, sort) "
|
||||
"VALUES (?, ?, ?)", (random, loop, ""))
|
||||
|
||||
def do_delete(self, state):
|
||||
return execute("DELETE FROM playlist_states WHERE plstateid=?", [ int(state) ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
return execute("SELECT plstateid FROM playlist_states "
|
||||
"WHERE plstateid=?", [ rowid ])
|
||||
|
||||
def insert(self, random=False, loop=False):
|
||||
return super().insert(random, loop)
|
||||
|
||||
|
||||
Table = PlaylistStateTable()
|
|
@ -0,0 +1,53 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import db
|
||||
import sqlite3
|
||||
import unittest
|
||||
from gi.repository import GObject
|
||||
|
||||
|
||||
class TestPlaylistStateTable(unittest.TestCase):
|
||||
def setUp(self):
|
||||
db.reset()
|
||||
|
||||
def test_playlist_state_table_init(self):
|
||||
self.assertIsInstance(db.state.Table, db.state.PlaylistStateTable)
|
||||
db.execute("SELECT plstateid,random,loop,current,sort "
|
||||
"FROM playlist_states")
|
||||
|
||||
def test_playlist_state_table_insert(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
|
||||
self.assertFalse(state.random)
|
||||
self.assertFalse(state.loop)
|
||||
self.assertEqual(state.current, -1)
|
||||
self.assertEqual(state.sort, "")
|
||||
|
||||
def test_playlist_state_table_delete(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
db.state.Table.delete(state)
|
||||
self.assertIsNone(db.state.Table.get(int(state)))
|
||||
|
||||
def test_playlist_state_table_get(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
self.assertEqual(db.state.Table.get(int(state)), state)
|
||||
self.assertIsNone(db.year.Table.get(int(state) + 1))
|
||||
|
||||
def test_playlist_state_table_lookup(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
with self.assertRaises(NotImplementedError):
|
||||
db.state.Table.lookup()
|
||||
|
||||
def test_playlist_state_properties(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
|
||||
state.random = True
|
||||
self.assertTrue(state.random)
|
||||
|
||||
state.loop = True
|
||||
self.assertTrue(state.loop)
|
||||
|
||||
state.current = 3
|
||||
self.assertEqual(state.current, 3)
|
||||
|
||||
state.sort = "album:True;artist:False"
|
||||
self.assertEqual(state.sort, "album:True;artist:False")
|
Loading…
Reference in New Issue