diff --git a/app/playlistmodel.py b/app/playlistmodel.py index be43a23..def80ab 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -335,21 +335,17 @@ class PlaylistModel(QAbstractTableModel): Move the playlist rows given to to_row and below. """ - # New thinking: - # Move relocated rows to correct place in mirror array - # Copy souurce to mirror around them - # Move mirror to original - # Fixup plr rownumbers and update db and display - # Signal rows have changed - - # Prep - # modified_rows: List[int] = [] - # moving_rows: dict[int, PlaylistRowData] = {} new_playlist_rows: dict[int, PlaylistRowData] = {} # Move the from_row records from the playlist_rows dict to the - # new_playlist_rows dict - next_to_row = to_row + # new_playlist_rows dict. The total number of elements in the + # playlist doesn't change, so check that adding the moved rows + # starting at to_row won't overshoot the end of the playlist. + if to_row + len(from_rows) > len(self.playlist_rows): + next_to_row = len(self.playlist_rows) - len(from_rows) + else: + next_to_row = to_row + for from_row in from_rows: new_playlist_rows[next_to_row] = self.playlist_rows[from_row] del self.playlist_rows[from_row] @@ -384,7 +380,6 @@ class PlaylistModel(QAbstractTableModel): self.playlist_rows[idx].plr_rownum = idx # Update display self.invalidate_rows([idx]) - print(f"Fixup {idx=}") def refresh_data(self): """Populate dicts for data calls""" diff --git a/test_playlistmodel.py b/test_playlistmodel.py index 80e8573..5513fba 100644 --- a/test_playlistmodel.py +++ b/test_playlistmodel.py @@ -75,31 +75,117 @@ def test_move_rows_test2(monkeypatch, Session): assert model.playlist_rows[row].note == str(3) -# def test_move_rows_test3(Session): -# # move row 4 to row 3 -# pass +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(Session): -# # move row 4 to row 2 -# pass +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(Session): -# # move rows [1, 4, 5, 10] → 8 -# pass +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(Session): -# # move rows [3, 6] → 5 -# pass +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(Session): -# # move rows [3, 5, 6] → 8 -# pass +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(Session): -# # move rows [7, 8, 10] → 5 -# pass +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]