本文整理汇总了Python中Tribler.Test.Core.base_test.MockObject类的典型用法代码示例。如果您正苦于以下问题:Python MockObject类的具体用法?Python MockObject怎么用?Python MockObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MockObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_restart_in_upload_mode
def test_restart_in_upload_mode(self):
tdef = self.create_tdef()
def mock_set_upload_mode(handle, mode):
handle.status().upload_mode = mode
fake_status = MockObject()
fake_status.share_mode = False
fake_status.upload_mode = False
impl = LibtorrentDownloadImpl(self.session, tdef)
impl.handle = MockObject()
impl.handle.status = lambda: fake_status
# impl.handle.upload_mode = False
impl.handle.set_priority = lambda _: None
impl.handle.set_sequential_download = lambda _: None
impl.handle.set_upload_mode = lambda mode, _handle=impl.handle: mock_set_upload_mode(_handle, mode)
impl.handle.get_upload_mode = lambda: impl.handle.status().upload_mode
impl.handle.is_valid = lambda: True
impl.handle.resume = lambda: None
# Create a dummy download config
impl.dlconfig = DownloadStartupConfig().dlconfig.copy()
impl.session.lm.on_download_wrapper_created = lambda _: True
impl.set_upload_mode(True)
impl.restart()
self.assertTrue(impl.get_upload_mode())
示例2: test_on_metadata_received_alert
def test_on_metadata_received_alert(self):
"""
Testing whether the right operations happen when we receive metadata
"""
test_deferred = Deferred()
def mocked_checkpoint():
test_deferred.callback(None)
mocked_file = MockObject()
mocked_file.path = 'test'
self.libtorrent_download_impl.handle.trackers = lambda: []
self.libtorrent_download_impl.handle.save_resume_data = lambda: None
self.libtorrent_download_impl.handle.rename_file = lambda *_: None
with open(os.path.join(TESTS_DATA_DIR, "bak_single.torrent"), mode='rb') as torrent_file:
encoded_metainfo = torrent_file.read()
decoded_metainfo = bdecode(encoded_metainfo)
get_info_from_handle(self.libtorrent_download_impl.handle).metadata = lambda: bencode(decoded_metainfo['info'])
get_info_from_handle(self.libtorrent_download_impl.handle).files = lambda: [mocked_file]
self.libtorrent_download_impl.checkpoint = mocked_checkpoint
self.libtorrent_download_impl.session = MockObject()
self.libtorrent_download_impl.session.lm = MockObject()
self.libtorrent_download_impl.session.lm.torrent_db = None
self.libtorrent_download_impl.handle.save_path = lambda: None
self.libtorrent_download_impl.handle.prioritize_files = lambda _: None
self.libtorrent_download_impl.get_save_path = lambda: ''
self.libtorrent_download_impl.get_share_mode = lambda: False
self.libtorrent_download_impl.on_metadata_received_alert(None)
return test_deferred
示例3: create_fake_block
def create_fake_block(self):
"""
Create a dummy block and return it
"""
block = MockObject()
block.hash = 'a'
return block
示例4: test_multifile_torrent
def test_multifile_torrent(self):
tdef = TorrentDef()
tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi"))
tdef.set_tracker("http://tribler.org/announce")
tdef.save()
impl = LibtorrentDownloadImpl(self.session, tdef)
# Override the add_torrent because it will be called
impl.ltmgr = MockObject()
impl.ltmgr.add_torrent = lambda _, _dummy2: succeed(fake_handler)
impl.set_selected_files = lambda: None
fake_handler = MockObject()
fake_handler.is_valid = lambda: True
fake_handler.status = lambda: fake_status
fake_handler.set_share_mode = lambda _: None
fake_handler.set_priority = lambda _: None
fake_handler.set_sequential_download = lambda _: None
fake_handler.resume = lambda: None
fake_handler.set_max_connections = lambda _: None
fake_status = MockObject()
fake_status.share_mode = False
# Create a dummy download config
impl.dlconfig = DownloadStartupConfig().dlconfig.copy()
# Create a dummy pstate
pstate = CallbackConfigParser()
pstate.add_section("state")
test_dict = dict()
test_dict["a"] = "b"
pstate.set("state", "engineresumedata", test_dict)
return impl.network_create_engine_wrapper(pstate)
示例5: test_selected_files_no_files
def test_selected_files_no_files(self):
"""
Test that no files are selected if torrent info is not available.
"""
def mocked_set_file_prios(_):
mocked_set_file_prios.called = True
mocked_set_file_prios.called = False
mocked_file = MockObject()
mocked_file.path = 'my/path'
mock_torrent_info = MockObject()
self.libtorrent_download_impl.handle.prioritize_files = mocked_set_file_prios
self.libtorrent_download_impl.handle.get_torrent_info = lambda: mock_torrent_info
self.libtorrent_download_impl.handle.rename_file = lambda *_: None
self.libtorrent_download_impl.tdef.get_infohash = lambda: 'a' * 20
self.libtorrent_download_impl.orig_files = ['a', 'b']
self.libtorrent_download_impl.get_save_path = lambda: 'my/path'
# If share mode is not enabled and everything else is fine, file priority should be set
# when set_selected_files() is called. But in this test, no files attribute is set in torrent info
# in order to test AttributeError, therfore, no call to set file priority is expected.
self.libtorrent_download_impl.get_share_mode = lambda: False
self.libtorrent_download_impl.set_selected_files(['a'])
self.assertFalse(mocked_set_file_prios.called)
示例6: on_wallet_created
def on_wallet_created(_):
wallet.get_balance = lambda: succeed({'available': 100000, 'pending': 0, 'currency': 'BTC', 'precision': 8})
mock_tx = MockObject()
mock_tx.hash = 'a' * 20
wallet.wallet.send_to = lambda *_: mock_tx
wallet.transfer(3000, '2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF').addCallback(
lambda _: test_deferred.callback(None))
示例7: test_encode_torrent
def test_encode_torrent(self):
"""
Test the encoding of a torrent file
"""
message = MockObject()
message.payload = MockObject()
message.payload.name = u'test'
message.payload.infohash = 'a' * 20
message.payload.timestamp = 1234
message.payload.files = [(u'a', 1234)]
message.payload.trackers = ['udp://tracker.openbittorrent.com:80/announce', 'http://google.com']
meta = self.channel_community.get_meta_message(u"torrent")
msg = MockObject()
msg.meta = meta
decoded_message = self.conversion._decode_torrent(msg, 0, self.conversion._encode_torrent(message)[0])[1]
self.assertEqual(len(decoded_message.files), 1)
self.assertEqual(len(decoded_message.trackers), 1)
message.payload.files = [(u'a', 1234)] * 1000
message.payload.trackers = ['udp://tracker.openbittorrent.com:80/announce'] * 100
decoded_message = self.conversion._decode_torrent(msg, 0, self.conversion._encode_torrent(message)[0])[1]
self.assertGreaterEqual(len(decoded_message.files), 133)
self.assertEqual(len(decoded_message.trackers), 10)
示例8: test_save_resume_preresolved_magnet
def test_save_resume_preresolved_magnet(self):
"""
Test whether a magnet link correctly writes save-resume data before it is resolved.
This can happen when a magnet link is added when the user does not have internet.
"""
self.ltmgr.initialize()
self.ltmgr.trsession = self.tribler_session
self.ltmgr.metadata_tmpdir = tempfile.mkdtemp(suffix=u'tribler_metainfo_tmpdir')
mock_tdef = MockObject()
mock_tdef.get_infohash = lambda: 'a' * 20
self.tribler_session.get_download = lambda _: None
self.tribler_session.get_downloads_pstate_dir = lambda: self.ltmgr.metadata_tmpdir
mock_lm = MockObject()
mock_lm.ltmgr = self.ltmgr
mock_lm.tunnel_community = None
self.tribler_session.lm = mock_lm
def dl_from_tdef(tdef, _):
dl = LibtorrentDownloadImpl(self.tribler_session, tdef)
dl.setup()
dl.cancel_all_pending_tasks()
return dl
self.tribler_session.start_download_from_tdef = dl_from_tdef
download = self.ltmgr.start_download_from_magnet("magnet:?xt=urn:btih:" + ('1'*40))
basename = hexlify(download.get_def().get_infohash()) + '.state'
filename = os.path.join(download.session.get_downloads_pstate_dir(), basename)
self.assertTrue(os.path.isfile(filename))
示例9: test_set_proxy_settings
def test_set_proxy_settings(self):
"""
Test setting the proxy settings
"""
def on_proxy_set(settings):
self.assertTrue(settings)
self.assertEqual(settings.hostname, 'a')
self.assertEqual(settings.port, 1234)
self.assertEqual(settings.username, 'abc')
self.assertEqual(settings.password, 'def')
def on_set_settings(settings):
self.assertTrue(settings)
self.assertEqual(settings['proxy_hostname'], 'a')
self.assertEqual(settings['proxy_port'], 1234)
self.assertEqual(settings['proxy_username'], 'abc')
self.assertEqual(settings['proxy_password'], 'def')
self.assertEqual(settings['proxy_peer_connections'], True)
self.assertEqual(settings['proxy_hostnames'], True)
mock_lt_session = MockObject()
mock_lt_session.get_settings = lambda: {}
mock_lt_session.set_settings = on_set_settings
mock_lt_session.set_proxy = on_proxy_set # Libtorrent < 1.1.0 uses set_proxy to set proxy settings
self.ltmgr.metadata_tmpdir = tempfile.mkdtemp(suffix=u'tribler_metainfo_tmpdir')
self.ltmgr.set_proxy_settings(mock_lt_session, 0, ('a', "1234"), ('abc', 'def'))
示例10: test_create_playlist
def test_create_playlist(self):
"""
Testing whether the API can create a new playlist in a given channel
"""
mock_channel_community = MockObject()
mock_channel_community.called_create = False
self.create_fake_channel("channel", "")
def verify_playlist_created(_):
self.assertTrue(mock_channel_community.called_create)
def create_playlist_called(name, description, _):
self.assertEqual(name, "test1")
self.assertEqual(description, "test2")
mock_channel_community.called_create = True
mock_channel_community.create_playlist = create_playlist_called
mock_dispersy = MockObject()
mock_dispersy.get_community = lambda _: mock_channel_community
self.session.get_dispersy_instance = lambda: mock_dispersy
expected_json = {"created": True}
post_params = {"name": "test1", "description": "test2"}
return self.do_request('channels/discovered/%s/playlists' % 'fakedispersyid'.encode('hex'), expected_code=200,
expected_json=expected_json, post_data=post_params, request_type='PUT')\
.addCallback(verify_playlist_created)
示例11: test_on_metadata_received_alert
def test_on_metadata_received_alert(self):
"""
Testing whether the right operations happen when we receive metadata
"""
test_deferred = Deferred()
def mocked_checkpoint():
test_deferred.callback(None)
mocked_file = MockObject()
mocked_file.path = 'test'
self.libtorrent_download_impl.handle.trackers = lambda: []
self.libtorrent_download_impl.handle.save_resume_data = lambda: None
torrent_dict = {'name': 'test', 'piece length': 42, 'pieces': '', 'files': []}
get_info_from_handle(self.libtorrent_download_impl.handle).metadata = lambda: lt.bencode(torrent_dict)
get_info_from_handle(self.libtorrent_download_impl.handle).files = lambda: [mocked_file]
self.libtorrent_download_impl.checkpoint = mocked_checkpoint
self.libtorrent_download_impl.session = MockObject()
self.libtorrent_download_impl.session.lm = MockObject()
self.libtorrent_download_impl.session.lm.rtorrent_handler = None
self.libtorrent_download_impl.session.lm.torrent_db = None
self.libtorrent_download_impl.handle.save_path = lambda: None
self.libtorrent_download_impl.handle.prioritize_files = lambda _: None
self.libtorrent_download_impl.get_save_path = lambda: ''
self.libtorrent_download_impl.get_share_mode = lambda: False
self.libtorrent_download_impl.on_metadata_received_alert(None)
return test_deferred
示例12: test_selected_files
def test_selected_files(self):
"""
Test whether the selected files are set correctly
"""
def mocked_set_file_prios(_):
mocked_set_file_prios.called = True
mocked_set_file_prios.called = False
mocked_file = MockObject()
mocked_file.path = 'my/path'
mock_torrent_info = MockObject()
mock_torrent_info.files = lambda: [mocked_file, mocked_file]
self.libtorrent_download_impl.handle.prioritize_files = mocked_set_file_prios
self.libtorrent_download_impl.handle.get_torrent_info = lambda: mock_torrent_info
self.libtorrent_download_impl.handle.rename_file = lambda *_: None
self.libtorrent_download_impl.get_share_mode = lambda: False
self.libtorrent_download_impl.tdef.get_infohash = lambda: 'a' * 20
self.libtorrent_download_impl.orig_files = ['a', 'b']
self.libtorrent_download_impl.get_save_path = lambda: 'my/path'
self.libtorrent_download_impl.set_selected_files(['a'])
self.assertTrue(mocked_set_file_prios.called)
self.libtorrent_download_impl.get_share_mode = lambda: False
mocked_set_file_prios.called = False
self.assertFalse(mocked_set_file_prios.called)
示例13: test_edit_playlist
def test_edit_playlist(self):
"""
Testing whether a playlist is correctly modified
"""
mock_channel_community = MockObject()
mock_channel_community.called_modify = False
my_channel_id = self.create_fake_channel("channel", "")
def verify_playlist_modified(_):
self.assertTrue(mock_channel_community.called_modify)
def modify_playlist_called(playlist_id, modifications):
self.assertEqual(playlist_id, 1)
self.assertEqual(modifications['name'], 'test')
self.assertEqual(modifications['description'], 'test')
mock_channel_community.called_modify = True
mock_channel_community.modifyPlaylist = modify_playlist_called
mock_dispersy = MockObject()
mock_dispersy.get_community = lambda _: mock_channel_community
self.session.get_dispersy_instance = lambda: mock_dispersy
self.create_playlist(my_channel_id, 1234, 42, "test playlist", "test description")
post_params = {'name': 'test', 'description': 'test'}
return self.do_request('channels/discovered/%s/playlists/1' % 'fakedispersyid'.encode('hex'),
expected_code=200, expected_json={"modified": True}, post_data=post_params,
request_type='POST').addCallback(verify_playlist_modified)
示例14: test_remove_torrent_playlist
def test_remove_torrent_playlist(self):
"""
Testing whether a torrent can be successfully removed from a playlist
"""
mock_channel_community = MockObject()
mock_channel_community.called_remove = False
my_channel_id = self.create_fake_channel("channel", "")
def verify_torrent_removed(_):
self.assertTrue(mock_channel_community.called_remove)
def modify_remove_called(playlist_id, torrents):
self.assertEqual(playlist_id, 1)
self.assertEqual(torrents, [42])
mock_channel_community.called_remove = True
mock_channel_community.remove_playlist_torrents = modify_remove_called
mock_dispersy = MockObject()
mock_dispersy.get_community = lambda _: mock_channel_community
self.session.get_dispersy_instance = lambda: mock_dispersy
self.create_playlist(my_channel_id, 1234, 42, "test playlist", "test description")
yield self.do_request('channels/discovered/%s/playlists/1/abcd' % 'fakedispersyid'.encode('hex'),
expected_code=404, request_type='DELETE')
torrent_list = [[my_channel_id, 1, 1, ('a' * 40).decode('hex'), 1460000000, "ubuntu-torrent.iso",
[['file1.txt', 42]], []]]
self.insert_torrents_into_channel(torrent_list)
self.insert_torrent_into_playlist(1234, ('a' * 40).decode('hex'))
yield self.do_request('channels/discovered/%s/playlists/1/%s' % ('fakedispersyid'.encode('hex'), 'a' * 40),
expected_code=200, request_type='DELETE', expected_json={'removed': True})\
.addCallback(verify_torrent_removed)
示例15: setUp
def setUp(self):
"""
Setup some classes and files that are used by the tests in this module.
"""
yield super(BaseTestChannel, self).setUp()
self.fake_session = MockObject()
self.fake_session.add_observer = lambda a, b, c: False
self.fake_session_config = MockObject()
self.fake_session_config.get_state_dir = lambda: self.session_base_dir
self.fake_session.config = self.fake_session_config
fake_notifier = MockObject()
fake_notifier.add_observer = lambda a, b, c, d: False
fake_notifier.notify = lambda a, b, c, d: False
self.fake_session.notifier = fake_notifier
self.fake_channel_community = MockObject()
self.fake_channel_community.get_channel_id = lambda: 42
self.fake_channel_community.cid = 'a' * 20
self.fake_channel_community.get_channel_name = lambda: "my fancy channel"
self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
self.votecast_db_handler = self.session.open_dbhandler(NTFY_VOTECAST)
self.session.get_dispersy = lambda: True
self.session.lm.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())