コード例 #1
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_ne(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image1 = spotify.Image(self.session, sp_image=sp_image)
        image2 = spotify.Image(self.session, sp_image=sp_image)

        self.assertFalse(image1 != image2)
コード例 #2
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_create_from_uri(self, link_mock, lib_mock):
        sp_image = spotify.ffi.new('int *')
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_image.return_value = spotify.Image(
            sp_image=sp_image)
        lib_mock.sp_image_create_from_link.return_value = sp_image
        uri = 'spotify:image:foo'

        result = spotify.Image(uri)

        link_mock.assert_called_with(uri)
        link_instance_mock.as_image.assert_called_with()
        lib_mock.sp_image_add_ref.assert_called_with(sp_image)
        self.assertEqual(result._sp_image, sp_image)
コード例 #3
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_create_with_callback_and_throw_away_image_and_call_load_callback(
            self, lib_mock):

        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        lib_mock.sp_image_remove_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        lib_mock.sp_image_create.return_value = sp_image
        callback = mock.Mock()

        # Add callback
        image = spotify.Image(self.session,
                              sp_image=sp_image,
                              callback=callback)
        loaded_event = image.loaded_event

        # Throw away reference to 'image'
        image = 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.

        # Call callback
        image_load_cb = lib_mock.sp_image_add_load_callback.call_args[0][1]
        callback_handle = lib_mock.sp_image_add_load_callback.call_args[0][2]
        image_load_cb(sp_image, callback_handle)

        loaded_event.wait(3)
        self.assertEqual(callback.call_count, 1)
        self.assertEqual(callback.call_args[0][0]._sp_image, sp_image)
コード例 #4
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_load(self, load_mock, lib_mock):
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        image.load(10)

        load_mock.assert_called_with(image, timeout=10)
コード例 #5
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_create_from_uri(self, link_mock, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_image.return_value = spotify.Image(
            self.session, sp_image=sp_image)
        lib_mock.sp_image_create_from_link.return_value = sp_image
        uri = 'spotify:image:foo'

        result = spotify.Image(self.session, uri=uri)

        link_mock.assert_called_with(self.session, uri=uri)
        link_instance_mock.as_image.assert_called_with()
        lib_mock.sp_image_add_ref.assert_called_with(sp_image)
        self.assertEqual(result._sp_image, sp_image)
コード例 #6
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_create_from_uri_fail_raises_error(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.as_image.return_value = None
        uri = 'spotify:image:foo'

        with self.assertRaises(ValueError):
            spotify.Image(uri)
コード例 #7
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_create_with_callback(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        lib_mock.sp_image_remove_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        lib_mock.sp_image_create.return_value = sp_image
        callback = mock.Mock()

        # Add callback
        image = spotify.Image(self.session,
                              sp_image=sp_image,
                              callback=callback)

        lib_mock.sp_image_add_load_callback.assert_called_with(
            sp_image, mock.ANY, mock.ANY)
        image_load_cb = lib_mock.sp_image_add_load_callback.call_args[0][1]
        callback_handle = lib_mock.sp_image_add_load_callback.call_args[0][2]

        # Call calls callback, sets event, and removes callback registration
        self.assertEqual(callback.call_count, 0)
        self.assertEqual(lib_mock.sp_image_remove_load_callback.call_count, 0)
        self.assertFalse(image.loaded_event.is_set())
        image_load_cb(sp_image, callback_handle)
        callback.assert_called_once_with(image)
        lib_mock.sp_image_remove_load_callback.assert_called_with(
            sp_image, image_load_cb, callback_handle)
        self.assertTrue(image.loaded_event.is_set())
コード例 #8
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_loaded_event_is_unset_by_default(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)

        self.assertFalse(image.loaded_event.is_set())
コード例 #9
0
 def get_image(sp_artistbrowse, key):
     image_id = lib.sp_artistbrowse_portrait(sp_artistbrowse, key)
     sp_image = lib.sp_image_create(image_id)
     return spotify.Image(self._session,
                          sp_image=sp_image,
                          add_ref=False,
                          callback=callback)
コード例 #10
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_add_load_callback_fails_if_error(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.BAD_API_VERSION)
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        with self.assertRaises(spotify.Error):
            image.add_load_callback(None)
コード例 #11
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_adds_ref_to_sp_image_when_created(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)

        spotify.Image(self.session, sp_image=sp_image)

        lib_mock.sp_image_add_ref.assert_called_with(sp_image)
コード例 #12
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_data_uri_is_none_if_unloaded(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 0
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = image.data_uri

        self.assertIsNone(result)
コード例 #13
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_releases_sp_image_when_image_dies(self, lib_mock):
        sp_image = spotify.ffi.new('int *')

        image = spotify.Image(sp_image=sp_image)
        image = None  # noqa
        tests.gc_collect()

        lib_mock.sp_image_release.assert_called_with(sp_image)
コード例 #14
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_load(self, load_mock, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)

        image.load(10)

        load_mock.assert_called_with(self.session, image, timeout=10)
コード例 #15
0
 def image_changed(sp_playlist, image_id, userdata):
     logger.debug('Playlist image changed')
     playlist = Playlist._cached(
         spotify._session_instance, sp_playlist, add_ref=True)
     sp_image = lib.sp_image_create(
         spotify._session_instance._sp_session, image_id)
     image = spotify.Image(
         spotify._session_instance, sp_image=sp_image, add_ref=False)
     playlist.emit(PlaylistEvent.IMAGE_CHANGED, playlist, image)
コード例 #16
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_repr(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.uri = 'foo'
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = repr(image)

        self.assertEqual(result, 'Image(%r)' % 'foo')
コード例 #17
0
 def as_image(self):
     """Make an :class:`Image` from the link."""
     if self.type is not LinkType.IMAGE:
         return None
     sp_image = lib.sp_image_create_from_link(
         spotify.session_instance._sp_session, self._sp_link)
     if sp_image == ffi.NULL:
         return None
     return spotify.Image(sp_image=sp_image, add_ref=False)
コード例 #18
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_data_uri_fails_if_unknown_image_format(self, lib_mock):
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)
        image.__dict__['format'] = mock.Mock(
            return_value=spotify.ImageFormat.UNKNOWN)
        image.__dict__['data'] = mock.Mock(return_value=b'01234\x006789')

        with self.assertRaises(ValueError):
            image.data_uri
コード例 #19
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 1
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = image.is_loaded

        lib_mock.sp_image_is_loaded.assert_called_once_with(sp_image)
        self.assertTrue(result)
コード例 #20
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_format_is_none_if_unloaded(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 0
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = image.format

        lib_mock.sp_image_is_loaded.assert_called_with(sp_image)
        self.assertIsNone(result)
コード例 #21
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_create_with_callback_fails_if_error_adding_callback(
            self, lib_mock):

        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.BAD_API_VERSION)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        callback = mock.Mock()

        with self.assertRaises(spotify.Error):
            spotify.Image(self.session, sp_image=sp_image, callback=callback)
コード例 #22
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_data_uri_is_none_if_unloaded(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 0
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)

        result = image.data_uri

        self.assertIsNone(result)
コード例 #23
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_format(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 1
        lib_mock.sp_image_format.return_value = int(spotify.ImageFormat.JPEG)
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = image.format

        lib_mock.sp_image_format.assert_called_with(sp_image)
        self.assertIs(result, spotify.ImageFormat.JPEG)
コード例 #24
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_releases_sp_image_when_image_dies(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)

        image = spotify.Image(self.session, sp_image=sp_image)
        image = None  # noqa
        tests.gc_collect()

        lib_mock.sp_image_release.assert_called_with(sp_image)
コード例 #25
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_error(self, lib_mock):
        lib_mock.sp_image_error.return_value = int(
            spotify.ErrorType.IS_LOADING)
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)

        result = image.error

        lib_mock.sp_image_error.assert_called_once_with(sp_image)
        self.assertIs(result, spotify.ErrorType.IS_LOADING)
コード例 #26
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_is_loaded(self, lib_mock):
        lib_mock.sp_image_is_loaded.return_value = 1
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)

        result = image.is_loaded

        lib_mock.sp_image_is_loaded.assert_called_once_with(sp_image)
        self.assertTrue(result)
コード例 #27
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_link_creates_link_to_image(self, link_mock, lib_mock):
        sp_image = spotify.ffi.new('int *')
        image = spotify.Image(sp_image=sp_image)
        sp_link = spotify.ffi.new('int *')
        lib_mock.sp_link_create_from_image.return_value = sp_link
        link_mock.return_value = mock.sentinel.link

        result = image.link

        link_mock.assert_called_once_with(sp_link=sp_link, add_ref=False)
        self.assertEqual(result, mock.sentinel.link)
コード例 #28
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_data_uri_fails_if_unknown_image_format(self, lib_mock):
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)
        image.__dict__['format'] = mock.Mock(
            return_value=spotify.ImageFormat.UNKNOWN)
        image.__dict__['data'] = mock.Mock(return_value=b'01234\x006789')

        with self.assertRaises(ValueError):
            image.data_uri
コード例 #29
0
ファイル: test_image.py プロジェクト: mdries14887/pyspotify
    def test_repr(self, link_mock, lib_mock):
        link_instance_mock = link_mock.return_value
        link_instance_mock.uri = 'foo'
        lib_mock.sp_image_add_load_callback.return_value = int(
            spotify.ErrorType.OK)
        sp_image = spotify.ffi.cast('sp_image *', 42)
        image = spotify.Image(self.session, sp_image=sp_image)

        result = repr(image)

        self.assertEqual(result, 'Image(%r)' % 'foo')
コード例 #30
0
ファイル: test_image.py プロジェクト: thomasvs/pyspotify
    def test_data_uri(self, lib_mock):
        lib_mock.sp_image_format.return_value = int(spotify.ImageFormat.JPEG)
        sp_image = spotify.ffi.new('int *')

        prop_mock = mock.PropertyMock()
        with mock.patch.object(spotify.Image, 'data', prop_mock):
            image = spotify.Image(sp_image=sp_image)
            prop_mock.return_value = b'01234\x006789'

            result = image.data_uri

        self.assertEqual(result, 'data:image/jpeg;base64,MDEyMzQANjc4OQ==')