db: Create new Playlist and ParentPlaylist classes

These are the base classes that will be used by all our Playlist-like
objects.

Implements: Issue #11 (Cache database items fields)
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-10-07 20:48:26 -04:00
parent a4fbd5f2f3
commit 6d796e0a89
2 changed files with 85 additions and 0 deletions

47
db/playlist.py Normal file
View File

@ -0,0 +1,47 @@
# Copyright 2021 (c) Anna Schumaker.
from gi.repository import GObject
from . import sql
from . import state
class Playlist(GObject.GObject):
def __init__(self, row):
GObject.GObject.__init__(self)
self._rowid = row[0]
self._plstate = state.Table.get(row["plstateid"])
def has_children(self): return False
@GObject.Property
def name(self): raise NotImplementedError
@GObject.Property
def plist_state(self): return self._plstate
@GObject.Property
def rowid(self): return self._rowid
class ParentPlaylist(Playlist):
def has_children(self): return True
def get_child_table(self): raise NotImplementedError
def get_n_children(self):
return self.get_child_table().get_n_children(self)
def get_child(self, n):
return self.get_child_table().get_child(self, n)
def get_child_index(self, child):
return self.get_child_table().get_child_index(self, child)
def find_child(self, *args):
if (res := self.lookup_child(*args)) == None:
res = self.get_child_table().insert(self, *args)
self.emit("children-changed", self.get_child_index(res), 0, 1)
return res
def lookup_child(self, *args):
return self.get_child_table().lookup(self, *args)
@GObject.Signal(arg_types=(int,int,int))
def children_changed(self, pos, rm, add): pass

38
db/test_playlist.py Normal file
View File

@ -0,0 +1,38 @@
# Copyright 2021 (c) Anna Schumaker.
import db
import unittest
from gi.repository import GObject
from . import playlist
class TestPlaylist(unittest.TestCase):
def test_init(self):
db.reset()
plist = playlist.Playlist({ 0:1, "plstateid":10 })
self.assertIsInstance(plist, GObject.GObject)
self.assertFalse(plist.has_children())
self.assertEqual(plist._rowid, 1)
self.assertEqual(plist.get_property("rowid"), 1)
self.assertIsNone(plist._plstate)
self.assertIsNone(plist.get_property("plist_state"))
with self.assertRaises(NotImplementedError):
plist.get_property("name")
class TestParentPlaylist(unittest.TestCase):
def test_init(self):
parent = playlist.ParentPlaylist({ 0:1, "plstateid":10 })
self.assertIsInstance(parent, playlist.Playlist)
self.assertTrue(parent.has_children())
with self.assertRaises(NotImplementedError):
parent.get_child_table()
with self.assertRaises(NotImplementedError):
parent.get_n_children()
with self.assertRaises(NotImplementedError):
parent.get_child(0)
with self.assertRaises(NotImplementedError):
parent.get_child_index(0)