diff --git a/app/repository.py b/app/repository.py index 5330b7a..c81acb1 100644 --- a/app/repository.py +++ b/app/repository.py @@ -332,11 +332,21 @@ def move_rows_within_playlist(playlist_id: int, from_rows: list[int], to_row: in playlistrows_dto = get_playlist_rows(playlist_id) new_order: dict[int, int | None] = dict.fromkeys(range(len(playlistrows_dto))) - # Populate new_order with moved rows + # The destination row number will need to be reduced by the + # number of rows being move from above the destination row + # otherwise rows below the destination row will end up above the + # moved rows. + # next_row = to_row - len([a for a in from_rows if a < to_row]) + # Need to ensure the moved rows won't overrun the total number of + # rows next_row = to_row - # We need to keep, where possible, the rows after to_row unmoved - if to_row + len(from_rows) > len(playlistrows_dto): - next_row = max(to_row - len(from_rows) + 1, 0) + if next_row + len(from_rows) > len(playlistrows_dto): + next_row = len(playlistrows_dto) - len(from_rows) + + # Populate new_order with moved rows + # # We need to keep, where possible, the rows after to_row unmoved + # if to_row + len(from_rows) > len(playlistrows_dto): + # next_row = max(to_row - len(from_rows) - len([a for a in from_rows if a < to_row]) + 1, 0) for from_row in from_rows: new_order[next_row] = from_row next_row += 1 diff --git a/tests/test_repository.py b/tests/test_repository.py index 9dfc3e0..f2a8ce6 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -179,7 +179,7 @@ class MyTestCase(unittest.TestCase): new_order = [] for row in repository.get_playlist_rows(playlist.playlist_id): new_order.append(int(row.note)) - assert new_order == [0, 2, 3, 6, 7, 1, 4, 5, 10, 8, 9] + assert new_order == [0, 2, 3, 6, 7, 8, 9, 1, 4, 5, 10] def test_move_rows_test5(self): # move rows [3, 6] → 5 @@ -195,7 +195,7 @@ class MyTestCase(unittest.TestCase): new_order.append(int(row.note)) assert new_order == [0, 1, 2, 4, 5, 3, 6, 7, 8, 9, 10] - def test_move_rows_test8(self): + def test_move_rows_test6(self): # move rows [3, 5, 6] → 8 number_of_rows = 11 @@ -207,46 +207,33 @@ class MyTestCase(unittest.TestCase): new_order = [] for row in repository.get_playlist_rows(playlist.playlist_id): new_order.append(int(row.note)) - assert new_order == [0, 1, 2, 4, 7, 3, 5, 6, 8, 9, 10] + assert new_order == [0, 1, 2, 4, 7, 8, 9, 10, 3, 5, 6] + def test_move_rows_test7(self): + # move rows [7, 8, 10] → 5 + number_of_rows = 11 + (playlist, model) = self.create_rows("test_move_rows_test6", number_of_rows) - - self.model.move_rows([3, 5, 6], 8) + repository.move_rows_within_playlist(playlist.playlist_id, [7, 8, 10], 5) # Check we have all rows and plr_rownums are correct new_order = [] - for row in range(self.model.rowCount()): - assert row in self.model.playlist_rows - assert self.model.playlist_rows[row].row_number == row - new_order.append(int(self.model.playlist_rows[row].note)) - assert new_order == [0, 1, 2, 4, 7, 3, 5, 6, 8, 9, 10] + for row in repository.get_playlist_rows(playlist.playlist_id): + new_order.append(int(row.note)) + assert new_order == [0, 1, 2, 3, 4, 7, 8, 10, 5, 6, 9] - # def test_move_rows_test8(self): - # # move rows [7, 8, 10] → 5 + def test_move_rows_test8(self): + # move rows [1, 2, 3] → 0 + # Replicate issue 244 - # self.model.move_rows([7, 8, 10], 5) - - # # Check we have all rows and plr_rownums are correct - # new_order = [] - # for row in range(self.model.rowCount()): - # assert row in self.model.playlist_rows - # assert self.model.playlist_rows[row].row_number == row - # new_order.append(int(self.model.playlist_rows[row].note)) - # assert new_order == [0, 1, 2, 3, 4, 7, 8, 10, 5, 6, 9] - - # def test_move_rows_test9(self): - # # move rows [1, 2, 3] → 0 - # # Replicate issue 244 - - # self.model.move_rows([0, 1, 2, 3], 0) - - # # Check we have all rows and plr_rownums are correct - # new_order = [] - # for row in range(self.model.rowCount()): - # assert row in self.model.playlist_rows - # assert self.model.playlist_rows[row].row_number == row - # new_order.append(int(self.model.playlist_rows[row].note)) - # assert new_order == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + number_of_rows = 11 + (playlist, model) = self.create_rows("test_move_rows_test6", number_of_rows) + repository.move_rows_within_playlist(playlist.playlist_id, [0, 1, 2, 3], 0) + # Check we have all rows and plr_rownums are correct + new_order = [] + for row in repository.get_playlist_rows(playlist.playlist_id): + new_order.append(int(row.note)) + assert new_order == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]