class Mixtape(): def __init__(self, options): self.input_file = self.validate_option(options['input']) self.output = self.validate_option(options['output']) self.changefile = self.validate_option(options['changefile']) self.file_controller = FileController() self.load_files() self.load_controllers() def validate_option(self, option): if option[0].endswith('.json'): return option[0] else: raise ValueError( 'Invalid file type. All file should be in json format.') def load_files(self): self.user_df, self.playlist_df, self.song_df = self.file_controller.build_dataframe( self.input_file) self.changes = self.file_controller.read_file( self.changefile)['changes'] def load_controllers(self): self.pl = PlaylistController(self.playlist_df) self.user = UserController(self.user_df) self.song = SongController(self.song_df) def run(self): for change in self.changes: if change['action'] == 'delete': self.playlist_df = self.pl.delete(change['playlist_id']) elif change['action'] == 'update': if self.song.exist(change['song_id']): self.playlist_df = self.pl.update(change['playlist_id'], change['song_id']) elif change['action'] == 'create': if self.user.exist(change['user_id']): if self.song.exist(change['song_ids']): self.playlist_df = self.pl.create( change['user_id'], change['song_ids']) else: raise ValueError('Invalid action: %s' % change['action']) self.file_controller.write_file(self.output, self.playlist_df) return None
def load_controllers(self): self.pl = PlaylistController(self.playlist_df) self.user = UserController(self.user_df) self.song = SongController(self.song_df)
def test_update_playlist_failed_with_invalid_playlist_id(self): with self.assertRaises(ValueError) as context: uc = PlaylistController(self.mock_data) uc.update(playlist_id=100, song_id=4) self.assertTrue('Playlist id, 100 doesnt exist' in context.exception)
def test_update_playlist_failed_with_invalid_song_id_format(self): with self.assertRaises(TypeError) as context: uc = PlaylistController(self.mock_data) uc.update(playlist_id=2, song_id=[4,3]) self.assertTrue('Invalid input. Please make sure song_id is in the correct format. Number in either string or integer format.' in context.exception)
def test_update_playlist_with_string(self): uc = PlaylistController(self.mock_data) uc.update(playlist_id=2, song_id='10') assert '10' in uc.pl_df['2']['song_ids']
def test_update_playlist_no_dupe(self): uc = PlaylistController(self.mock_data) uc.update(playlist_id=2, song_id=3) self.assertEqual(len(uc.pl_df['2']['song_ids']), len(self.mock_data['2']['song_ids']))
def test_create_playlist_with_multiple_songs(self): uc = PlaylistController(self.mock_data) uc.create(user_id=2, song_ids=[1,3,6]) self.assertEqual(uc.pl_df['4']['user_id'], '2') self.assertEqual(uc.pl_df['4']['song_ids'], ['1','3','6'])
def test_update_playlist_with_integer(self): uc = PlaylistController(self.mock_data) uc.update(playlist_id=2, song_id=4) assert '4' in uc.pl_df['2']['song_ids']
def test_create_playlist(self): uc = PlaylistController(self.mock_data) uc.create(user_id=2, song_ids=[1]) self.assertEqual(uc.pl_df['4']['user_id'], '2') self.assertEqual(uc.pl_df['4']['song_ids'], ['1'])
def test_new_playlist_id_empty_playlist(self): uc = PlaylistController({}) self.assertEqual(uc.new_playlist_id(), 1)
def test_new_playlist_id(self): uc = PlaylistController(self.mock_data) self.assertEqual(uc.new_playlist_id(), 4)
def test_delete_unexist_playlist(self): with self.assertRaises(ValueError) as context: uc = PlaylistController(self.mock_data) uc.delete(100) self.assertTrue('Can not delete playlist, 100 doesnt exist' in context.exception)
def test_delete_exist_playlist(self): uc = PlaylistController(self.mock_data) assert uc.delete(3)