def logged_in(self, session, error): track = mock_track("foo", 0, mock_album("bar", mock_artist("baz", 1), 0, "", 0), 0, 0, 0, 0, 0, 1) session.load(track) session.seek(40000) session.play(True) session.logout() self.disconnect()
class TestTrack(unittest.TestCase): track = mock_track("foo", 3, mock_album("bar", mock_artist("baz", 1), 0, "", 0, 1), 10, 20, 30, 40, 0, 1) def test_artists(self): self.assertEqual([x.name() for x in self.track.artists()], ["a1", "a2", "a3"]) def test_album(self): self.assertEqual(self.track.album().name(), "bar") def test_name(self): self.assertEqual(self.track.name(), "foo") def test_duration(self): self.assertEqual(self.track.duration(), 10) def test_popularity(self): self.assertEqual(self.track.popularity(), 20) def test_disc(self): self.assertEqual(self.track.disc(), 30) def test_index(self): self.assertEqual(self.track.index(), 40) def test_error(self): self.assertEqual(self.track.error(), 0) def test_is_loaded(self): self.assertEqual(self.track.is_loaded(), 1)
class TestAlbumbrowser(unittest.TestCase): artist = mock_artist("foo") album = mock_album("bar", artist) tracks = [ mock_track("baz1", [artist], album), mock_track("baz2", [artist], album), mock_track("baz3", [artist], album), ] browser = mock_albumbrowse(album, tracks, artist=artist, error=0) def callback(self, browser, userdata): global callback_called global callback_userdata callback_called = True callback_userdata = userdata def setUp(self): registry_add('spotify:album:1234', self.album) registry_add('spotify:albumbrowse:1234', self.browser) def tearDown(self): registry_clean() def test_is_loaded(self): assert self.browser.is_loaded() def test_sequence(self): assert len(self.browser) == 3 assert self.browser[0].name() == 'baz1' assert self.browser[1].name() == 'baz2' assert self.browser[2].name() == 'baz3' def test_browser(self): browser = AlbumBrowser(self.album) def test_browser_with_callback(self): global callback_called global callback_userdata callback_called = False browser = AlbumBrowser(self.album, self.callback, self) self.assertTrue(callback_called) self.assertEqual(callback_userdata, self)
def _mock_track(self, name): return mock_track( name, 0, mock_album("foo", mock_artist("bar", 1), 2006, "01234567890123456789", Album.ALBUM, 1, 1), 0, 0, 0, 0, 0, 1, )
class TestToplist(unittest.TestCase): artist1 = mock_artist('artist1') album1 = mock_album('album1', artist1) tracks1 = [ mock_track('track11', [artist1], album1), mock_track('track12', [artist1], album1), mock_track('track13', [artist1], album1), ] artist2 = mock_artist('artist2') album2 = mock_album('album2', artist2) tracks2 = [ mock_track('track21', [artist2], album2), mock_track('track22', [artist2], album2), mock_track('track23', [artist2], album2), ] albums = [album1, album2] artists = [artist1, artist2] tracks = tracks1 + tracks2 browser_albums = mock_toplistbrowse(albums, [], []) browser_artists = mock_toplistbrowse([], artists, []) browser_tracks = mock_toplistbrowse([], [], tracks) def setUp(self): registry_add('spotify:toplist:albums:FR', self.browser_albums) registry_add('spotify:toplist:artists:FR', self.browser_artists) registry_add('spotify:toplist:tracks:FR', self.browser_tracks) def tearDown(self): registry_clean() def test_is_loaded(self): browser = ToplistBrowser('tracks', 'FR') self.assertTrue(browser.is_loaded()) def test_sequence_albums(self): browser = ToplistBrowser('albums', 'FR') self.assertEqual([a.name() for a in browser], ['album1', 'album2']) def test_sequence_artists(self): browser = ToplistBrowser('artists', 'FR') self.assertEqual([a.name() for a in browser], ['artist1', 'artist2']) def test_sequence_tracks(self): browser = ToplistBrowser('tracks', 'FR') self.assertEqual( [t.name() for t in browser], ['track11', 'track12', 'track13', 'track21', 'track22', 'track23']) def test_callback(self): global callback_called global callback_userdata callback_called = False browser = ToplistBrowser('tracks', 'FR', callback, self) self.assertTrue(callback_called) self.assertEqual(callback_userdata, self)
class TestSearch(unittest.TestCase): artist1 = mock_artist('artist1') album1 = mock_album('album1', artist1) tracks1 = [ mock_track('track11', [artist1], album1), mock_track('track12', [artist1], album1), mock_track('track13', [artist1], album1), ] artist2 = mock_artist('artist2') album2 = mock_album('album2', artist2) tracks2 = [ mock_track('track21', [artist2], album2), mock_track('track22', [artist2], album2), mock_track('track23', [artist2], album2), ] albums = [album1, album2] artists = [artist1, artist2] tracks = tracks1 + tracks2 search = mock_search('query', tracks, albums, artists, 6, 2, 2, 'query2', 0) def test_search_is_loaded(self): self.assertEqual(self.search.is_loaded(), True) def test_artists(self): self.assertEqual([a.name() for a in self.search.artists()], ['artist1', 'artist2']) def test_albums(self): self.assertEqual([a.name() for a in self.search.albums()], ['album1', 'album2']) def test_tracks(self): self.assertEqual( [t.name() for t in self.search.tracks()], ['track11', 'track12', 'track13', 'track21', 'track22', 'track23']) def test_query(self): self.assertEqual(self.search.query(), "query") def test_error(self): self.assertEqual(self.search.error(), 0) def test_did_you_mean(self): self.assertEqual(self.search.did_you_mean(), "query2") def test_totals(self): self.assertEqual(self.search.total_albums(), 2) self.assertEqual(self.search.total_artists(), 2) self.assertEqual(self.search.total_tracks(), 6)
class TestPlaylistManager(unittest.TestCase): artist = mock_artist('artist') album = mock_album('album', artist) owner = mock_user('owner') tracks = [ (mock_track('track1', [artist], album), owner, 1320961109), (mock_track('track2', [artist], album), owner, 1320961109), (mock_track('track3', [artist], album), owner, 1320961109), ] pure_tracks = [t[0] for t in tracks] description = 'description' num_subscribers = 42 subscribers = ['sub1', 'sub2', 'sub3'] image = '01234567890123456789' playlist = mock_playlist('foo_', [], owner, subscribers, num_subscribers, description, image) def setUp(self): global callback_called self.manager = MyPlaylistManager() callback_called = None def test_tracks_added(self): global callback_called callback = self.manager.tracks_added self.playlist.add_tracks_added_callback(callback, self.manager) mock_event_trigger(20, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name,"tracks_added") self.assertEqual(len(args), 5) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), list) self.assertEqual(len(args[2]), 3) self.assertEqual(map(lambda x: x.name(), args[2]), ['foo', 'bar', 'baz']) self.assertEqual(type(args[3]), int) def test_tracks_moved(self): global callback_called callback = self.manager.tracks_moved self.playlist.add_tracks_moved_callback(callback, self.manager) mock_event_trigger(21, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "tracks_moved") self.assertEqual(len(args), 5) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), list) self.assertEqual(len(args[2]), 3) self.assertEqual(args[2], [0, 1, 2]) self.assertEqual(type(args[3]), int) def test_tracks_removed(self): global callback_called callback = self.manager.tracks_removed self.playlist.add_tracks_removed_callback(callback, self.manager) mock_event_trigger(22, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "tracks_removed") self.assertEqual(len(args), 4) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), list) self.assertEqual(len(args[2]), 3) self.assertEqual(args[2], [0, 1, 2]) def test_playlist_renamed(self): global callback_called callback = self.manager.playlist_renamed self.playlist.add_playlist_renamed_callback(callback, self.manager) mock_event_trigger(23, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "playlist_renamed") self.assertEqual(len(args), 3) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) def test_playlist_state_changed(self): global callback_called callback = self.manager.playlist_state_changed self.playlist.add_playlist_state_changed_callback(callback, self.manager) mock_event_trigger(24, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "playlist_state_changed") self.assertEqual(len(args), 3) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) def test_playlist_update_in_progress(self): global callback_called callback = self.manager.playlist_update_in_progress self.playlist.add_playlist_update_in_progress_callback(callback, self.manager) mock_event_trigger(25, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "playlist_update_in_progress") self.assertEqual(len(args), 4) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), bool) self.assertEqual(args[2], True) def test_playlist_metadata_updated(self): global callback_called callback = self.manager.playlist_metadata_updated self.playlist.add_playlist_metadata_updated_callback(callback, self.manager) mock_event_trigger(26, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "playlist_metadata_updated") self.assertEqual(len(args), 3) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) def test_track_created_changed(self): global callback_called callback = self.manager.track_created_changed self.playlist.add_track_created_changed_callback(callback, self.manager) mock_event_trigger(27, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "track_created_changed") self.assertEqual(len(args), 6) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), int) self.assertEqual(args[2], 1) self.assertEqual(type(args[3]), User) self.assertEqual(args[3].canonical_name(), u'foo') self.assertEqual(type(args[4]), int) self.assertEqual(args[4], 123) def test_track_message_changed(self): global callback_called callback = self.manager.track_message_changed self.playlist.add_track_message_changed_callback(callback, self.manager) mock_event_trigger(28, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "track_message_changed") self.assertEqual(len(args), 5) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), int) self.assertEqual(args[2], 1) self.assertEqual(type(args[3]), unicode) self.assertEqual(args[3], u'foo') def test_track_seen_changed(self): global callback_called callback = self.manager.track_seen_changed self.playlist.add_track_seen_changed_callback(callback, self.manager) mock_event_trigger(29, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "track_seen_changed") self.assertEqual(len(args), 5) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), int) self.assertEqual(args[2], 1) self.assertEqual(type(args[3]), bool) self.assertEqual(args[3], False) def test_description_changed(self): global callback_called callback = self.manager.description_changed self.playlist.add_description_changed_callback(callback, self.manager) mock_event_trigger(30, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "description_changed") self.assertEqual(len(args), 4) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), unicode) self.assertEqual(args[2], u'foo') def test_subscribers_changed(self): global callback_called callback = self.manager.subscribers_changed self.playlist.add_subscribers_changed_callback(callback, self.manager) mock_event_trigger(31, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "subscribers_changed") self.assertEqual(len(args), 3) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) def test_image_changed(self): global callback_called callback = self.manager.image_changed self.playlist.add_image_changed_callback(callback, self.manager) mock_event_trigger(32, self.playlist) self.assertNotEqual(callback_called, None) name, args = callback_called self.assertEqual(name, "image_changed") self.assertEqual(len(args), 4) self.assertEqual(args[0], self.manager) self.assertEqual(type(args[1]), Playlist) self.assertEqual(args[1].name(), self.playlist.name()) self.assertEqual(type(args[2]), bytes) self.assertEqual(args[2], '01234567890123456789')
class TestLink(unittest.TestCase): user = mock_user('user') artist = mock_artist('artist') album = mock_album('album', artist) playlist = mock_playlist('playlist', [], user) search = mock_search('query', [], [], []) track = mock_track('track', [artist], album) def setUp(self): registry_add('spotify:artist:test_artist', self.artist) registry_add('spotify:album:test_album', self.album) registry_add('spotify:playlist:test_playlist', self.playlist) registry_add('spotify:track:test_track', self.track) def tearDown(self): registry_clean() def test_from_string(self): s = "spotify:artist:test" l = Link.from_string(s) self.assertEqual(str(l), "spotify:artist:test") def test_from_track(self): l2 = Link.from_track(self.track, 42 * 1000) self.assertEqual(str(l2), "spotify:track:test_track#00:42") def test_from_album(self): l2 = Link.from_album(self.album) self.assertEqual(str(l2), "spotify:album:test_album") def test_from_artist(self): l = Link.from_artist(self.artist) self.assertEqual(str(l), "spotify:artist:test_artist") def test_from_search(self): l2 = Link.from_search(self.search) self.assertEqual(str(l2), "spotify:search:query") def test_from_playlist(self): l = Link.from_playlist(self.playlist) self.assertEqual(str(l), "spotify:playlist:test_playlist") def test_type(self): l = Link.from_track(self.track, 0) self.assertEqual(l.type(), Link.LINK_TRACK) def test_badlink(self): self.assertRaises(SpotifyError, Link.from_string, "BADLINK"); def test_as_track(self): l = Link.from_string("spotify:track:test_track") t = l.as_track() self.assertEqual(str(t), "track") def test_as_album(self): l = Link.from_string("spotify:album:test_album") t = l.as_album() self.assertEqual(str(t), "album") def test_as_artist(self): l = Link.from_string("spotify:artist:test_artist") a = l.as_artist() self.assertEqual(str(a), "artist") def test_as_string(self): s = "spotify:track:str_test" l = Link.from_string(s) self.assertEqual(str(l), "spotify:track:str_test")
class TestTrack(unittest.TestCase): artists = [mock_artist('artist1'), mock_artist('artist2')] album = mock_album('album1', artists[0]) track = mock_track(u'æâ€êþÿ', artists, album, 10, 20, 30, 40) track_linked = mock_track(u'linked', artists, album, 10, 20, 30, 40, is_autolinked=1, playable=track) def test_artists(self): self.assertEqual([x.name() for x in self.track.artists()], ['artist1', 'artist2']) def test_album(self): self.assertEqual(self.track.album().name(), "album1") def test_name(self): self.assertEqual(self.track.name(), u'æâ€êþÿ') def test_duration(self): self.assertEqual(self.track.duration(), 10) def test_popularity(self): self.assertEqual(self.track.popularity(), 20) def test_disc(self): self.assertEqual(self.track.disc(), 30) def test_index(self): self.assertEqual(self.track.index(), 40) def test_error(self): self.assertEqual(self.track.error(), 0) def test_is_loaded(self): self.assertEqual(self.track.is_loaded(), 1) def test_starred(self): session = mock_session() self.assertEqual(self.track.starred(session), False) self.track.starred(session, set=True) self.assertEqual(self.track.starred(session), True) self.track.starred(session, set=False) self.assertEqual(self.track.starred(session), False) def test_availability(self): self.assertEqual(self.track.availability(), 1) def test_is_local(self): self.assertFalse(self.track.is_local()) def test_is_autolinked(self): self.assertFalse(self.track.is_autolinked()) self.assertTrue(self.track_linked.is_autolinked()) def test_playable(self): self.assertEqual(self.track_linked.playable().name(), self.track.name())
def _mock_track(self, name): return mock_track( name, 0, mock_album("foo", mock_artist("bar", 1), 2006, "01234567890123456789", Album.ALBUM, 1, 1), 0, 0, 0, 0, 0, 1)
def test_type(self): t = mock_track("foo", 0, mock_album("bar", mock_artist("baz", 1), 0, "", 0, 1), 0, 0, 0, 0, 0, 1) l2 = Link.from_track(t, 42) self.assertEqual(l2.type(), Link.LINK_TRACK)
def test_from_track(self): t = mock_track("foo", 0, mock_album("bar", mock_artist("baz", 1), 0, "", 0, 1), 0, 0, 0, 0, 0, 1) l2 = Link.from_track(t, 42) self.assertEqual(str(l2), "link:track:foo/42")
class TestArtistbrowser(unittest.TestCase): artist = mock_artist("foo") similar_artists = [ mock_artist('artist1'), mock_artist('artist2'), mock_artist('artist3'), ] albums = [ mock_album("album1", artist), mock_album("album2", artist), mock_album("album3", artist), ] tracks = [ mock_track('track1', [artist], albums[0]), mock_track('track2', [artist], albums[0]), mock_track('track3', [artist], albums[0]), ] browser = mock_artistbrowse(artist, tracks, albums, similar_artists, 0) def callback(self, browser, userdata): global callback_called global callback_userdata callback_called = True callback_userdata = userdata def setUp(self): registry_add('spotify:artist:foo', self.artist) registry_add('spotify:artistbrowse:foo', self.browser) def tearDown(self): registry_clean() def test_is_loaded(self): self.assertTrue(self.browser.is_loaded()) def test_sequence(self): self.assertEqual([a.name() for a in self.browser], ['track1', 'track2', 'track3']) def test_albums(self): self.assertEqual([a.name() for a in self.browser.albums()], ['album1', 'album2', 'album3']) def test_artists(self): self.assertEqual([a.name() for a in self.browser.similar_artists()], ['artist1', 'artist2', 'artist3']) def test_tracks(self): self.assertEqual([a.name() for a in self.browser.tracks()], ['track1', 'track2', 'track3']) def test_browser(self): browser = ArtistBrowser(self.artist) def test_browser_with_callback(self): global callback_called global callback_userdata callback_called = False browser = ArtistBrowser(self.artist, 'full', self.callback, self) self.assertTrue(callback_called) self.assertEqual(callback_userdata, self)
class TestPlaylist(unittest.TestCase): artist = mock_artist('artist') album = mock_album('album', artist) owner = mock_user('owner') tracks = [ (mock_track('track1', [artist], album), owner, 1320961109), (mock_track('track2', [artist], album), owner, 1320961109), (mock_track('track3', [artist], album), owner, 1320961109), ] pure_tracks = [t[0] for t in tracks] session = mock_session() def setUp(self): mock_set_current_session(self.session) def tearDown(self): mock_set_current_session(None) def test_name(self): playlist = mock_playlist('playlist', [], self.owner) self.assertEqual(playlist.name(), 'playlist') def test_name_unicode(self): playlist = mock_playlist(u'plåylïst', [], self.owner) self.assertEqual(playlist.name(), u'plåylïst') def test_rename(self): playlist = mock_playlist(u'foo', [], self.owner) playlist.rename(u'bar') self.assertEqual(playlist.name(), u'bar') def test_rename_unicode(self): playlist = mock_playlist(u'foo', [], self.owner) playlist.rename(u'bąr') self.assertEqual(playlist.name(), u'bąr') @raises(ValueError) def test_rename_too_long(self): playlist = mock_playlist(u'foo', [], self.owner) playlist.rename(u'bar' * 100) def test_len(self): playlist = mock_playlist(u'foo', self.tracks, self.owner) self.assertEqual(len(playlist), 3) def test_sq_item(self): playlist = mock_playlist(u'foo', self.tracks, self.owner) self.assertEqual(playlist[0].name(), 'track1') self.assertEqual(playlist[1].name(), 'track2') self.assertEqual(playlist[2].name(), 'track3') def test_num_subscribers(self): playlist = mock_playlist('foo', [], self.owner, num_subscribers=42) self.assertEqual(playlist.num_subscribers(), 42) def test_subscribers(self): playlist = mock_playlist(u'foo', [], self.owner, subscribers=['foo', 'bar', 'baz']) self.assertEqual(playlist.subscribers(), [u'foo', u'bar', u'baz']) def test_add_tracks_ok(self): playlist = mock_playlist(u'foo', [], self.owner) playlist.add_tracks(0, self.pure_tracks) @raises(IndexError) def test_add_tracks_wrong_position(self): playlist = mock_playlist('foo', [], self.owner) playlist.add_tracks(99, self.pure_tracks) def test_add_tracks_wrong_types(self): playlist = mock_playlist('foo', [], self.owner) self.assertRaises(TypeError, playlist.add_tracks, 0, True) self.assertRaises(TypeError, playlist.add_tracks, [False]) def test_track_create_time(self): playlist = mock_playlist('foo', self.tracks, self.owner) self.assertEqual(playlist.track_create_time(0), 1320961109) def test_owner(self): playlist = mock_playlist('foo', [], self.owner) self.assertEqual(playlist.owner().canonical_name(), self.owner.canonical_name())