Ejemplo n.º 1
0
    def test_ne(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist1 = spotify.Toplist(self.session,
                                   sp_toplistbrowse=sp_toplistbrowse)
        toplist2 = spotify.Toplist(self.session,
                                   sp_toplistbrowse=sp_toplistbrowse)

        self.assertFalse(toplist1 != toplist2)
Ejemplo n.º 2
0
    def test_hash(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist1 = spotify.Toplist(self.session,
                                   sp_toplistbrowse=sp_toplistbrowse)
        toplist2 = spotify.Toplist(self.session,
                                   sp_toplistbrowse=sp_toplistbrowse)

        self.assertEqual(hash(toplist1), hash(toplist2))
Ejemplo n.º 3
0
    def test_load(self, load_mock, lib_mock):
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        toplist.load(10)

        load_mock.assert_called_with(toplist, timeout=10)
Ejemplo n.º 4
0
    def test_adds_ref_to_sp_toplistbrowse_when_created(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)

        spotify.Toplist(self.session, sp_toplistbrowse=sp_toplistbrowse)

        lib_mock.sp_toplistbrowse_add_ref.assert_called_once_with(
            sp_toplistbrowse)
Ejemplo n.º 5
0
    def test_adds_ref_to_sp_toplistbrowse_when_created(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.new('int *')

        spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        lib_mock.sp_toplistbrowse_add_ref.assert_called_once_with(
            sp_toplistbrowse)
Ejemplo n.º 6
0
    def test_toplist_is_gone_before_callback_is_called(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse
        callback = mock.Mock()

        result = spotify.Toplist(
            self.session,
            type=spotify.ToplistType.TRACKS,
            region=spotify.ToplistRegion.USER,
            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.
        toplistbrowse_complete_cb = lib_mock.sp_toplistbrowse_create.call_args[
            0][4]
        userdata = lib_mock.sp_toplistbrowse_create.call_args[0][5]
        toplistbrowse_complete_cb(sp_toplistbrowse, userdata)

        loaded_event.wait(3)
        self.assertEqual(callback.call_count, 1)
        self.assertEqual(callback.call_args[0][0]._sp_toplistbrowse,
                         sp_toplistbrowse)
Ejemplo n.º 7
0
    def test_toplist_is_gone_before_callback_is_called(self, lib_mock):
        tests.create_session()
        sp_toplistbrowse = spotify.ffi.cast(
            'sp_toplistbrowse *', spotify.ffi.new('int *'))
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse
        callback = mock.Mock()

        result = spotify.Toplist(
            type=spotify.ToplistType.TRACKS, region=spotify.ToplistRegion.USER,
            callback=callback)
        complete_event = result.complete_event
        result = None  # noqa
        tests.gc_collect()

        # FIXME The mock keeps the handle/userdata alive, thus the toplist is
        # kept alive, and this test doesn't test what it is intended to test.
        toplistbrowse_complete_cb = (
            lib_mock.sp_toplistbrowse_create.call_args[0][4])
        userdata = lib_mock.sp_toplistbrowse_create.call_args[0][5]
        toplistbrowse_complete_cb(sp_toplistbrowse, userdata)

        complete_event.wait(3)
        self.assertEqual(callback.call_count, 1)
        self.assertEqual(
            callback.call_args[0][0]._sp_toplistbrowse, sp_toplistbrowse)
Ejemplo n.º 8
0
    def assert_fails_if_error(self, lib_mock, func):
        lib_mock.sp_toplistbrowse_error.return_value = (
            spotify.ErrorType.BAD_API_VERSION)
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        with self.assertRaises(spotify.Error):
            func(toplist)
Ejemplo n.º 9
0
    def test_releases_sp_toplistbrowse_when_toplist_dies(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.new('int *')

        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)
        toplist = None  # noqa
        tests.gc_collect()

        lib_mock.sp_toplistbrowse_release.assert_called_with(sp_toplistbrowse)
Ejemplo n.º 10
0
    def test_load(self, load_mock, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)

        toplist.load(10)

        load_mock.assert_called_with(self.session, toplist, timeout=10)
Ejemplo n.º 11
0
    def test_releases_sp_toplistbrowse_when_toplist_dies(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)

        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)
        toplist = None  # noqa
        tests.gc_collect()

        lib_mock.sp_toplistbrowse_release.assert_called_with(sp_toplistbrowse)
Ejemplo n.º 12
0
    def test_backend_request_duration(self, lib_mock):
        lib_mock.sp_toplistbrowse_backend_request_duration.return_value = 137
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.backend_request_duration

        lib_mock.sp_toplistbrowse_backend_request_duration.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(result, 137)
Ejemplo n.º 13
0
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_toplistbrowse_is_loaded.return_value = 1
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.is_loaded

        lib_mock.sp_toplistbrowse_is_loaded.assert_called_once_with(
            sp_toplistbrowse)
        self.assertTrue(result)
Ejemplo n.º 14
0
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_toplistbrowse_is_loaded.return_value = 1
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.is_loaded

        lib_mock.sp_toplistbrowse_is_loaded.assert_called_once_with(
            sp_toplistbrowse)
        self.assertTrue(result)
Ejemplo n.º 15
0
    def test_artists_if_unloaded(self, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        lib_mock.sp_toplistbrowse_is_loaded.return_value = 0
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.artists

        lib_mock.sp_toplistbrowse_is_loaded.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(len(result), 0)
Ejemplo n.º 16
0
    def test_create_from_type_and_country(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse

        spotify.Toplist(self.session,
                        type=spotify.ToplistType.TRACKS,
                        region='NO')

        lib_mock.sp_toplistbrowse_create.assert_called_with(
            self.session._sp_session, int(spotify.ToplistType.TRACKS), 20047,
            spotify.ffi.NULL, mock.ANY, mock.ANY)
Ejemplo n.º 17
0
    def test_error(self, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = int(
            spotify.ErrorType.OTHER_PERMANENT)
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.error

        lib_mock.sp_toplistbrowse_error.assert_called_once_with(
            sp_toplistbrowse)
        self.assertIs(result, spotify.ErrorType.OTHER_PERMANENT)
Ejemplo n.º 18
0
    def test_artists_if_no_artists(self, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        lib_mock.sp_toplistbrowse_num_artists.return_value = 0
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.artists

        self.assertEqual(len(result), 0)
        lib_mock.sp_toplistbrowse_num_artists.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(lib_mock.sp_toplistbrowse_artist.call_count, 0)
Ejemplo n.º 19
0
    def test_backend_request_duration_when_not_loaded(self, lib_mock):
        lib_mock.sp_toplistbrowse_is_loaded.return_value = 0
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.backend_request_duration

        lib_mock.sp_toplistbrowse_is_loaded.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(
            lib_mock.sp_toplistbrowse_backend_request_duration.call_count, 0)
        self.assertIsNone(result)
Ejemplo n.º 20
0
    def test_repr(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse
        toplist = spotify.Toplist(self.session,
                                  type=spotify.ToplistType.TRACKS,
                                  region='NO')

        result = repr(toplist)

        self.assertEqual(
            result, "Toplist(type=<ToplistType.TRACKS: 2>, region=%r, "
            "canonical_username=None)" % 'NO')
Ejemplo n.º 21
0
    def test_albums_if_unloaded(self, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        lib_mock.sp_toplistbrowse_is_loaded.return_value = 0
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.albums

        lib_mock.sp_toplistbrowse_is_loaded.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(len(result), 0)
Ejemplo n.º 22
0
    def test_albums_if_no_albums(self, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        lib_mock.sp_toplistbrowse_num_albums.return_value = 0
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)

        result = toplist.albums

        self.assertEqual(len(result), 0)
        lib_mock.sp_toplistbrowse_num_albums.assert_called_with(
            sp_toplistbrowse)
        self.assertEqual(lib_mock.sp_toplistbrowse_album.call_count, 0)
Ejemplo n.º 23
0
    def test_create_from_type_and_current_user_region(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse

        result = spotify.Toplist(self.session,
                                 type=spotify.ToplistType.TRACKS,
                                 region=spotify.ToplistRegion.USER)

        lib_mock.sp_toplistbrowse_create.assert_called_with(
            self.session._sp_session, int(spotify.ToplistType.TRACKS),
            int(spotify.ToplistRegion.USER), spotify.ffi.NULL, mock.ANY,
            mock.ANY)
        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 0)
        self.assertEqual(result._sp_toplistbrowse, sp_toplistbrowse)
Ejemplo n.º 24
0
    def test_create_from_type_and_specific_user_region(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse

        spotify.Toplist(self.session,
                        type=spotify.ToplistType.TRACKS,
                        region=spotify.ToplistRegion.USER,
                        canonical_username='******')

        lib_mock.sp_toplistbrowse_create.assert_called_with(
            self.session._sp_session, int(spotify.ToplistType.TRACKS),
            int(spotify.ToplistRegion.USER), mock.ANY, mock.ANY, mock.ANY)
        self.assertEqual(
            spotify.ffi.string(
                lib_mock.sp_toplistbrowse_create.call_args[0][3]), b'alice')
Ejemplo n.º 25
0
    def test_create_with_callback(self, lib_mock):
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        lib_mock.sp_toplistbrowse_create.return_value = sp_toplistbrowse
        callback = mock.Mock()

        result = spotify.Toplist(self.session,
                                 type=spotify.ToplistType.TRACKS,
                                 region=spotify.ToplistRegion.USER,
                                 callback=callback)

        toplistbrowse_complete_cb = (
            lib_mock.sp_toplistbrowse_create.call_args[0][4])
        userdata = lib_mock.sp_toplistbrowse_create.call_args[0][5]
        toplistbrowse_complete_cb(sp_toplistbrowse, userdata)

        result.loaded_event.wait(3)
        callback.assert_called_with(result)
Ejemplo n.º 26
0
    def get_toplist(self,
                    type=None,
                    region=None,
                    canonical_username=None,
                    callback=None):
        """Get a :class:`Toplist` of artists, albums, or tracks that are the
        currently most popular worldwide or in a specific region.

        ``type`` is a :class:`ToplistType` instance that specifies the type of
        toplist to create.

        ``region`` is either a :class:`ToplistRegion` instance, or a 2-letter
        ISO 3166-1 country code as a unicode string, that specifies the
        geographical region to create a toplist for.

        If ``region`` is :attr:`ToplistRegion.USER` and ``canonical_username``
        isn't specified, the region of the current user will be used. If
        ``canonical_username`` is specified, the region of the specified user
        will be used instead.

        If ``callback`` isn't :class:`None`, it is expected to be a callable
        that accepts a single argument, a :class:`Toplist` instance, when the
        toplist request completes.

        Example::

            >>> import spotify
            >>> session = spotify.Session()
            # ...
            >>> toplist = session.get_toplist(
            ...     type=spotify.ToplistType.TRACKS, region='US')
            >>> toplist.load()
            >>> len(toplist.tracks)
            100
            >>> len(toplist.artists)
            0
            >>> toplist.tracks[0]
            Track(u'spotify:track:2dLLR6qlu5UJ5gk0dKz0h3')
        """
        return spotify.Toplist(
            self,
            type=type,
            region=region,
            canonical_username=canonical_username,
            callback=callback,
        )
Ejemplo n.º 27
0
    def test_tracks(self, track_lib_mock, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        sp_track = spotify.ffi.cast('sp_track *', spotify.ffi.new('int *'))
        lib_mock.sp_toplistbrowse_num_tracks.return_value = 1
        lib_mock.sp_toplistbrowse_track.return_value = sp_track
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 1)
        result = toplist.tracks
        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 2)

        self.assertEqual(len(result), 1)
        lib_mock.sp_toplistbrowse_num_tracks.assert_called_with(
            sp_toplistbrowse)

        item = result[0]
        self.assertIsInstance(item, spotify.Track)
        self.assertEqual(item._sp_track, sp_track)
        self.assertEqual(lib_mock.sp_toplistbrowse_track.call_count, 1)
        lib_mock.sp_toplistbrowse_track.assert_called_with(sp_toplistbrowse, 0)
        track_lib_mock.sp_track_add_ref.assert_called_with(sp_track)
Ejemplo n.º 28
0
    def test_albums(self, album_lib_mock, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        sp_album = spotify.ffi.cast('sp_album *', spotify.ffi.new('int *'))
        lib_mock.sp_toplistbrowse_num_albums.return_value = 1
        lib_mock.sp_toplistbrowse_album.return_value = sp_album
        sp_toplistbrowse = spotify.ffi.new('int *')
        toplist = spotify.Toplist(sp_toplistbrowse=sp_toplistbrowse)

        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 1)
        result = toplist.albums
        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 2)

        self.assertEqual(len(result), 1)
        lib_mock.sp_toplistbrowse_num_albums.assert_called_with(
            sp_toplistbrowse)

        item = result[0]
        self.assertIsInstance(item, spotify.Album)
        self.assertEqual(item._sp_album, sp_album)
        self.assertEqual(lib_mock.sp_toplistbrowse_album.call_count, 1)
        lib_mock.sp_toplistbrowse_album.assert_called_with(sp_toplistbrowse, 0)
        album_lib_mock.sp_album_add_ref.assert_called_with(sp_album)
Ejemplo n.º 29
0
    def test_artists(self, artist_lib_mock, lib_mock):
        lib_mock.sp_toplistbrowse_error.return_value = spotify.ErrorType.OK
        sp_artist = spotify.ffi.cast('sp_artist *', 43)
        lib_mock.sp_toplistbrowse_num_artists.return_value = 1
        lib_mock.sp_toplistbrowse_artist.return_value = sp_artist
        sp_toplistbrowse = spotify.ffi.cast('sp_toplistbrowse *', 42)
        toplist = spotify.Toplist(self.session,
                                  sp_toplistbrowse=sp_toplistbrowse)

        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 1)
        result = toplist.artists
        self.assertEqual(lib_mock.sp_toplistbrowse_add_ref.call_count, 2)

        self.assertEqual(len(result), 1)
        lib_mock.sp_toplistbrowse_num_artists.assert_called_with(
            sp_toplistbrowse)

        item = result[0]
        self.assertIsInstance(item, spotify.Artist)
        self.assertEqual(item._sp_artist, sp_artist)
        self.assertEqual(lib_mock.sp_toplistbrowse_artist.call_count, 1)
        lib_mock.sp_toplistbrowse_artist.assert_called_with(
            sp_toplistbrowse, 0)
        artist_lib_mock.sp_artist_add_ref.assert_called_with(sp_artist)
Ejemplo n.º 30
0
 def test_create_without_type_or_region_or_sp_toplistbrowse_fails(
         self, lib_mock):
     with self.assertRaises(AssertionError):
         spotify.Toplist(self.session)