def test_inbox_post_tracks(self, track_lib_mock, lib_mock): session = tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox result = spotify.InboxPostResult('alice', [track1, track2], '♥') lib_mock.sp_inbox_post_tracks.assert_called_with( session._sp_session, mock.ANY, mock.ANY, 2, mock.ANY, mock.ANY, mock.ANY) self.assertEqual( spotify.ffi.string(lib_mock.sp_inbox_post_tracks.call_args[0][1]), b'alice') self.assertIn(sp_track1, lib_mock.sp_inbox_post_tracks.call_args[0][2]) self.assertIn(sp_track2, lib_mock.sp_inbox_post_tracks.call_args[0][2]) self.assertEqual( spotify.ffi.string(lib_mock.sp_inbox_post_tracks.call_args[0][4]), b'\xe2\x99\xa5') self.assertIsInstance(result, spotify.InboxPostResult) self.assertEqual(result._sp_inbox, sp_inbox) self.assertFalse(result.complete_event.is_set()) inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) self.assertTrue(result.complete_event.wait(3))
def test_eq(self, lib_mock): sp_track = spotify.ffi.cast('sp_track *', 42) track1 = spotify.Track(self.session, sp_track=sp_track) track2 = spotify.Track(self.session, sp_track=sp_track) self.assertTrue(track1 == track2) self.assertFalse(track1 == 'foo')
def test_inbox_post_where_result_is_gone_before_callback_is_called( self, track_lib_mock, lib_mock): sp_track1 = spotify.ffi.cast('sp_track *', 43) track1 = spotify.Track(self.session, sp_track=sp_track1) sp_track2 = spotify.ffi.cast('sp_track *', 44) track2 = spotify.Track(self.session, sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', 42) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox callback = mock.Mock() result = spotify.InboxPostResult(self.session, 'alice', [track1, track2], callback=callback) loaded_event = result.loaded_event result = None # noqa tests.gc_collect() # The mock keeps the handle/userdata alive, thus this test doesn't # really test that session._callback_handles keeps the handle alive. inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) loaded_event.wait(3) self.assertEqual(callback.call_count, 1) self.assertEqual(callback.call_args[0][0]._sp_inbox, sp_inbox)
def test_inbox_post_where_result_is_gone_before_callback_is_called( self, track_lib_mock, lib_mock): tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox callback = mock.Mock() result = spotify.InboxPostResult('alice', [track1, track2], callback=callback) complete_event = result.complete_event result = None # noqa tests.gc_collect() # FIXME The mock keeps the handle/userdata alive, thus the search # result is kept alive, and this test doesn't test what it is intended # to test. inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) complete_event.wait(3) self.assertEqual(callback.call_count, 1) self.assertEqual(callback.call_args[0][0]._sp_inbox, sp_inbox)
def test_fail_to_init_raises_error(self, track_lib_mock, lib_mock): sp_track1 = spotify.ffi.cast('sp_track *', 43) track1 = spotify.Track(self.session, sp_track=sp_track1) sp_track2 = spotify.ffi.cast('sp_track *', 44) track2 = spotify.Track(self.session, sp_track=sp_track2) lib_mock.sp_inbox_post_tracks.return_value = spotify.ffi.NULL with self.assertRaises(spotify.Error): spotify.InboxPostResult(self.session, 'alice', [track1, track2], 'Enjoy!')
def test_fail_to_init_raises_error(self, track_lib_mock, lib_mock): tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) lib_mock.sp_inbox_post_tracks.return_value = spotify.ffi.NULL with self.assertRaises(spotify.Error): spotify.InboxPostResult('alice', [track1, track2], 'Enjoy!')
def test_create_from_uri(self, link_mock, lib_mock): sp_track = spotify.ffi.cast('sp_track *', 42) link_instance_mock = link_mock.return_value link_instance_mock.as_track.return_value = spotify.Track( self.session, sp_track=sp_track) uri = 'spotify:track:foo' result = spotify.Track(self.session, uri=uri) link_mock.assert_called_with(self.session, uri=uri) link_instance_mock.as_track.assert_called_with() lib_mock.sp_track_add_ref.assert_called_with(sp_track) self.assertEqual(result._sp_track, sp_track)
def test_add_tracks(self, track_lib_mock, lib_mock): lib_mock.sp_playlist_add_tracks.return_value = int(spotify.ErrorType.OK) sp_track1 = spotify.ffi.new('int * ') track1 = spotify.Track(self.session, sp_track=sp_track1) sp_track2 = spotify.ffi.new('int * ') track2 = spotify.Track(self.session, sp_track=sp_track2) sp_playlist = spotify.ffi.cast('sp_playlist *', 42) playlist = spotify.Playlist(self.session, sp_playlist=sp_playlist) playlist.add_tracks([track1, track2], index=4) lib_mock.sp_playlist_add_tracks.assert_called_with( sp_playlist, [sp_track1, sp_track2], 2, 4, self.session._sp_session )
def main(): parser = argparse.ArgumentParser(description = 'Export Spotify playlist to MP3.') parser.add_argument('-f', '--filename', help='File holding list of HTTP spotify URIs.', required = True) args = vars(parser.parse_args()) groove.getToken() with open( args['filename'], 'rb') as input: for line in input: print "~"*80 sp_track = spotify.Track(line) cout.header(str(sp_track)) if sp_track.getFileExists("."): cout.warning("File alredy exits") continue gs_tracks = compare.get_groove_tracks(groove.getResultsFromSearch(sp_track.getName())) match_score, match_track = sp_track.get_bets_match(gs_tracks) if not match_track: cout.warning("No match") continue groove.downloadSong(match_track.meta, sp_track.getFileName(), groove.chunk_report)
def assert_fails_if_no_session(self, lib_mock, func): spotify.session_instance = None sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) with self.assertRaises(RuntimeError): func(track)
def test_load(self, load_mock, lib_mock): sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) track.load(10) load_mock.assert_called_with(track, timeout=10)
def test_load(self, load_mock, lib_mock): sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) track.load(10) load_mock.assert_called_with(self.session, track, timeout=10)
def assert_fails_if_error(self, lib_mock, func): lib_mock.sp_track_error.return_value = spotify.ErrorType.BAD_API_VERSION sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) with self.assertRaises(spotify.Error): func(track)
def track(self): """The :class:`~spotify.Track`.""" return spotify.Track( self._session, sp_track=lib.sp_playlist_track(self._sp_playlist, self._index), add_ref=True, )
def test_create_from_uri_fail_raises_error(self, link_mock, lib_mock): link_instance_mock = link_mock.return_value link_instance_mock.as_track.return_value = None uri = 'spotify:track:foo' with self.assertRaises(ValueError): spotify.Track(self.session, uri=uri)
def test_releases_sp_track_when_track_dies(self, lib_mock): sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) track = None # noqa tests.gc_collect() lib_mock.sp_track_release.assert_called_with(sp_track)
def test_releases_sp_track_when_track_dies(self, lib_mock): sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) track = None # noqa tests.gc_collect() lib_mock.sp_track_release.assert_called_with(sp_track)
def test_offline_status_fails_if_error(self, lib_mock): lib_mock.sp_track_error.return_value = spotify.ErrorType.BAD_API_VERSION lib_mock.sp_track_offline_get_status.return_value = 2 sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) with self.assertRaises(spotify.Error): track.offline_status
def test_repr(self, link_mock, lib_mock): link_instance_mock = link_mock.return_value link_instance_mock.uri = 'foo' sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) result = repr(track) self.assertEqual(result, 'Track(%r)' % 'foo')
def test_set_starred_fails_if_error(self, lib_mock): tests.create_session_mock() lib_mock.sp_track_set_starred.return_value = ( spotify.ErrorType.BAD_API_VERSION) sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) with self.assertRaises(spotify.Error): track.starred = True
def test_set_starred(self, lib_mock): lib_mock.sp_track_set_starred.return_value = spotify.ErrorType.OK sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) track.starred = True lib_mock.sp_track_set_starred.assert_called_with( self.session._sp_session, mock.ANY, 1, 1)
def test_player_load_fail_raises_error(self, track_lib_mock, lib_mock): lib_mock.sp_session_player_load.return_value = ( spotify.ErrorType.TRACK_NOT_PLAYABLE) session = create_session(lib_mock) sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) with self.assertRaises(spotify.Error): session.player.load(track)
def assert_fails_if_error(self, lib_mock, func): tests.create_session() lib_mock.sp_track_error.return_value = ( spotify.ErrorType.BAD_API_VERSION) sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) with self.assertRaises(spotify.Error): func(track)
def test_is_loaded(self, lib_mock): lib_mock.sp_track_is_loaded.return_value = 1 sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(self.session, sp_track=sp_track) result = track.is_loaded lib_mock.sp_track_is_loaded.assert_called_once_with(sp_track) self.assertTrue(result)
def test_repr(self, link_mock, lib_mock): link_instance_mock = link_mock.return_value link_instance_mock.uri = 'foo' sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) result = repr(track) self.assertEqual(result, 'Track(%r)' % 'foo')
def test_is_loaded(self, lib_mock): lib_mock.sp_track_is_loaded.return_value = 1 sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) result = track.is_loaded lib_mock.sp_track_is_loaded.assert_called_once_with(sp_track) self.assertTrue(result)
def test_offline_status_fails_if_error(self, lib_mock): lib_mock.sp_track_error.return_value = ( spotify.ErrorType.BAD_API_VERSION) lib_mock.sp_track_offline_get_status.return_value = 2 sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) with self.assertRaises(spotify.Error): track.offline_status
def test_player_prefetch_fail_raises_error(self, track_lib_mock, lib_mock): lib_mock.sp_session_player_prefetch.return_value = ( spotify.ErrorType.NO_CACHE) session = create_session(lib_mock) sp_track = spotify.ffi.new('int *') track = spotify.Track(sp_track=sp_track) with self.assertRaises(spotify.Error): session.player.prefetch(track)
def play_track(self, track_uri): next_track = spotify.Track(self.session, uri=track_uri).load() logger.debug('In play_track :: next_track.name = {}'.format(next_track.name)) self.session.player.load(next_track) self.session.player.play() self.current_track = next_track self.current_track_uri = track_uri self.is_playing = True self.end_of_track_event.clear()
def test_player_load_fail_raises_error(self, track_lib_mock, session_lib_mock, lib_mock): lib_mock.sp_session_player_load.return_value = ( spotify.ErrorType.TRACK_NOT_PLAYABLE) session = tests.create_real_session(session_lib_mock) sp_track = spotify.ffi.cast('sp_track *', 42) track = spotify.Track(session, sp_track=sp_track) with self.assertRaises(spotify.Error): session.player.load(track)