WIP V3: drag 'n' drop rows working with tests

This commit is contained in:
Keith Edmunds 2023-10-24 21:46:21 +01:00
parent 86a1678f41
commit 87172c8757
2 changed files with 112 additions and 31 deletions

View File

@ -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"""

View File

@ -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]