db: Create an AlbumTable and Album tag
Similar to Artists, only holding information about each album instead. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
2d125546dd
commit
6a1713630b
|
@ -12,9 +12,10 @@ execute = Connection.execute
|
|||
executemany = Connection.executemany
|
||||
|
||||
from . import artist
|
||||
from . import album
|
||||
|
||||
def reset():
|
||||
mods = [ artist ]
|
||||
mods = [ artist, album ]
|
||||
|
||||
for mod in mods: mod.Table.drop()
|
||||
for mod in mods: mod.Table.do_create()
|
||||
|
|
58
db/album.py
Normal file
58
db/album.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: albums
|
||||
# +---------+----------+------+------+
|
||||
# | albumid | artistid | name | sort |
|
||||
# +---------+----------+------+------+
|
||||
#
|
||||
# Index: album_index
|
||||
# +-----------------------------+
|
||||
# | (artistid, name) -> albumid |
|
||||
# +-----------------------------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import artist
|
||||
|
||||
class Album(objects.Tag):
|
||||
def do_get_column(self, column):
|
||||
return execute(f"SELECT {column} FROM albums "
|
||||
"WHERE albumid=?", [ self.rowid ])
|
||||
|
||||
@GObject.Property
|
||||
def artist(self):
|
||||
return artist.Artist(self.get_column("artistid"))
|
||||
|
||||
|
||||
class AlbumTable(objects.Table):
|
||||
def __init__(self):
|
||||
objects.Table.__init__(self, "albums", Album)
|
||||
|
||||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS albums "
|
||||
"(albumid INTEGER PRIMARY KEY, "
|
||||
" artistid INTEGER, "
|
||||
" name TEXT, "
|
||||
" sort TEXT, "
|
||||
" FOREIGN KEY(artistid) REFERENCES artists(artistid), "
|
||||
" UNIQUE(artistid, name))")
|
||||
execute("CREATE INDEX IF NOT EXISTS artist_index "
|
||||
"ON albums(artistid, name)")
|
||||
|
||||
def do_insert(self, artist, name):
|
||||
return execute("INSERT INTO albums (artistid, name, sort) "
|
||||
"VALUES (?, ?, ?)", (int(artist), name, name.casefold()))
|
||||
|
||||
def do_delete(self, album):
|
||||
return execute("DELETE FROM albums WHERE albumid=?", [ int(album) ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
return execute("SELECT albumid FROM albums "
|
||||
"WHERE albumid=?", [ rowid ])
|
||||
|
||||
def do_lookup(self, artist, name):
|
||||
return execute("SELECT albumid FROM albums "
|
||||
"WHERE (artistid=? AND name=?)", [ int(artist), name ])
|
||||
|
||||
|
||||
Table = AlbumTable()
|
53
db/test_album.py
Normal file
53
db/test_album.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import db
|
||||
import sqlite3
|
||||
import unittest
|
||||
from gi.repository import GObject
|
||||
|
||||
|
||||
class TestAlbumTable(unittest.TestCase):
|
||||
def on_row_inserted(self, table, row):
|
||||
self.row_inserted = row
|
||||
|
||||
def setUp(self):
|
||||
db.reset()
|
||||
|
||||
def test_album_table_init(self):
|
||||
self.assertIsInstance(db.album.Table, db.album.AlbumTable)
|
||||
db.execute("SELECT albumid,artistid,name,sort FROM albums")
|
||||
|
||||
def test_album_table_insert(self):
|
||||
db.album.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = db.album.Table.insert(artist, "Test Album")
|
||||
|
||||
self.assertIsInstance(album, db.album.Album)
|
||||
self.assertIsInstance(album, db.objects.Tag)
|
||||
self.assertEqual(self.row_inserted, album)
|
||||
|
||||
self.assertEqual(album.name, "Test Album")
|
||||
self.assertEqual(album.sort, "test album")
|
||||
self.assertEqual(album.get_property("artist"), artist)
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.album.Table.insert(artist, "Test Album")
|
||||
|
||||
def test_album_table_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = db.album.Table.find(artist, "Test Album")
|
||||
|
||||
db.album.Table.delete(album)
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "Test Album"))
|
||||
|
||||
def test_album_table_get(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = db.album.Table.insert(artist, "Test Album")
|
||||
self.assertEqual(db.album.Table.get(1), album)
|
||||
self.assertIsNone(db.album.Table.get(2))
|
||||
|
||||
def test_album_table_lookup(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = db.album.Table.insert(artist, "Test Album")
|
||||
self.assertEqual(db.album.Table.lookup(artist, "Test Album"), album)
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "none"))
|
Loading…
Reference in New Issue
Block a user