def test_eq(self, lib_mock): sp_inbox = spotify.ffi.cast('sp_inbox *', 42) inbox1 = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) inbox2 = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) self.assertTrue(inbox1 == inbox2) self.assertFalse(inbox1 == 'foo')
def test_inbox_post_where_result_is_gone_before_callback_is_called( self, track_lib_mock, lib_mock): sp_track1 = spotify.ffi.cast('sp_track *', 43) track1 = spotify.Track(self.session, sp_track=sp_track1) sp_track2 = spotify.ffi.cast('sp_track *', 44) track2 = spotify.Track(self.session, sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', 42) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox callback = mock.Mock() result = spotify.InboxPostResult(self.session, 'alice', [track1, track2], 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. inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) loaded_event.wait(3) self.assertEqual(callback.call_count, 1) self.assertEqual(callback.call_args[0][0]._sp_inbox, sp_inbox)
def test_inbox_post_where_result_is_gone_before_callback_is_called( self, track_lib_mock, lib_mock): tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox callback = mock.Mock() result = spotify.InboxPostResult('alice', [track1, track2], callback=callback) complete_event = result.complete_event result = None # noqa tests.gc_collect() # FIXME The mock keeps the handle/userdata alive, thus the search # result is kept alive, and this test doesn't test what it is intended # to test. inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) complete_event.wait(3) self.assertEqual(callback.call_count, 1) self.assertEqual(callback.call_args[0][0]._sp_inbox, sp_inbox)
def test_inbox_post_tracks(self, track_lib_mock, lib_mock): session = tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox result = spotify.InboxPostResult('alice', [track1, track2], '♥') lib_mock.sp_inbox_post_tracks.assert_called_with( session._sp_session, mock.ANY, mock.ANY, 2, mock.ANY, mock.ANY, mock.ANY) self.assertEqual( spotify.ffi.string(lib_mock.sp_inbox_post_tracks.call_args[0][1]), b'alice') self.assertIn(sp_track1, lib_mock.sp_inbox_post_tracks.call_args[0][2]) self.assertIn(sp_track2, lib_mock.sp_inbox_post_tracks.call_args[0][2]) self.assertEqual( spotify.ffi.string(lib_mock.sp_inbox_post_tracks.call_args[0][4]), b'\xe2\x99\xa5') self.assertIsInstance(result, spotify.InboxPostResult) self.assertEqual(result._sp_inbox, sp_inbox) self.assertFalse(result.complete_event.is_set()) inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) self.assertTrue(result.complete_event.wait(3))
def test_releases_sp_inbox_when_result_dies(self, lib_mock): sp_inbox = spotify.ffi.new('int *') inbox_post_result = spotify.InboxPostResult(sp_inbox=sp_inbox) inbox_post_result = None # noqa tests.gc_collect() lib_mock.sp_inbox_release.assert_called_with(sp_inbox)
def test_releases_sp_inbox_when_result_dies(self, lib_mock): sp_inbox = spotify.ffi.cast('sp_inbox *', 42) inbox_post_result = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) inbox_post_result = None # noqa tests.gc_collect() lib_mock.sp_inbox_release.assert_called_with(sp_inbox)
def test_fail_to_init_raises_error(self, track_lib_mock, lib_mock): sp_track1 = spotify.ffi.cast('sp_track *', 43) track1 = spotify.Track(self.session, sp_track=sp_track1) sp_track2 = spotify.ffi.cast('sp_track *', 44) track2 = spotify.Track(self.session, sp_track=sp_track2) lib_mock.sp_inbox_post_tracks.return_value = spotify.ffi.NULL with self.assertRaises(spotify.Error): spotify.InboxPostResult(self.session, 'alice', [track1, track2], 'Enjoy!')
def test_error(self, lib_mock): lib_mock.sp_inbox_error.return_value = int( spotify.ErrorType.INBOX_IS_FULL) sp_inbox = spotify.ffi.new('int *') inbox_post_result = spotify.InboxPostResult(sp_inbox=sp_inbox) result = inbox_post_result.error lib_mock.sp_inbox_error.assert_called_once_with(sp_inbox) self.assertIs(result, spotify.ErrorType.INBOX_IS_FULL)
def test_fail_to_init_raises_error(self, track_lib_mock, lib_mock): tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) lib_mock.sp_inbox_post_tracks.return_value = spotify.ffi.NULL with self.assertRaises(spotify.Error): spotify.InboxPostResult('alice', [track1, track2], 'Enjoy!')
def test_repr(self, lib_mock): sp_inbox = spotify.ffi.new('int *') inbox_post_result = spotify.InboxPostResult(sp_inbox=sp_inbox) self.assertEqual(repr(inbox_post_result), '<InboxPostResult: pending>') inbox_post_result.complete_event.set() lib_mock.sp_inbox_error.return_value = int( spotify.ErrorType.INBOX_IS_FULL) self.assertEqual(repr(inbox_post_result), '<InboxPostResult: INBOX_IS_FULL>')
def test_inbox_post_with_single_track(self, track_lib_mock, lib_mock): session = tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox result = spotify.InboxPostResult('alice', track1, 'Enjoy!') lib_mock.sp_inbox_post_tracks.assert_called_with( session._sp_session, mock.ANY, mock.ANY, 1, mock.ANY, mock.ANY, mock.ANY) self.assertIn(sp_track1, lib_mock.sp_inbox_post_tracks.call_args[0][2]) self.assertIsInstance(result, spotify.InboxPostResult) self.assertEqual(result._sp_inbox, sp_inbox)
def inbox_post_tracks( self, canonical_username, tracks, message, callback=None): """Post a ``message`` and one or more ``tracks`` to the inbox of the user with the given ``canonical_username``. ``tracks`` can be a single :class:`~spotify.Track` or a list of :class:`~spotify.Track` objects. Returns an :class:`InboxPostResult` that can be used to check if the request completed successfully. If callback isn't :class:`None`, it is called with an :class:`InboxPostResult` instance when the request has completed. """ return spotify.InboxPostResult( self, canonical_username, tracks, message, callback)
def test_repr(self, lib_mock): sp_inbox = spotify.ffi.cast('sp_inbox *', 42) inbox_post_result = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) self.assertEqual(repr(inbox_post_result), 'InboxPostResult(<pending>)') inbox_post_result.loaded_event.set() lib_mock.sp_inbox_error.return_value = int( spotify.ErrorType.INBOX_IS_FULL) self.assertEqual(repr(inbox_post_result), 'InboxPostResult(INBOX_IS_FULL)') lib_mock.sp_inbox_error.return_value = int(spotify.ErrorType.OK) self.assertEqual(repr(inbox_post_result), 'InboxPostResult(OK)')
def test_inbox_post_with_callback(self, track_lib_mock, lib_mock): tests.create_session() sp_track1 = spotify.ffi.new('int *') track1 = spotify.Track(sp_track=sp_track1) sp_track2 = spotify.ffi.new('int *') track2 = spotify.Track(sp_track=sp_track2) sp_inbox = spotify.ffi.cast('sp_inbox *', spotify.ffi.new('int *')) lib_mock.sp_inbox_post_tracks.return_value = sp_inbox callback = mock.Mock() result = spotify.InboxPostResult('alice', [track1, track2], callback=callback) inboxpost_complete_cb = lib_mock.sp_inbox_post_tracks.call_args[0][5] userdata = lib_mock.sp_inbox_post_tracks.call_args[0][6] inboxpost_complete_cb(sp_inbox, userdata) result.complete_event.wait(3) callback.assert_called_with(result)
def test_hash(self, lib_mock): sp_inbox = spotify.ffi.cast('sp_inbox *', 42) inbox1 = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) inbox2 = spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) self.assertEqual(hash(inbox1), hash(inbox2))
def test_adds_ref_to_sp_inbox_when_created(self, lib_mock): sp_inbox = spotify.ffi.cast('sp_inbox *', 42) spotify.InboxPostResult(self.session, sp_inbox=sp_inbox) lib_mock.sp_inbox_add_ref.assert_called_with(sp_inbox)
def test_adds_ref_to_sp_inbox_when_created(self, lib_mock): sp_inbox = spotify.ffi.new('int *') spotify.InboxPostResult(sp_inbox=sp_inbox) lib_mock.sp_inbox_add_ref.assert_called_with(sp_inbox)
def test_create_without_user_and_tracks_or_sp_inbox_fails(self, lib_mock): with self.assertRaises(AssertionError): spotify.InboxPostResult()