예제 #1
0
    def test_hash(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser1 = spotify.AlbumBrowser(self.session,
                                        sp_albumbrowse=sp_albumbrowse)
        browser2 = spotify.AlbumBrowser(self.session,
                                        sp_albumbrowse=sp_albumbrowse)

        self.assertEqual(hash(browser1), hash(browser2))
예제 #2
0
    def test_ne(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser1 = spotify.AlbumBrowser(self.session,
                                        sp_albumbrowse=sp_albumbrowse)
        browser2 = spotify.AlbumBrowser(self.session,
                                        sp_albumbrowse=sp_albumbrowse)

        self.assertFalse(browser1 != browser2)
예제 #3
0
    def test_load(self, load_mock, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        browser.load(10)

        load_mock.assert_called_with(browser, timeout=10)
예제 #4
0
    def test_adds_ref_to_sp_albumbrowse_when_created(self, lib_mock):
        session = tests.create_session_mock()
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)

        spotify.AlbumBrowser(session, sp_albumbrowse=sp_albumbrowse)

        lib_mock.sp_albumbrowse_add_ref.assert_called_with(sp_albumbrowse)
예제 #5
0
    def test_browser_is_gone_before_callback_is_called(self, lib_mock):
        sp_album = spotify.ffi.cast('sp_album *', 43)
        album = spotify.Album(self.session, sp_album=sp_album)
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        lib_mock.sp_albumbrowse_create.return_value = sp_albumbrowse
        callback = mock.Mock()

        result = spotify.AlbumBrowser(self.session,
                                      album=album,
                                      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.
        albumbrowse_complete_cb = (
            lib_mock.sp_albumbrowse_create.call_args[0][2])
        userdata = lib_mock.sp_albumbrowse_create.call_args[0][3]
        albumbrowse_complete_cb(sp_albumbrowse, userdata)

        loaded_event.wait(3)
        self.assertEqual(callback.call_count, 1)
        self.assertEqual(callback.call_args[0][0]._sp_albumbrowse,
                         sp_albumbrowse)
예제 #6
0
    def test_browser_is_gone_before_callback_is_called(self, lib_mock):
        tests.create_session()
        sp_album = spotify.ffi.new('int *')
        album = spotify.Album(sp_album=sp_album)
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *',
                                          spotify.ffi.new('int *'))
        lib_mock.sp_albumbrowse_create.return_value = sp_albumbrowse
        callback = mock.Mock()

        result = spotify.AlbumBrowser(album=album, callback=callback)
        complete_event = result.complete_event
        result = None  # noqa
        tests.gc_collect()

        # FIXME The mock keeps the handle/userdata alive, thus the album is
        # kept alive, and this test doesn't test what it is intended to test.
        albumbrowse_complete_cb = (
            lib_mock.sp_albumbrowse_create.call_args[0][2])
        userdata = lib_mock.sp_albumbrowse_create.call_args[0][3]
        albumbrowse_complete_cb(sp_albumbrowse, userdata)

        complete_event.wait(3)
        self.assertEqual(callback.call_count, 1)
        self.assertEqual(callback.call_args[0][0]._sp_albumbrowse,
                         sp_albumbrowse)
예제 #7
0
    def test_repr_if_unloaded(self, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        lib_mock.sp_albumbrowse_is_loaded.return_value = 0

        result = repr(browser)

        self.assertEqual(result, 'AlbumBrowser(<not loaded>)')
예제 #8
0
    def test_load(self, load_mock, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)

        browser.load(10)

        load_mock.assert_called_with(self.session, browser, timeout=10)
예제 #9
0
    def test_releases_sp_albumbrowse_when_album_dies(self, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')

        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        browser = None  # noqa
        tests.gc_collect()

        lib_mock.sp_albumbrowse_release.assert_called_with(sp_albumbrowse)
예제 #10
0
    def test_artist_when_not_loaded(self, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        lib_mock.sp_albumbrowse_artist.return_value = spotify.ffi.NULL

        result = browser.artist

        lib_mock.sp_albumbrowse_artist.assert_called_with(sp_albumbrowse)
        self.assertIsNone(result)
예제 #11
0
    def test_tracks_if_unloaded(self, lib_mock):
        lib_mock.sp_albumbrowse_is_loaded.return_value = 0
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.tracks

        lib_mock.sp_albumbrowse_is_loaded.assert_called_with(sp_albumbrowse)
        self.assertEqual(len(result), 0)
예제 #12
0
    def test_repr_if_unloaded(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        lib_mock.sp_albumbrowse_is_loaded.return_value = 0

        result = repr(browser)

        self.assertEqual(result, 'AlbumBrowser(<not loaded>)')
예제 #13
0
    def test_releases_sp_albumbrowse_when_album_dies(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)

        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        browser = None  # noqa
        tests.gc_collect()

        lib_mock.sp_albumbrowse_release.assert_called_with(sp_albumbrowse)
예제 #14
0
파일: album.py 프로젝트: thomasvs/pyspotify
    def browse(self, callback=None):
        """Get an :class:`AlbumBrowser` for the album.

        If ``callback`` isn't :class:`None`, it is expected to be a callable
        that accepts a single argument, an :class:`AlbumBrowser` instance, when
        the browser is done loading.

        Can be created without the album being loaded.
        """
        return spotify.AlbumBrowser(album=self, callback=callback)
예제 #15
0
    def test_copyrights_if_unloaded(self, lib_mock):
        lib_mock.sp_albumbrowse_is_loaded.return_value = 0
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)

        result = browser.copyrights

        lib_mock.sp_albumbrowse_is_loaded.assert_called_with(sp_albumbrowse)
        self.assertEqual(len(result), 0)
예제 #16
0
    def test_album_when_not_loaded(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        lib_mock.sp_albumbrowse_album.return_value = spotify.ffi.NULL

        result = browser.album

        lib_mock.sp_albumbrowse_album.assert_called_with(sp_albumbrowse)
        self.assertIsNone(result)
예제 #17
0
    def test_review(self, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        review = spotify.ffi.new('char[]', b'A nice album')
        lib_mock.sp_albumbrowse_review.return_value = review

        result = browser.review

        self.assertIsInstance(result, utils.text_type)
        self.assertEqual(result, 'A nice album')
예제 #18
0
    def test_album(self, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        sp_album = spotify.ffi.new('int *')
        lib_mock.sp_albumbrowse_album.return_value = sp_album

        result = browser.album

        self.assertIsInstance(result, spotify.Album)
        self.assertEqual(result._sp_album, sp_album)
예제 #19
0
    def test_tracks_if_no_tracks(self, lib_mock):
        lib_mock.sp_albumbrowse_num_tracks.return_value = 0
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.tracks

        self.assertEqual(len(result), 0)
        lib_mock.sp_albumbrowse_num_tracks.assert_called_with(sp_albumbrowse)
        self.assertEqual(lib_mock.sp_albumbrowse_track.call_count, 0)
예제 #20
0
    def test_artist(self, artist_lib_mock, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        sp_artist = spotify.ffi.new('int *')
        lib_mock.sp_albumbrowse_artist.return_value = sp_artist

        result = browser.artist

        self.assertIsInstance(result, spotify.Artist)
        self.assertEqual(result._sp_artist, sp_artist)
예제 #21
0
    def test_backend_request_duration(self, lib_mock):
        lib_mock.sp_albumbrowse_backend_request_duration.return_value = 137
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.backend_request_duration

        lib_mock.sp_albumbrowse_backend_request_duration.assert_called_with(
            sp_albumbrowse)
        self.assertEqual(result, 137)
예제 #22
0
    def test_error(self, lib_mock):
        lib_mock.sp_albumbrowse_error.return_value = int(
            spotify.ErrorType.OTHER_PERMANENT)
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.error

        lib_mock.sp_albumbrowse_error.assert_called_once_with(sp_albumbrowse)
        self.assertIs(result, spotify.ErrorType.OTHER_PERMANENT)
예제 #23
0
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_albumbrowse_is_loaded.return_value = 1
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.is_loaded

        lib_mock.sp_albumbrowse_is_loaded.assert_called_once_with(
            sp_albumbrowse)
        self.assertTrue(result)
예제 #24
0
    def test_review(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        review = spotify.ffi.new('char[]', b'A nice album')
        lib_mock.sp_albumbrowse_review.return_value = review

        result = browser.review

        self.assertIsInstance(result, compat.text_type)
        self.assertEqual(result, 'A nice album')
예제 #25
0
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_albumbrowse_is_loaded.return_value = 1
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)

        result = browser.is_loaded

        lib_mock.sp_albumbrowse_is_loaded.assert_called_once_with(
            sp_albumbrowse)
        self.assertTrue(result)
예제 #26
0
    def test_album(self, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        sp_album = spotify.ffi.cast('sp_album *', 43)
        lib_mock.sp_albumbrowse_album.return_value = sp_album

        result = browser.album

        self.assertIsInstance(result, spotify.Album)
        self.assertEqual(result._sp_album, sp_album)
예제 #27
0
    def test_backend_request_duration_when_not_loaded(self, lib_mock):
        lib_mock.sp_albumbrowse_is_loaded.return_value = 0
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)

        result = browser.backend_request_duration

        lib_mock.sp_albumbrowse_is_loaded.assert_called_with(sp_albumbrowse)
        self.assertEqual(
            lib_mock.sp_albumbrowse_backend_request_duration.call_count, 0)
        self.assertIsNone(result)
예제 #28
0
    def test_artist(self, artist_lib_mock, lib_mock):
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)
        sp_artist = spotify.ffi.cast('sp_artist *', 43)
        lib_mock.sp_albumbrowse_artist.return_value = sp_artist

        result = browser.artist

        self.assertIsInstance(result, spotify.Artist)
        self.assertEqual(result._sp_artist, sp_artist)
예제 #29
0
    def test_copyrights_if_no_copyrights(self, lib_mock):
        lib_mock.sp_albumbrowse_num_copyrights.return_value = 0
        sp_albumbrowse = spotify.ffi.cast('sp_albumbrowse *', 42)
        browser = spotify.AlbumBrowser(self.session,
                                       sp_albumbrowse=sp_albumbrowse)

        result = browser.copyrights

        self.assertEqual(len(result), 0)
        lib_mock.sp_albumbrowse_num_copyrights.assert_called_with(
            sp_albumbrowse)
        self.assertEqual(lib_mock.sp_albumbrowse_copyright.call_count, 0)
예제 #30
0
    def test_repr(self, link_mock, lib_mock):
        sp_albumbrowse = spotify.ffi.new('int *')
        browser = spotify.AlbumBrowser(sp_albumbrowse=sp_albumbrowse)
        lib_mock.sp_albumbrowse_is_loaded.return_value = 1
        sp_album = spotify.ffi.new('int *')
        lib_mock.sp_albumbrowse_album.return_value = sp_album
        link_instance_mock = link_mock.return_value
        link_instance_mock.uri = 'foo'

        result = repr(browser)

        self.assertEqual(result, 'AlbumBrowser(%r)' % 'foo')