Пример #1
0
    def test_eq(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        link1 = spotify.Link(self.session, sp_link=sp_link)
        link2 = spotify.Link(self.session, sp_link=sp_link)

        self.assertTrue(link1 == link2)
        self.assertFalse(link1 == 'foo')
Пример #2
0
    def __init__(self, session, uri=None, sp_playlist=None, add_ref=True):
        super(Playlist, self).__init__()

        assert uri or sp_playlist, 'uri or sp_playlist is required'

        self._session = session

        if uri is not None:
            playlist = spotify.Link(self._session, uri).as_playlist()
            if playlist is None:
                raise spotify.Error(
                    'Failed to get playlist from Spotify URI: %r' % uri)
            sp_playlist = playlist._sp_playlist
            session._cache[sp_playlist] = self
            add_ref = True

        if add_ref:
            lib.sp_playlist_add_ref(sp_playlist)
        self._sp_playlist = ffi.gc(sp_playlist, lib.sp_playlist_release)

        self._sp_playlist_callbacks = None

        # Make sure we remove callbacks in __del__() using the same lib as we
        # added callbacks with.
        self._lib = lib
Пример #3
0
 def link(self):
     """A :class:`Link` to the image."""
     return spotify.Link(
         self._session,
         sp_link=lib.sp_link_create_from_image(self._sp_image),
         add_ref=False,
     )
Пример #4
0
 def link(self):
     """A :class:`Link` to the search."""
     return spotify.Link(
         self._session,
         sp_link=lib.sp_link_create_from_search(self._sp_search),
         add_ref=False,
     )
Пример #5
0
    def __init__(self,
                 session,
                 uri=None,
                 sp_image=None,
                 add_ref=True,
                 callback=None):

        assert uri or sp_image, 'uri or sp_image is required'

        self._session = session

        if uri is not None:
            image = spotify.Link(self._session, uri=uri).as_image()
            if image is None:
                raise ValueError('Failed to get image from Spotify URI: %r' %
                                 uri)
            sp_image = image._sp_image
            add_ref = True

        if add_ref:
            lib.sp_image_add_ref(sp_image)
        self._sp_image = ffi.gc(sp_image, lib.sp_image_release)

        self.loaded_event = threading.Event()

        handle = ffi.new_handle((self._session, self, callback))
        self._session._callback_handles.add(handle)
        spotify.Error.maybe_raise(
            lib.sp_image_add_load_callback(self._sp_image,
                                           _image_load_callback, handle))
Пример #6
0
 def link(self):
     """A :class:`Link` to the user."""
     return spotify.Link(
         self._session,
         sp_link=lib.sp_link_create_from_user(self._sp_user),
         add_ref=False,
     )
Пример #7
0
 def link_with_offset(self, offset):
     """A :class:`Link` to the track with an ``offset`` in milliseconds into
     the track."""
     return spotify.Link(self._session,
                         sp_link=lib.sp_link_create_from_track(
                             self._sp_track, offset),
                         add_ref=False)
Пример #8
0
    def test_as_user_if_not_a_user(self, user_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_as_user.return_value = spotify.ffi.NULL

        link = spotify.Link(self.session, 'spotify:user:foo')
        self.assertIsNone(link.as_user())

        lib_mock.sp_link_as_user.assert_called_once_with(sp_link)
Пример #9
0
    def test_as_image_if_not_a_image(self, image_lib_mock, lib_mock):
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_type.return_value = spotify.LinkType.ARTIST

        link = spotify.Link('spotify:image:foo')
        self.assertIsNone(link.as_image())

        self.assertEqual(lib_mock.sp_image_create_from_link.call_count, 0)
Пример #10
0
    def test_as_track_if_not_a_track(self, track_lib_mock, lib_mock):
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_as_track.return_value = spotify.ffi.NULL

        link = spotify.Link('spotify:track:foo')
        self.assertIsNone(link.as_track())

        lib_mock.sp_link_as_track.assert_called_once_with(sp_link)
Пример #11
0
    def test_type(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_type.return_value = 1
        link = spotify.Link(self.session, 'spotify:track:foo')

        self.assertIs(link.type, spotify.LinkType.TRACK)

        lib_mock.sp_link_type.assert_called_once_with(sp_link)
Пример #12
0
    def test_releases_sp_link_when_link_dies(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link

        link = spotify.Link(self.session, 'spotify:track:foo')
        link = None  # noqa
        tests.gc_collect()

        lib_mock.sp_link_release.assert_called_with(sp_link)
Пример #13
0
    def test_as_artist_if_not_an_artist(self, artist_lib_mock, lib_mock):
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_as_artist.return_value = spotify.ffi.NULL

        link = spotify.Link('spotify:artist:foo')
        self.assertIsNone(link.as_artist())

        lib_mock.sp_link_as_artist.assert_called_once_with(sp_link)
Пример #14
0
    def test_str(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        string = 'foo'

        lib_mock.sp_link_as_string.side_effect = tests.buffer_writer(string)
        link = spotify.Link(self.session, string)

        self.assertEqual(str(link), link.uri)
Пример #15
0
    def test_as_playlist_if_not_a_playlist(self, playlist_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_type.return_value = spotify.LinkType.ARTIST

        link = spotify.Link(self.session, 'spotify:playlist:foo')
        self.assertIsNone(link.as_playlist())

        self.assertEqual(lib_mock.sp_playlist_create.call_count, 0)
Пример #16
0
    def test_as_artist(self, artist_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        sp_artist = spotify.ffi.cast('sp_artist *', 43)
        lib_mock.sp_link_as_artist.return_value = sp_artist

        link = spotify.Link(self.session, 'spotify:artist:foo')
        self.assertEqual(link.as_artist()._sp_artist, sp_artist)

        lib_mock.sp_link_as_artist.assert_called_once_with(sp_link)
Пример #17
0
    def test_as_track(self, track_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        sp_track = spotify.ffi.cast('sp_track *', 43)
        lib_mock.sp_link_as_track.return_value = sp_track

        link = spotify.Link(self.session, 'spotify:track:foo')
        self.assertEqual(link.as_track()._sp_track, sp_track)

        lib_mock.sp_link_as_track.assert_called_once_with(sp_link)
Пример #18
0
    def test_url_expands_uri_to_http_url(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        string = 'spotify:track:foo'
        lib_mock.sp_link_as_string.side_effect = tests.buffer_writer(string)
        link = spotify.Link(self.session, string)

        result = link.url

        self.assertEqual(result, 'https://open.spotify.com/track/foo')
Пример #19
0
    def test_as_album(self, album_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        sp_album = spotify.ffi.cast('sp_album *', 43)
        lib_mock.sp_link_as_album.return_value = sp_album

        link = spotify.Link(self.session, 'spotify:album:foo')
        self.assertEqual(link.as_album()._sp_album, sp_album)

        lib_mock.sp_link_as_album.assert_called_once_with(sp_link)
Пример #20
0
    def test_as_user(self, user_lib_mock, lib_mock):
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_string.return_value = sp_link
        sp_user = spotify.ffi.new('int *')
        lib_mock.sp_link_as_user.return_value = sp_user

        link = spotify.Link('spotify:user:foo')
        self.assertEqual(link.as_user()._sp_user, sp_user)

        lib_mock.sp_link_as_user.assert_called_once_with(sp_link)
Пример #21
0
    def test_as_image_if_not_a_image(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_type.return_value = spotify.LinkType.ARTIST

        link = spotify.Link(self.session, 'spotify:image:foo')
        result = link.as_image()

        self.assertIsNone(result)
        self.assertEqual(lib_mock.sp_image_create_from_link.call_count, 0)
Пример #22
0
    def test_as_user(self, user_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        sp_user = spotify.ffi.cast('sp_user *', 43)
        lib_mock.sp_link_as_user.return_value = sp_user

        link = spotify.Link(self.session, 'spotify:user:foo')
        self.assertEqual(link.as_user()._sp_user, sp_user)

        lib_mock.sp_link_as_user.assert_called_once_with(sp_link)
Пример #23
0
    def test_repr(self, lib_mock):
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_string.return_value = sp_link
        string = 'foo'

        lib_mock.sp_link_as_string.side_effect = tests.buffer_writer(string)
        link = spotify.Link(string)

        result = repr(link)

        self.assertEqual(result, 'Link(%r)' % string)
Пример #24
0
    def test_create_from_open_spotify_com_url(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link

        spotify.Link(self.session, 'http://open.spotify.com/track/bar ')

        lib_mock.sp_link_create_from_string.assert_called_once_with(mock.ANY)
        self.assertEqual(
            spotify.ffi.string(
                lib_mock.sp_link_create_from_string.call_args[0][0]),
            b'spotify:track:bar')
Пример #25
0
    def test_as_track_with_offset_if_not_a_track(self, track_lib_mock,
                                                 lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_as_track_and_offset.return_value = spotify.ffi.NULL

        link = spotify.Link(self.session, 'spotify:track:foo')
        offset = link.as_track_offset()

        self.assertIsNone(offset)
        lib_mock.sp_link_as_track_and_offset.assert_called_once_with(
            sp_link, mock.ANY)
Пример #26
0
    def test_create_from_uri(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link

        spotify.Link(self.session, 'spotify:track:foo')

        lib_mock.sp_link_create_from_string.assert_called_once_with(mock.ANY)
        self.assertEqual(
            spotify.ffi.string(
                lib_mock.sp_link_create_from_string.call_args[0][0]),
            b'spotify:track:foo')
        self.assertEqual(lib_mock.sp_link_add_ref.call_count, 0)
Пример #27
0
    def test_as_playlist_if_starred(self, playlist_lib_mock, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        lib_mock.sp_link_type.return_value = spotify.LinkType.STARRED
        sp_playlist = spotify.ffi.cast('sp_playlist *', 43)
        lib_mock.sp_playlist_create.return_value = sp_playlist

        link = spotify.Link(self.session, 'spotify:playlist:foo')
        self.assertEqual(link.as_playlist()._sp_playlist, sp_playlist)

        lib_mock.sp_playlist_create.assert_called_once_with(
            self.session._sp_session, sp_link)
Пример #28
0
 def link(self):
     """A :class:`Link` to the playlist."""
     if not self.is_loaded:
         raise spotify.Error('The playlist must be loaded to create a link')
     sp_link = lib.sp_link_create_from_playlist(self._sp_playlist)
     if sp_link == ffi.NULL:
         if not self.is_in_ram:
             raise spotify.Error(
                 'The playlist must have been in RAM to create a link')
         # XXX Figure out why we can still get NULL here even if
         # the playlist is both loaded and in RAM.
         raise spotify.Error('Failed to get link from Spotify playlist')
     return spotify.Link(self._session, sp_link=sp_link, add_ref=False)
Пример #29
0
    def __init__(self, uri=None, sp_artist=None, add_ref=True):
        assert uri or sp_artist, 'uri or sp_artist is required'

        if uri is not None:
            artist = spotify.Link(uri).as_artist()
            if artist is None:
                raise ValueError('Failed to get artist from Spotify URI: %r' %
                                 uri)
            sp_artist = artist._sp_artist

        if add_ref:
            lib.sp_artist_add_ref(sp_artist)
        self._sp_artist = ffi.gc(sp_artist, lib.sp_artist_release)
Пример #30
0
    def test_uri_grows_buffer_to_fit_link(self, lib_mock):
        sp_link = spotify.ffi.cast('sp_link *', 42)
        lib_mock.sp_link_create_from_string.return_value = sp_link
        string = 'foo' * 100

        lib_mock.sp_link_as_string.side_effect = tests.buffer_writer(string)
        link = spotify.Link(self.session, string)

        result = link.uri

        lib_mock.sp_link_as_string.assert_called_with(sp_link, mock.ANY,
                                                      mock.ANY)
        self.assertEqual(result, string)