# 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([3], playlist.playlist_id, 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([4], playlist.playlist_id, 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([4], playlist.playlist_id, 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([1, 4, 5, 10], playlist.playlist_id, 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, 8, 1, 4, 5, 10, 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([3, 6], playlist.playlist_id, 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_test6(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([3, 5, 6], playlist.playlist_id, 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, 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_test7", number_of_rows) repository.move_rows([7, 8, 10], playlist.playlist_id, 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, 3, 4, 7, 8, 10, 5, 6, 9] def test_move_rows_test8(self): # move rows [1, 2, 3] → 0 # Replicate issue 244 number_of_rows = 11 (playlist, model) = self.create_rows("test_move_rows_test8", number_of_rows) repository.move_rows([0, 1, 2, 3], playlist.playlist_id, 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] def test_move_rows_to_playlist(self): number_of_rows = 11 rows_to_move = [2, 4, 6] to_row = 5 (playlist_src, model_src) = self.create_rows("src playlist", number_of_rows) (playlist_dst, model_dst) = self.create_rows("dst playlist", number_of_rows) repository.move_rows( rows_to_move, playlist_src.playlist_id, to_row, playlist_dst.playlist_id ) # Check we have all rows and plr_rownums are correct new_order_src = [] for row in repository.get_playlist_rows(playlist_src.playlist_id): new_order_src.append(int(row.note)) assert new_order_src == [0, 1, 3, 5, 7, 8, 9, 10] new_order_dst = [] for row in repository.get_playlist_rows(playlist_dst.playlist_id): new_order_dst.append(int(row.note)) assert new_order_dst == [0, 1, 2, 3, 4, 2, 4, 6, 5, 6, 7, 8, 9, 10] def test_remove_rows(self): pass def test_get_playlist_by_id(self): pass def test_settings(self): pass