Put commit()s where needed, move some info to debug logging

This commit is contained in:
Keith Edmunds 2024-04-05 14:42:04 +01:00
parent 16e3c8235e
commit 76021aa1c6
3 changed files with 45 additions and 46 deletions

View File

@ -78,7 +78,7 @@ class NoteColours(dbtables.NoteColoursTable):
self.order = order self.order = order
session.add(self) session.add(self)
session.flush() session.commit()
@classmethod @classmethod
def get_all(cls, session: Session) -> Sequence["NoteColours"]: def get_all(cls, session: Session) -> Sequence["NoteColours"]:
@ -162,7 +162,7 @@ class Playlists(dbtables.PlaylistsTable):
def __init__(self, session: Session, name: str): def __init__(self, session: Session, name: str):
self.name = name self.name = name
session.add(self) session.add(self)
session.flush() session.commit()
@staticmethod @staticmethod
def clear_tabs(session: Session, playlist_ids: List[int]) -> None: def clear_tabs(session: Session, playlist_ids: List[int]) -> None:
@ -201,7 +201,7 @@ class Playlists(dbtables.PlaylistsTable):
""" """
self.deleted = True self.deleted = True
session.flush() session.commit()
@classmethod @classmethod
def get_all(cls, session: Session) -> Sequence["Playlists"]: def get_all(cls, session: Session) -> Sequence["Playlists"]:
@ -268,7 +268,7 @@ class Playlists(dbtables.PlaylistsTable):
""" """
self.name = new_name self.name = new_name
session.flush() session.commit()
@staticmethod @staticmethod
def save_as_template( def save_as_template(
@ -381,7 +381,7 @@ class PlaylistRows(dbtables.PlaylistRowsTable):
PlaylistRows.plr_rownum > maxrow, PlaylistRows.plr_rownum > maxrow,
) )
) )
session.flush() session.commit()
@staticmethod @staticmethod
def delete_row(session: Session, playlist_id: int, row_number: int) -> None: def delete_row(session: Session, playlist_id: int, row_number: int) -> None:
@ -575,7 +575,7 @@ class Settings(dbtables.SettingsTable):
def __init__(self, session: Session, name: str): def __init__(self, session: Session, name: str):
self.name = name self.name = name
session.add(self) session.add(self)
session.flush() session.commit()
@classmethod @classmethod
def all_as_dict(cls, session): def all_as_dict(cls, session):
@ -605,7 +605,7 @@ class Settings(dbtables.SettingsTable):
for key, value in data.items(): for key, value in data.items():
assert hasattr(self, key) assert hasattr(self, key)
setattr(self, key, value) setattr(self, key, value)
session.flush() session.commit()
class Tracks(dbtables.TracksTable): class Tracks(dbtables.TracksTable):

View File

@ -123,7 +123,7 @@ class PlaylistModel(QAbstractTableModel):
*args, *args,
**kwargs, **kwargs,
): ):
log.info(f"PlaylistModel.__init__({playlist_id=})") log.debug(f"PlaylistModel.__init__({playlist_id=})")
self.playlist_id = playlist_id self.playlist_id = playlist_id
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -548,7 +548,7 @@ class PlaylistModel(QAbstractTableModel):
If not given, return row number to add to end of model. If not given, return row number to add to end of model.
""" """
log.info(f"_get_new_row_number({proposed_row_number=})") log.debug(f"_get_new_row_number({proposed_row_number=})")
if proposed_row_number is None or proposed_row_number > len(self.playlist_rows): if proposed_row_number is None or proposed_row_number > len(self.playlist_rows):
# We are adding to the end of the list # We are adding to the end of the list
@ -845,7 +845,7 @@ class PlaylistModel(QAbstractTableModel):
Move the playlist rows given to to_row and below. Move the playlist rows given to to_row and below.
""" """
log.info(f"move_rows({from_rows=}, {to_row_number=}") log.debug(f"move_rows({from_rows=}, {to_row_number=}")
# Build a {current_row_number: new_row_number} dictionary # Build a {current_row_number: new_row_number} dictionary
row_map: dict[int, int] = {} row_map: dict[int, int] = {}
@ -1107,7 +1107,7 @@ class PlaylistModel(QAbstractTableModel):
Signal handler for when row ordering has changed Signal handler for when row ordering has changed
""" """
log.info("reset_track_sequence_row_numbers()") log.debug("reset_track_sequence_row_numbers()")
# Check the track_sequence next, now and previous plrs and # Check the track_sequence next, now and previous plrs and
# update the row number # update the row number

View File

@ -28,47 +28,47 @@ from app.models import ( # noqa: E402
) )
# class TestMMMiscTracks(unittest.TestCase): class TestMMMiscTracks(unittest.TestCase):
# def setUp(self): def setUp(self):
# PLAYLIST_NAME = "tracks playlist" PLAYLIST_NAME = "tracks playlist"
# self.test_tracks = [ self.test_tracks = [
# "testdata/isa.mp3", "testdata/isa.mp3",
# "testdata/isa_with_gap.mp3", "testdata/isa_with_gap.mp3",
# "testdata/loser.mp3", "testdata/loser.mp3",
# "testdata/lovecats-10seconds.mp3", "testdata/lovecats-10seconds.mp3",
# "testdata/lovecats.mp3", "testdata/lovecats.mp3",
# "testdata/mom.mp3", "testdata/mom.mp3",
# "testdata/sitting.mp3", "testdata/sitting.mp3",
# ] ]
# db.create_all() db.create_all()
# # Create a playlist and model # Create a playlist and model
# with db.Session() as session: with db.Session() as session:
# self.playlist = Playlists(session, PLAYLIST_NAME) self.playlist = Playlists(session, PLAYLIST_NAME)
# self.model = playlistmodel.PlaylistModel(self.playlist.id) self.model = playlistmodel.PlaylistModel(self.playlist.id)
# for row in range(len(self.test_tracks)): for row in range(len(self.test_tracks)):
# track_path = self.test_tracks[row % len(self.test_tracks)] track_path = self.test_tracks[row % len(self.test_tracks)]
# metadata = get_file_metadata(track_path) metadata = get_file_metadata(track_path)
# track = Tracks(session, **metadata) track = Tracks(session, **metadata)
# self.model.insert_row( self.model.insert_row(
# proposed_row_number=row, track_id=track.id, note=f"{row=}" proposed_row_number=row, track_id=track.id, note=f"{row=}"
# ) )
# session.commit() session.commit()
# def tearDown(self): def tearDown(self):
# db.drop_all() db.drop_all()
# def test_7_row_playlist(self): def test_7_row_playlist(self):
# # Test auto-created playlist # Test auto-created playlist
# assert self.model.rowCount() == 7 assert self.model.rowCount() == 7
# assert max(self.model.playlist_rows.keys()) == 6 assert max(self.model.playlist_rows.keys()) == 6
# for row in range(self.model.rowCount()): for row in range(self.model.rowCount()):
# assert row in self.model.playlist_rows assert row in self.model.playlist_rows
# assert self.model.playlist_rows[row].plr_rownum == row assert self.model.playlist_rows[row].plr_rownum == row
class TestMMMiscRowMove(unittest.TestCase): class TestMMMiscRowMove(unittest.TestCase):
@ -82,7 +82,6 @@ class TestMMMiscRowMove(unittest.TestCase):
self.playlist = Playlists(session, PLAYLIST_NAME) self.playlist = Playlists(session, PLAYLIST_NAME)
self.model = playlistmodel.PlaylistModel(self.playlist.id) self.model = playlistmodel.PlaylistModel(self.playlist.id)
for row in range(ROWS_TO_CREATE): for row in range(ROWS_TO_CREATE):
print(f"{row=}")
self.model.insert_row(proposed_row_number=row, note=str(row)) self.model.insert_row(proposed_row_number=row, note=str(row))
session.commit() session.commit()