db: Create a DiscTable and Disc object

To represent each disc of an album. Discs may have different subtitles
that we want to display (or there may not be a subtitle at all). Casting
a Disc to a string either appends the subtitle to the album name or
returns the album name directly. This is intended to be used by the
ColumnView to display album names

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-08-17 17:47:54 -04:00
parent 94f08dcd06
commit bbb248f665
3 changed files with 141 additions and 1 deletions

View File

@ -13,9 +13,10 @@ executemany = Connection.executemany
from . import artist
from . import album
from . import disc
def reset():
mods = [ artist, album ]
mods = [ artist, album, disc ]
for mod in mods: mod.Table.drop()
for mod in mods: mod.Table.do_create()

67
db/disc.py Normal file
View File

@ -0,0 +1,67 @@
# Copyright 2021 (c) Anna Schumaker.
#
# Table: discs
# +--------+---------+--------+----------+
# | discid | albumid | number | subtitle |
# +--------+---------+--------+----------+
from gi.repository import GObject
from . import album
from . import execute
from . import objects
class Disc(objects.Row):
def __gt__(self, rhs): return self.number > rhs.number
def __lt__(self, rhs): return self.number < rhs.number
def __str__(self):
if self.subtitle:
return f"{str(self.album)}: {self.subtitle}"
return f"{str(self.album)}"
def do_get_column(self, column):
return execute(f"SELECT {column} FROM discs "
"WHERE discid=?", [ self.rowid ])
@GObject.Property
def album(self):
return album.Album(self.get_column("albumid"))
@GObject.Property
def number(self):
return self.get_column("number")
@GObject.Property
def subtitle(self):
return self.get_column("subtitle")
class DiscTable(objects.Table):
def __init__(self):
objects.Table.__init__(self, "discs", Disc)
def do_create(self):
execute("CREATE TABLE IF NOT EXISTS discs "
"(discid INTEGER PRIMARY KEY, "
" albumid INTEGER, "
" number INTEGER, "
" subtitle TEXT, "
" FOREIGN KEY(albumid) REFERENCES albums(albumid), "
" UNIQUE(albumid, number))")
def do_insert(self, album, number, subtitle):
subtitle = subtitle if subtitle and len(subtitle) > 0 else None
return execute("INSERT INTO discs (albumid, number, subtitle) "
"VALUES (?, ?, ?)", (int(album), number, subtitle))
def do_delete(self, disc):
return execute("DELETE FROM discs WHERE discid=?", [ int(disc) ])
def do_get(self, rowid):
return execute("SELECT discid FROM discs "
"WHERE discid=?", [ rowid ])
def do_lookup(self, album, number, subtitle=None):
return execute("SELECT discid FROM discs "
"WHERE (albumid=? AND number=?)", [ int(album), number ])
Table = DiscTable()

72
db/test_disc.py Normal file
View File

@ -0,0 +1,72 @@
# Copyright 2021 (c) Anna Schumaker.
import db
import sqlite3
import unittest
from gi.repository import GObject
class TestDiscTable(unittest.TestCase):
def setUp(self):
db.reset()
def test_disc_table_init(self):
self.assertIsInstance(db.disc.Table, db.disc.DiscTable)
db.execute("SELECT discid,albumid,number,subtitle FROM discs")
def test_disc_table_insert(self):
artist = db.artist.Table.insert("Test Artist", "Test Sort")
album = db.album.Table.insert(artist, "Test Album")
disc = db.disc.Table.insert(album, 1, "subtitle")
self.assertIsInstance(disc, db.disc.Disc)
self.assertIsInstance(disc, db.objects.Row)
self.assertEqual(disc.album, album)
self.assertEqual(disc.number, 1)
self.assertEqual(disc.subtitle, "subtitle")
self.assertEqual(str(disc), "Test Album: subtitle")
disc2 = db.disc.Table.insert(album, 2, None)
self.assertEqual(disc2.subtitle, None)
self.assertEqual(str(disc2), "Test Album")
disc3 = db.disc.Table.insert(album, 3, "")
self.assertEqual(disc3.subtitle, None)
self.assertEqual(str(disc3), "Test Album")
with self.assertRaises(sqlite3.IntegrityError):
db.disc.Table.insert(album, 1, "subtitle")
def test_disc_table_delete(self):
artist = db.artist.Table.find("Test Artist", "Test Sort")
album = db.album.Table.find(artist, "Test Album")
disc = db.disc.Table.find(album, 1, "subtitle")
db.disc.Table.delete(disc)
self.assertIsNone(db.disc.Table.lookup(album, 1))
def test_disc_table_get(self):
artist = db.artist.Table.insert("Test Artist", "Test Sort")
album = db.album.Table.insert(artist, "Test Album")
disc = db.disc.Table.insert(album, 1, None)
self.assertEqual(db.disc.Table.get(1), disc)
self.assertIsNone(db.disc.Table.get(2))
def test_disc_table_lookup(self):
artist = db.artist.Table.insert("Test Artist", "Test Sort")
album = db.album.Table.insert(artist, "Test Album")
disc = db.disc.Table.insert(album, 1, None)
self.assertEqual(db.disc.Table.lookup(album, 1), disc)
self.assertIsNone(db.disc.Table.lookup(album, "none"))
def test_disc_compare(self):
artist = db.artist.Table.insert("Test Artist", "Test Sort")
album = db.album.Table.insert(artist, "Test Album")
disc1 = db.disc.Table.insert(album, 1, "subtitle")
disc2 = db.disc.Table.insert(album, 2, "subtitle")
self.assertTrue(disc1 < disc2)
self.assertTrue(disc2 > disc1)
self.assertFalse(disc1 > disc2)
self.assertFalse(disc2 < disc1)