db: Create a LibraryTable and Library object
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
63e2e6473d
commit
f519830095
|
@ -17,9 +17,10 @@ from . import disc
|
|||
from . import genre
|
||||
from . import decade
|
||||
from . import year
|
||||
from . import library
|
||||
|
||||
def reset():
|
||||
mods = [ artist, album, disc, genre, decade, year ]
|
||||
mods = [ artist, album, disc, genre, decade, year, library ]
|
||||
|
||||
for mod in mods: mod.Table.drop()
|
||||
for mod in mods: mod.Table.do_create()
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: libraries
|
||||
# +-----------+------+---------+
|
||||
# | libraryid | path | enabled |
|
||||
# +-----------+------+---------+
|
||||
import pathlib
|
||||
from gi.repository import GObject
|
||||
from . import commit
|
||||
from . import execute
|
||||
from . import objects
|
||||
|
||||
class Library(objects.Row):
|
||||
def __lt__(self, rhs): return self.path < rhs.path
|
||||
def __gt__(self, rhs): return self.path > rhs.path
|
||||
def __str__(self): return str(self.path)
|
||||
|
||||
def do_get_column(self, column):
|
||||
return execute(f"SELECT {column} FROM libraries "
|
||||
"WHERE libraryid=?", [ self.rowid ])
|
||||
|
||||
@GObject.Property
|
||||
def path(self):
|
||||
return pathlib.Path(self.get_column("path"))
|
||||
|
||||
@GObject.Property(type=bool,default=False)
|
||||
def enabled(self):
|
||||
return bool(self.get_column("enabled"))
|
||||
|
||||
@enabled.setter
|
||||
def enabled(self, newval):
|
||||
execute("UPDATE libraries "
|
||||
"SET enabled=? "
|
||||
"WHERE libraryid=?", [ newval, self.rowid ])
|
||||
commit()
|
||||
|
||||
|
||||
class LibraryTable(objects.Table):
|
||||
def __init__(self):
|
||||
objects.Table.__init__(self, "libraries", Library)
|
||||
|
||||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS libraries "
|
||||
"(libraryid INTEGER PRIMARY KEY, "
|
||||
" path TEXT UNIQUE, "
|
||||
" enabled INTEGER DEFAULT 1)")
|
||||
|
||||
def do_insert(self, path):
|
||||
return execute("INSERT INTO libraries (path) "
|
||||
"VALUES (?)", (str(path),))
|
||||
|
||||
def do_delete(self, library):
|
||||
return execute("DELETE FROM libraries WHERE libraryid=?", [ int(library) ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
return execute("SELECT libraryid FROM libraries "
|
||||
"WHERE libraryid=?", [ rowid ])
|
||||
|
||||
def do_lookup(self, path):
|
||||
return execute("SELECT libraryid FROM libraries "
|
||||
"WHERE path=?", [ str(path) ])
|
||||
|
||||
|
||||
Table = LibraryTable()
|
|
@ -0,0 +1,59 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import db
|
||||
import pathlib
|
||||
import sqlite3
|
||||
import unittest
|
||||
from gi.repository import GObject
|
||||
|
||||
|
||||
class TestLibraryTable(unittest.TestCase):
|
||||
def setUp(self):
|
||||
db.reset()
|
||||
|
||||
def test_library_table_init(self):
|
||||
self.assertIsInstance(db.library.Table, db.library.LibraryTable)
|
||||
db.execute("SELECT libraryid,path,enabled FROM libraries")
|
||||
|
||||
def test_library_table_insert(self):
|
||||
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
self.assertIsInstance(library, db.library.Library)
|
||||
self.assertIsInstance(library, db.objects.Row)
|
||||
|
||||
self.assertEqual(library.path, pathlib.Path("/a/b/c"))
|
||||
self.assertEqual(str(library), "/a/b/c")
|
||||
self.assertTrue(library.enabled)
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
|
||||
def test_library_table_delete(self):
|
||||
library = db.library.Table.find(pathlib.Path("/a/b/c"))
|
||||
db.library.Table.delete(library)
|
||||
self.assertIsNone(db.library.Table.lookup(pathlib.Path("/a/b/c")))
|
||||
|
||||
def test_library_table_get(self):
|
||||
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
self.assertEqual(db.library.Table.get(1), library)
|
||||
self.assertIsNone(db.library.Table.get(2))
|
||||
|
||||
def test_library_table_lookup(self):
|
||||
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
self.assertEqual(db.library.Table.lookup(pathlib.Path("/a/b/c")), library)
|
||||
self.assertIsNone(db.library.Table.lookup(pathlib.Path("/a/b/d")))
|
||||
|
||||
def test_library_compare(self):
|
||||
abc = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
efg = db.library.Table.insert(pathlib.Path("/e/f/g"))
|
||||
|
||||
self.assertTrue(abc < efg)
|
||||
self.assertTrue(efg > abc)
|
||||
|
||||
self.assertFalse(abc > efg)
|
||||
self.assertFalse(efg < abc)
|
||||
|
||||
def test_library_enabled(self):
|
||||
abc = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
self.assertTrue(abc.enabled)
|
||||
|
||||
abc.set_property("enabled", False)
|
||||
self.assertFalse(abc.enabled)
|
Loading…
Reference in New Issue