from app.models import ( Playlists, ) from app import playlistmodel from dbconfig import scoped_session def create_model_with_playlist_rows( session: scoped_session, rows: int ) -> "playlistmodel.PlaylistModel": playlist = Playlists(session, "test playlist") # Create a model model = playlistmodel.PlaylistModel(playlist.id, None) for row in range(rows): plr = model._insert_row(session, row) newrow = plr.plr_rownum model.playlist_rows[newrow].note = str(newrow) session.commit() return model def test_insert_row(monkeypatch, Session): monkeypatch.setattr(playlistmodel, "Session", Session) # Create a playlist with Session() as session: playlist = Playlists(Session, "test playlist") # Create a model model = playlistmodel.PlaylistModel(playlist.id, None) assert model.rowCount() == 0 model._insert_row(session, 0) assert model.rowCount() == 1 def test_insert_high_row(monkeypatch, Session): monkeypatch.setattr(playlistmodel, "Session", Session) # Create a playlist with Session() as session: playlist = Playlists(Session, "test playlist") # Create a model model = playlistmodel.PlaylistModel(playlist.id, None) assert model.rowCount() == 0 model._insert_row(session, 5) assert model.rowCount() == 1 def test_11_row_playlist(monkeypatch, Session): # Create multirow playlist monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) assert model.rowCount() == 11 assert max(model.playlist_rows.keys()) == 10 for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row def test_move_rows_test2(monkeypatch, Session): # move row 3 to row 5 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([3], 5) # Check we have all rows and plr_rownums are correct for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row if row not in [3, 4, 5]: assert model.playlist_rows[row].note == str(row) elif row == 3: assert model.playlist_rows[row].note == str(4) elif row == 4: assert model.playlist_rows[row].note == str(5) elif row == 5: assert model.playlist_rows[row].note == str(3) def test_move_rows_test3(monkeypatch, Session): # move row 4 to row 3 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([4], 3) # Check we have all rows and plr_rownums are correct for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row if row not in [3, 4]: assert model.playlist_rows[row].note == str(row) elif row == 3: assert model.playlist_rows[row].note == str(4) elif row == 4: assert model.playlist_rows[row].note == str(3) def test_move_rows_test4(monkeypatch, Session): # move row 4 to row 2 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([4], 2) # Check we have all rows and plr_rownums are correct for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row if row not in [2, 3, 4]: assert model.playlist_rows[row].note == str(row) elif row == 2: assert model.playlist_rows[row].note == str(4) elif row == 3: assert model.playlist_rows[row].note == str(2) elif row == 4: assert model.playlist_rows[row].note == str(3) def test_move_rows_test5(monkeypatch, Session): # move rows [1, 4, 5, 10] → 8 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([1, 4, 5, 10], 8) # Check we have all rows and plr_rownums are correct new_order = [] for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row new_order.append(int(model.playlist_rows[row].note)) assert new_order == [0, 2, 3, 6, 7, 8, 9, 1, 4, 5, 10] def test_move_rows_test6(monkeypatch, Session): # move rows [3, 6] → 5 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([3, 6], 5) # Check we have all rows and plr_rownums are correct new_order = [] for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row new_order.append(int(model.playlist_rows[row].note)) assert new_order == [0, 1, 2, 4, 5, 3, 6, 7, 8, 9, 10] def test_move_rows_test7(monkeypatch, Session): # move rows [3, 5, 6] → 8 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([3, 5, 6], 8) # Check we have all rows and plr_rownums are correct new_order = [] for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row new_order.append(int(model.playlist_rows[row].note)) assert new_order == [0, 1, 2, 4, 7, 8, 9, 10, 3, 5, 6] def test_move_rows_test8(monkeypatch, Session): # move rows [7, 8, 10] → 5 monkeypatch.setattr(playlistmodel, "Session", Session) with Session() as session: model = create_model_with_playlist_rows(session, 11) model.move_rows([7, 8, 10], 5) # Check we have all rows and plr_rownums are correct new_order = [] for row in range(model.rowCount()): assert row in model.playlist_rows assert model.playlist_rows[row].plr_rownum == row new_order.append(int(model.playlist_rows[row].note)) assert new_order == [0, 1, 2, 3, 4, 7, 8, 10, 5, 6, 9] def test_insert_header_row(monkeypatch, Session): # insert header row monkeypatch.setattr(playlistmodel, "Session", Session) note_text = "test text" with Session() as session: model = create_model_with_playlist_rows(session, 11) row_number = model.insert_header_row(None, note_text) session.flush() assert model.rowCount() == row_number + 1 prd = model.playlist_rows[row_number] # Test against edit_role because display_role for headers is # handled differently (sets up row span) assert model.edit_role(row_number, playlistmodel.Col.NOTE.value, prd) == note_text