# Standard library imports import unittest # PyQt imports # Third party imports # App imports from app import playlistmodel from app import repository from app.models import db from classes import PlaylistDTO from playlistmodel import PlaylistModel class MyTestCase(unittest.TestCase): @classmethod def setUpClass(cls): """Runs once before any test in this class""" cls.isa_path = "testdata/isa.mp3" cls.isa_title = "I'm So Afraid" cls.isa_artist = "Fleetwood Mac" cls.mom_path = "testdata/mom.mp3" cls.mom_title = "Man of Mystery" cls.mom_artist = "The Shadows" @classmethod def tearDownClass(cls): """Runs once after all tests""" pass def setUp(self): """Runs before each test""" db.create_all() def create_playlist_and_model(self, playlist_name: str) -> (PlaylistDTO, PlaylistModel): # Create a playlist and model playlist = repository.create_playlist(name=playlist_name, template_id=0) assert playlist model = playlistmodel.PlaylistModel(playlist.playlist_id, is_template=False) assert model return (playlist, model) def create_playlist_model_tracks(self, playlist_name: str): (playlist, model) = self.create_playlist_and_model("my playlist") # Create tracks self.track1 = repository.create_track(self.isa_path) self.track2 = repository.create_track(self.mom_path) # Add tracks and header to playlist self.row0 = repository.insert_row( playlist.playlist_id, row_number=0, track_id=self.track1.track_id, note="track 1", ) self.row1 = repository.insert_row( playlist.playlist_id, row_number=1, track_id=0, note="Header row", ) self.row2 = repository.insert_row( playlist.playlist_id, row_number=2, track_id=self.track2.track_id, note="track 2", ) def create_rows(self, playlist_name: str, number_of_rows: int) -> (PlaylistDTO, PlaylistModel): (playlist, model) = self.create_playlist_and_model(playlist_name) for row_number in range(number_of_rows): repository.insert_row( playlist.playlist_id, row_number, None, str(row_number) ) return (playlist, model) def tearDown(self): """Runs after each test""" db.drop_all() def test_add_track_to_header(self): """Add a track to a header row""" self.create_playlist_model_tracks("my playlist") repository.add_track_to_header(self.row1.playlistrow_id, self.track2.track_id) result = repository.get_playlist_row(self.row1.playlistrow_id) assert result.track_id == self.track2.track_id def test_create_track(self): repository.create_track(self.isa_path) results = repository.get_all_tracks() assert len(results) == 1 assert results[0].path == self.isa_path def test_get_track_by_id(self): dto = repository.create_track(self.isa_path) result = repository.track_by_id(dto.track_id) assert result.path == self.isa_path def test_get_track_by_artist(self): _ = repository.create_track(self.isa_path) _ = repository.create_track(self.mom_path) result_isa = repository.tracks_like_artist(self.isa_artist) assert len(result_isa) == 1 assert result_isa[0].artist == self.isa_artist result_mom = repository.tracks_like_artist(self.mom_artist) assert len(result_mom) == 1 assert result_mom[0].artist == self.mom_artist def test_get_track_by_title(self): _ = repository.create_track(self.isa_path) _ = repository.create_track(self.mom_path) result_isa = repository.tracks_like_title(self.isa_title) assert len(result_isa) == 1 assert result_isa[0].title == self.isa_title result_mom = repository.tracks_like_title(self.mom_title) assert len(result_mom) == 1 assert result_mom[0].title == self.mom_title def test_move_rows_test1(self): # move row 3 to row 5 number_of_rows = 10 (playlist, model) = self.create_rows("test_move_rows_test1", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [3], 5) # 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, 4, 5, 3, 6, 7, 8, 9] def test_move_rows_test2(self): # move row 4 to row 3 number_of_rows = 10 (playlist, model) = self.create_rows("test_move_rows_test2", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [4], 3) # 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, 4, 3, 5, 6, 7, 8, 9] def test_move_rows_test3(self): # move row 4 to row 2 number_of_rows = 10 (playlist, model) = self.create_rows("test_move_rows_test3", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [4], 2) # 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, 4, 2, 3, 5, 6, 7, 8, 9] def test_move_rows_test4(self): # move rows [1, 4, 5, 10] → 8 number_of_rows = 11 (playlist, model) = self.create_rows("test_move_rows_test4", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [1, 4, 5, 10], 8) # 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, 2, 3, 6, 7, 1, 4, 5, 10, 8, 9] def test_move_rows_test5(self): # move rows [3, 6] → 5 number_of_rows = 11 (playlist, model) = self.create_rows("test_move_rows_test5", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [3, 6], 5) # 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, 4, 5, 3, 6, 7, 8, 9, 10] def test_move_rows_test8(self): # move rows [3, 5, 6] → 8 number_of_rows = 11 (playlist, model) = self.create_rows("test_move_rows_test6", number_of_rows) repository.move_rows_within_playlist(playlist.playlist_id, [3, 5, 6], 8) # 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, 4, 7, 3, 5, 6, 8, 9, 10] self.model.move_rows([3, 5, 6], 8) # 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] # def test_move_rows_test8(self): # # move rows [7, 8, 10] → 5 # 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]