From 92d85304f2da51b2e7ae7bb22d59d2b4dd437276 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 5 Apr 2024 14:42:04 +0100 Subject: [PATCH] Put commit()s where needed, move some info to debug logging --- app/models.py | 14 ++++---- app/playlistmodel.py | 8 ++--- tests/test_playlistmodel.py | 69 ++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/app/models.py b/app/models.py index 10f9a31..e0c8f0f 100644 --- a/app/models.py +++ b/app/models.py @@ -78,7 +78,7 @@ class NoteColours(dbtables.NoteColoursTable): self.order = order session.add(self) - session.flush() + session.commit() @classmethod def get_all(cls, session: Session) -> Sequence["NoteColours"]: @@ -162,7 +162,7 @@ class Playlists(dbtables.PlaylistsTable): def __init__(self, session: Session, name: str): self.name = name session.add(self) - session.flush() + session.commit() @staticmethod def clear_tabs(session: Session, playlist_ids: List[int]) -> None: @@ -201,7 +201,7 @@ class Playlists(dbtables.PlaylistsTable): """ self.deleted = True - session.flush() + session.commit() @classmethod def get_all(cls, session: Session) -> Sequence["Playlists"]: @@ -268,7 +268,7 @@ class Playlists(dbtables.PlaylistsTable): """ self.name = new_name - session.flush() + session.commit() @staticmethod def save_as_template( @@ -381,7 +381,7 @@ class PlaylistRows(dbtables.PlaylistRowsTable): PlaylistRows.plr_rownum > maxrow, ) ) - session.flush() + session.commit() @staticmethod 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): self.name = name session.add(self) - session.flush() + session.commit() @classmethod def all_as_dict(cls, session): @@ -605,7 +605,7 @@ class Settings(dbtables.SettingsTable): for key, value in data.items(): assert hasattr(self, key) setattr(self, key, value) - session.flush() + session.commit() class Tracks(dbtables.TracksTable): diff --git a/app/playlistmodel.py b/app/playlistmodel.py index 1e45758..66b2477 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -123,7 +123,7 @@ class PlaylistModel(QAbstractTableModel): *args, **kwargs, ): - log.info(f"PlaylistModel.__init__({playlist_id=})") + log.debug(f"PlaylistModel.__init__({playlist_id=})") self.playlist_id = playlist_id super().__init__(*args, **kwargs) @@ -543,7 +543,7 @@ class PlaylistModel(QAbstractTableModel): 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): # We are adding to the end of the list @@ -840,7 +840,7 @@ class PlaylistModel(QAbstractTableModel): 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 row_map: dict[int, int] = {} @@ -1102,7 +1102,7 @@ class PlaylistModel(QAbstractTableModel): 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 # update the row number diff --git a/tests/test_playlistmodel.py b/tests/test_playlistmodel.py index 281e4c0..0e69bc7 100644 --- a/tests/test_playlistmodel.py +++ b/tests/test_playlistmodel.py @@ -28,47 +28,47 @@ from app.models import ( # noqa: E402 ) -# class TestMMMiscTracks(unittest.TestCase): -# def setUp(self): -# PLAYLIST_NAME = "tracks playlist" -# self.test_tracks = [ -# "testdata/isa.mp3", -# "testdata/isa_with_gap.mp3", -# "testdata/loser.mp3", -# "testdata/lovecats-10seconds.mp3", -# "testdata/lovecats.mp3", -# "testdata/mom.mp3", -# "testdata/sitting.mp3", -# ] +class TestMMMiscTracks(unittest.TestCase): + def setUp(self): + PLAYLIST_NAME = "tracks playlist" + self.test_tracks = [ + "testdata/isa.mp3", + "testdata/isa_with_gap.mp3", + "testdata/loser.mp3", + "testdata/lovecats-10seconds.mp3", + "testdata/lovecats.mp3", + "testdata/mom.mp3", + "testdata/sitting.mp3", + ] -# db.create_all() + db.create_all() -# # Create a playlist and model -# with db.Session() as session: -# self.playlist = Playlists(session, PLAYLIST_NAME) -# self.model = playlistmodel.PlaylistModel(self.playlist.id) + # Create a playlist and model + with db.Session() as session: + self.playlist = Playlists(session, PLAYLIST_NAME) + self.model = playlistmodel.PlaylistModel(self.playlist.id) -# for row in range(len(self.test_tracks)): -# track_path = self.test_tracks[row % len(self.test_tracks)] -# metadata = get_file_metadata(track_path) -# track = Tracks(session, **metadata) -# self.model.insert_row( -# proposed_row_number=row, track_id=track.id, note=f"{row=}" -# ) + for row in range(len(self.test_tracks)): + track_path = self.test_tracks[row % len(self.test_tracks)] + metadata = get_file_metadata(track_path) + track = Tracks(session, **metadata) + self.model.insert_row( + proposed_row_number=row, track_id=track.id, note=f"{row=}" + ) -# session.commit() + session.commit() -# def tearDown(self): -# db.drop_all() + def tearDown(self): + db.drop_all() -# def test_7_row_playlist(self): -# # Test auto-created playlist + def test_7_row_playlist(self): + # Test auto-created playlist -# assert self.model.rowCount() == 7 -# assert max(self.model.playlist_rows.keys()) == 6 -# for row in range(self.model.rowCount()): -# assert row in self.model.playlist_rows -# assert self.model.playlist_rows[row].plr_rownum == row + assert self.model.rowCount() == 7 + assert max(self.model.playlist_rows.keys()) == 6 + for row in range(self.model.rowCount()): + assert row in self.model.playlist_rows + assert self.model.playlist_rows[row].plr_rownum == row class TestMMMiscRowMove(unittest.TestCase): @@ -82,7 +82,6 @@ class TestMMMiscRowMove(unittest.TestCase): self.playlist = Playlists(session, PLAYLIST_NAME) self.model = playlistmodel.PlaylistModel(self.playlist.id) for row in range(ROWS_TO_CREATE): - print(f"{row=}") self.model.insert_row(proposed_row_number=row, note=str(row)) session.commit()